Skip to main content

vim tips and tricks

· 4 min read
Tomasz Kubacki
Random dev

This post is a list of vim tips and tricks i found useful during my never ending jurney of learing vim.

Learn vim basic

You need to know some vim basics to use those tips. Nothing really deep just how to start, leave or change vim mode. If you have Vim installed, you probably have vimtutor command installed as well. Just enter your command line and type

vimtutor

to check it out.

Modes and leaving Vim

Use ctrl+c to switch to normal mode

There are two ways to switch to normal mode one is to use escape key, but often more convenienient is to use ctrl+c shortcut

Famous leave vim jokes arouse, because users can't enter the normal mode after editing.

Leave Vim with multiple files and tabs opened

As you know to quit the editor you need to type

:q

in normal mode.

To leave Vim with multiple windows and tabs opened use

:qa

Beyond normal vim movements like scrolling with

ctrl + f

and scrolling backward with

ctrl + b

there are several very useful commands if you edit source code provided by CoC plugin (more about plugins at end of this post).

gd

will go to function/variable definition and

gr 

will find all variable/function references.

After that you can use go back to last cursor position with

ctl+o

and forward to last position with

ctl+i

Split windows and navigate between them

The magic command to split vertically is :vsplit command

:vs 

or

ctrl + w v

and to split horizontally use :split

:sp

or

ctrl+w s

To move around use

ctrl+w [arrow key]

command.

Resizing

increase size

ctrl+w +

decrease size

ctrl+w +

make windows size equal

ctrl+w =

Maximize current window

ctrl+w _

File Browser

Vim has built in file browser. You can open it using:

:Ex

command.

However it's often more convenienient to use

:Lex

This one will show up on the left side and open files in a new window on the right of the browser.

Run Terminal inside vim

Vim has build in terminal plugin. You can open it with

:term

in the normal mode. This will split your current editor window and create terminal in the upper window.

If you want terminal below current window use

:bel term

Copy between editor and terminal

Copy from the editor to the terminal

Assume you did copy some text into default register (e.g. by using yy coping line), then you can paste it into terminal by using

ctrl+w ""

note: it is possible to copy from other registers as well. To list registers use the :register command

Copy from the terminal to the editor

It's possible to switch terminal into normal read-only editor by pressing

ctrl+w N

(it's capital N)

In this mode you can copy whatever you like just like in the normal editor window.

To go back to the terminal mode just press i (like insert)

Macros

You can start recording a macro in vim using 'q' followed by register name. e.g.

qa

will start record macro and store it in the 'a' register.

Another

q

will stop recoding.

Now pressing

@a

will reply previously recorded macro.

Plugins

Use decent plugin manager

One modern and easy to use vim plugin manager is vim-plug

Use NERDTree plugin

NERDTree is more advanced file browser after install just open it with

:NERDTree

in normal mode

Use CoC plugin

CoC plugin makes almost IDE like experience from your vim or nvim setup.

It's main purpose is to allow autocompletion, show context code errors and documentation, but one of the most useful is advanced code navigation (like gd - 'go to definition' mentioned above)

Fin

Vim has enormous ecosystem of plugins but also large community (especially if you count both vim and nvim). It's worth to check out vim reddit group for even more tips and interesting plugins.

You may also be interested in my vim config.