7 Useful vim recipes

Vim is my favorite editor. For everything. Python, Latex, Matlab, data-analysis, love-letters. Anything. Over the years, I have come to love several of VIM's features. Here is a continuously growing list for easy reference

1 Mksession

There are times when we have many tabs, split windows, buffer-local commands and such defined for a given vim session. Why not save the state of this session for a later date when you get back to the same project? It is easy with mksession

Save the current session with this command


:mksession path-to-save-session-file-to

And when it is time to open up the session again, just fire up vim like this


vim -S path-to-load-session-file-from

2 Encryption

For confidentiality, you can encrypt the contents of your file with vim. :X in normal mode prompts you for a key. You can also invoke vim with the -x flag to edit encrypted files. If you want utmost privacy, make sure you disable the use of a viminfo file. You can disable the use of viminfo file from the command-prompt by using the -i flag


vim -x -i '' filename

When you reopen the file, vim will request the key. If you provide an incorrect key, vim will return gibberish. You should also use the strongest possible encryption, which at this time is blowfish2


:set viminfo=
:setlocal cryptmethod=blowfish2

Note that you need to modify the file after setting the blowfish method so that it is re-encrypted using the method.

3 Timestamps and dates

Insert current date and time stamp easily into a document with the put command


:put =strftime(\"%c\")

Do so often by mapping a key to the command


:nnoremap thekey "=strftime("%c")

4 Vimtutor

Beginners can get started with vim by just firing up the command vimtutor on their terminal. It is a succinct and comprehensive reference for vim users.

5 Movement

Scroll bars are inefficient. Vim empowers Superman-like movement. You just have to know where to go.

  • gg beginning of document
  • G end of document
  • 0 beginning of line
  • $ end of line
  • w word forward
  • b word backward
  • ( sentence forward
  • ) sentence backward
  • { paragraph forward
  • } paragraph backward
  • 'a jump to the line of mark a (set by ma)
  • `a jumpt to exact position, line and column, of mark a.

6 Objects

Vim treats the following as objects and how they are denoted within commands

  • word w
  • sentence s
  • paragraph p
  • "" and '' quotes ", '
  • () block b
  • {} block B
  • <> block <
  • [] block [
  • tags such as html tags

Use a combination of movement and action commands to work directly with objects.

  • inside of a quote is represented by i". So, di" will delete the inside of the double quotes.
  • entire quote is represented by a". So, da" will delete the entire quote including the double quotes.

Similarly, i and a can be used with other symbols (e.g. aw, as, and ip) to denote the corresponding object.

7 Open file in read-only mode

To ensure that you do not accidently write into a file that you only wish to read, open it in another buffer with :view /path/to/file. You can also open it another tab with :tab sview /path/to/file.

Please support us

Help us create more concise and informative content and keep it free of paywalls and advertisements!