Vim has been my favorite text editor for a long while now. Unlike common belief, I actually like its learning curve: you can learn the basics within 5 minutes of using
vimtutor, and then keep on learning for years.
Still if you've just finished vimtutor and wonder where to go next, here are 3 skills I find useful at any text editing task I'm facing.
Back To Last Edit (Automatic Marks)
Vim tracks our movement and changes in the file. This provides simple features like undo and redo, but also more advanced ones like automatic marks and jump list.
Marks are places in the file that you want to remember. You can set a mark with
m<id> and go back to it with
<id> can be any letter or digit.
To try it, open a text file for editing, move the cursor to any line, press
ma, move the cursor to any other location and press
'a, and your cursor jumps back to the marked line.
Automatic marks are set automatically by vim in response to user actions. The most useful in my opinion is the last edit mark. Go to any line in the file, add some text and then move to any other location in the file. Now you can use
'. to jump back to the last line that was edited, or ``.` to jump to the exact character position of the change.
Even normal movements in vim are tracked using what vim calls a
jumplist. We can navigate the jumplist moving to the last cursor position with
Ctrl - O or forward with
Ctrl - I.
Read more on marks by typing
:help mark in vim, and on jumps by typing
Vim's visual block lets you select a block of text and modify or delete it. We use
Ctrl - V to start visual block mode, then use normal movement operations to set the size of the block and finally use one of the following:
dto delete the selected block
Ito start bulk insert operation: add text to the beginning of each selected line.
cto start bulk change operation: write some text and all selected words will change to it.
So to bulk insert comments in multiple lines we'll enter visual mode, select these lines, press
I and then type the
# characters and finally press
Esc to have the
# copied to all lines.
Read more on visual block by typing:
help visual-block in vim.
Editing Multiple Files
Learning how to edit multiple files simultaneously changed everything in my use of vim. Instead of using it to edit single files sporadically I now prefer to leave vim always on.
Vim can load multiple files into memory as buffers. We'll use the following commands to edit multiple files:
:e <filename>open another file for editing.
bdclose current buffer.
:lsshow all active buffers.
:b <bufferid>move to a buffer identified by buffer id (either part of its name or its number, both taken from
bnmove to edit the next buffer.
bpmove to edit the previous buffer.
Pro Tip: You can press
:e . to see a list of all files in the current directory. To open a file select it and press Enter.
Read more on buffers by typing
:help edit and
:help files in vim.