This is a sample section from the in-progress book 10 Minute Vim available DRM-free on Leanpub. If you find it helpful, the book has 50+ more sections like this!
The included exercises are contrived - they are designed to teach you the
muscle memory needed to perform the specific command. The exercises should be
performed on a pristine file: we recommend cloning this repository with git clone [email protected]:steveshogren/10-minute-vim-exercises.git
and running a git checkout
after each exercise to bring the files back to their pristine state.
Other Samples:
Vim allows you to perform an Ex command on any line matching a certain search pattern. The grammar is:
Examples | |
---|---|
multiple repeat | :[range]g/{pattern}/{cmd} |
multiple repeat inverted match | :[range]g!/{pattern}/{cmd} |
There is a huge number of Ex commands, and the full list is available here. The most common used are:
Examples | |
---|---|
d |
Delete line(s) |
m{LINE} |
Move line(s) |
t{LINE} |
Copy lines(s) |
p |
Print line(s) |
As an interesting aside, we could search for every line that matches using a
regular expression {re}
and print it using the global command :g/{re}/p
.
This is the source of name of the command grep
!
Examples | |
---|---|
:g/^$/d |
delete all empty lines |
:g/test/t$ |
copy all lines with test to the end of the file |
:g/horse/p |
print all lines with horse |
:g!/public function/d |
delete all lines without public function |
Use the file ozymandias.txt to prepend "Line " to all lines with poetry on
them with a single :g
command. Then delete the lines that do not have any
poetry on them with a single :g
command.
In the file variables.php, move all lines that do not access
$array
to the bottom of the file.
In the file BookCalendar.hs, show all function signatures by
printing all lines with a function signature. (Hint: they have ::
after the
function name).