vim

Tips and tricks to boost productivity

Syntax#

  • :set relativenumber
  • :set number
  • :set nonumber / :set nonu
  • :pwd

Remarks#

This automatic reload will only happen if you edit your vimrc in a version full version of vim which supports autocmd.

Quick “throwaway” macros

Add this to your vimrc:

nnoremap Q @q

To start recording the “throwaway” macro, use qq. To finish recording hit q (in normal mode for both).

To execute the recorded macro, use Q.

This is useful for macros that you need to repeat many times in a row but won’t be likely to use again afterward.

Using the path completion feature inside Vim

This is very common, you memorize a path to a file or folder, you open up Vim and try to write what you’ve just memorized, but you are not 100% sure it’s correct, so you close the editor and start over.

When you want the path completion feature, and you have a file /home/ubuntu/my_folder/my_file and you are editing another file referencing to the path of the previous one:

Enter insert mode: insert or do it the way you want. Next, write /h. When the cursor is under h, press Ctrlx and then Ctrlf so the editor will complete it to /home/ because the pattern /h is unique

Now, suppose you have two folders inside /home/ubuntu/ called my_folder_1 my_folder_2

and you want the path /home/ubuntu/my_folder_2

as usual:

Enter insert mode

write /h and press Ctrlx and then Ctrlf . Now you have /home/ Next add u after /home/ and press Ctrlx and then Ctrlf . Now, you have /home/ubuntu/ because that path is unique. Now, write my after /home/ubuntu/ and press Ctrlx and then Ctrlf . The editor will complete your word until my_folder_ and you will see the directory tree so use the arrow keys to choose the one you want.

Turn On Relative Line Numbers

To delete some lines of text when you don’t know exact number of lines to delete, you try 10dd , 5dd , 3dd until you remove all the lines.

Relative line numbers solves this problem, suppose we have a file containing :

sometimes, you see a block of 
text. You want to remove 
it but you 
cannot directly get the 
exact number of 
lines to delete 
so you try 
10d , 5d 
3d until 
you
remove all the block.

Enter NORMAL mode: Esc

Now, execute :set relativenumber. Once done the file will be displayed as:

3 sometimes, you see a block of
2 text. You want to remove
1 it but you
0 cannot directly get the
1 exact number of
2 lines to delete
3 so you try
4 10d , 5d
5 3d until
6 you
7 remove all the block.

where 0 is the line number for the current line and it also shows the real line number in front of relative number, so now you don’t have to estimate the numbers of lines from the current line to cut or delete or worse count them one by one.

You can now execute your usual command like 6dd and you are sure about the number of lines.

You can also use the short form of the same command :set rnu to turn on relative numbers and :set nornu to turn off the same.

If you also :set number or have :set number already on, you’ll get the line number of the line in which the cursor is at.

3 sometimes, you see a block of 
2 text. You want to remove 
1 it but you 
4 cannot directly get the 
1 exact number of 
2 lines to delete 
3 so you try 
4 10d , 5d 
5 3d until 
6 you 
7 remove all the block. 

Viewing line numbers

To view line numbers from Default view enter

:set number

To hide line numbers

:set nonumber

There is also a shortcut for above. nu is same as number.

:set nonu

To hide line numbers, we can also use

:set nu!

Mappings for exiting Insert mode

A lot of Vim users find the Esc too hard to reach, and end up finding another mapping that’s easy to reach from the home row. Note that Ctrl-[ may be equivalent to Esc on an English keyboard, and is much easier to reach.

jk

inoremap jk <ESC> 

This one is really easy to trigger; just smash your first two fingers on the home row at the same time. It’s also hard to trigger accidentally since “jk” never appears in any English word, and if you’re in normal mode it doesn’t do anything. If you don’t type “blackjack” too much, then consider also adding inoremap kj <ESC> so you don’t need to worry about timing of the two keys.

Caps Lock

Linux

On Linux, you can use xmodmap to make Caps Lock do the same thing as Esc. Put this in a file:

!! No clear Lock 
clear lock
!! make caps lock an escape key
keycode 0x42 = Escape

Then run xmodmap file. This remaps Caps Lock to Esc.

Windows

On Windows you can use SharpKey or AutoHotkey.

macOS

If you have macOS 10.12.1 or later, you can remap Caps Lock to Escape using System Preferences. Select Keyboard, go to the Keyboard tab, and click Modifier Keys.

Keyboard Preferences

How to know the directory and/or the path of the file you are editing

To know the path of the directory your file is in you can use:

Esc to enter command mode

:pwd

This will print the path to the directory at the bottom of the editor, like this

I'm a ninja
~
~
~
~
~
/home/ubuntu/myfolder                                                1,5         All

Now, if you want to know the file name you are editing relatively to the vim working directory, you can use:

Esc to enter command mode CTRLG

I'm a ninja
~
~
~
~
~
"myfile"[Modified][New file] 1 line --100%--                         1,5         All

Finally to get the absolute path to the file you are editing, use

Esc to enter command mode,

1 and then CTRLG

I'm a ninja
~
~
~
~
~
"~/myfolder/myfile"[Modified][New file] 1 line --100%--              1,5         All

Search within a function block

To search for text foo within a {} block surrounding the cursor use the following command (<ESC> - escape key, <CR> - enter key) :

vi{<ESC>/\%Vfoo<CR>

now you can jump between the matches within the block by pressing n and p. If you have hlsearch option enabled this will highlight all the matches. \%V is a special search pattern part, that tells vim to search only in the visually selected area. You can also make a mapping like this:

:vnoremap g/ <ESC>/\%V

After this the above command is shortened to the following:

vi{g/foo<CR>

Another useful trick is to print all the lines containing the pattern:

vi{
:'<,'>g/foo/#

The '<,'> range is inserted automatically.

See :help range and :help :g.

Copy, move or delete found line

A lot of users find themselves in a situation where they just want to copy, move or delete a line quickly and return to where they were.

Usually, if you’d want to move a line which contains the word lot below the current line you’d type something like:

/lot<Esc>dd<C-o>p

But to boost productivity you can use this shortcut in these cases:

" It's recommended to turn on incremental search when doing so
set incsearch

" copy the found line
cnoremap $t <CR>:t''<CR>
" move the found line
cnoremap $m <CR>:m''<CR>
" delete the found line
cnoremap $d <CR>:d<CR>``

So a search like this:

/lot$m

would move the line which contains lot below the line your cursor was on when you started the search.

Write a file if you forget to sudo before starting vim

This command will save the open file with sudo rights

:w !sudo tee % >/dev/null

You can also map w!! to write out a file as root

:cnoremap w!! w !sudo tee % >/dev/null

Automatically reload vimrc upon save

To automatically reload vimrc upon save, add the following to your vimrc:

if has('autocmd') " ignore this section if your vim does not support autocommands
    augroup reload_vimrc
        autocmd!
        autocmd! BufWritePost $MYVIMRC,$MYGVIMRC nested source %
    augroup END
endif

and then for the last time, type:

:so $MYVIMRC

The next time you save your vimrc, it will be automatically reloaded.

nested is useful if you’re using vim-airline. The process of loading airline triggers some autocommands, but since you’re in the process of executing an autocommand they get skipped. nested allows triggering nested autocommands and allows airline to load properly.

Command line completion

set wildmenu to turn on completion suggestions for command line.
Execute the following

set wildmenu
set wildmode=list:longest,full

Now if you do say, :color tab,

You’ll get

256-jungle  Benokai  BlackSea  C64  CandyPaper  Chasing_Logic  ChocolateLiquor  
:color 0x7A69_dark

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow