How to Use Vim/Neovim

April 8, 2023

In this article, I will talk about how to use vim/neovim - its key mapping mainly. For other vim/neovim-related matters, look at these articles. Vim keys allow you to stay on keys in the home rows and other keys around that. Therefore, when you use Vim keys, you don't have to move your fingers a lot. On top of that, most of these keys work in a similar fashion. For example, d$ and y$, 3j, 3d. So, you can memorize them easily. These keys should work on vim, neovim, vi, and any other plugin which provides vim key mappings.

📒 Note: I have boldened the important keys here. You need to start with them. At a later stage, you can learn the rest.

What are the Modes in Vim

Vim has many types of modes some of which are:

  1. Normal Mode: It is the mode used when you want to move around texts your using your keys such as j, k, etc. It does not type anything.

  2. Insert Mode: To type into your text, you need to get into Insert Mode. Whatever keys, you press, are typed into the insert mode.

  3. Visual Mode: This mode is used to select a chunk of text. There are various types of visual modes - visual line mode, visual block mode, etc.

  4. Console Mode: This mode is used to execute a command or search for a text in vim. You need to press : or / to enter the console mode.

To find out the current mode, look at the bottom line in your editor. In figure 2, it is Visual Block Mode. The bottom line does not indicate the normal mode since it is the most used mode.

Keys in Normal Mode in Vim/Neovim

Movement Keys in Vim/Neovim

To move the cursor up, down, left, and right, use k, j, h, and l. You can also use the arrow keys.

To move to the top, middle, or bottom of the screen, use H, M, or L respectively.

To move to the start, and end of an upcoming word, use w and e respectively. If the word contains punctuation, use W and E instead. Now, your jumps will include this punctuation as well. You can also move in a backward direction. To move back to the start of the word, use b. For punctuation including words, use B instead.

To jump to the start, and end of the line, use 0, and $ respectively. A line can have non-blank characters such as space at its beginning and/or end. To jump to the first and last non-blank character of a such line, use ^, and g_ respectively.

Press gg to reach the first line of the file. For moving to the last line, use G. For n'th line press nG.

You can also move to the upcoming characters in a line. For this, use f and F. For example, to jump to the next occurrence of character x, use fx. To jump to the next occurrence of character x but just before the character, use tx. To jump to the previous occurrence of character x, use Fx, To Jump to after previous occurrence of character x, use Tx.

To repeat the previous f, t, F, or T movement, use ;. To do the same, but in the backward direction use ,.

But I find a plugin [justinmk/vim-sneak](https://github.com/justinmk/vim-sneak) to be more useful. Just press sx or Sx for it to highlight all next or previous occurrences of x. Then press one of the suggested keys to sneak to the wanted position.

using justinmk/vim-sneak to sneak to any character

fig 1: using justinmk/vim-sneak to sneak to any character

To jump to the next paragraph/function/block in code or texts, use }. For such movements but in the reverse direction, use {.

If you want to put the cursor in the middle of the screen, use zz. To move the screen down and up one line (without moving the cursor), use Ctrl + e, and Ctrl + y respectively. To move back, and forward one full screen, use Ctrl + b, Ctrl + f respectively. For moving forward 1/2 a screen, you need to use Ctrl + d. For moving back 1/2 a screen use Ctrl + u instead.

To move to a matching character, use %. For example, if you are at ( and want to move to the ), you need to press the %. Similarly, you can jump to }, and ] from { and [ respectively. See the help page :h matchpairs for more information.

Other Keys

To open the man page for word under the cursor, use K. For example, you are editing a shell script file and there is a term cd. If you want to open the man page of the cd in neovim itself, you can move your cursor right on cd, and then press K.

To replace a single character r. For example, pressing rx on a character will replace that character by x.

To change (replace) the entire line, use cc. To change (replace) to the end of the line c$. To change (replace) the entire word, use ciw. To change (replace) to the end of the word cw. After all these change actions, you will reach the insert mode to type something.

To join the line below to the current one with one space in between, use J and for the same but without space, use gJ.

To undo any action, use u. For redo use Ctrl + r.

To Repeat the last command or group of commands, use . But if you are finding it difficult to use it, try macros which are explained in the below paragraph.

To shift text right, use the keys > and to shift text left press <. This shifting means the selected text is moved left or right by a tab. To select these texts, you can use the visual mode (explained below).

To convert lowercase to uppercase and vice-versa, use the key ~.

Cut, Copy, and Paste in Neovim/Vim

📒 Note: It involves selecting a line. You can use the visual mode (explained below) for this purpose.

📋 Yank(Copy): The simplest method to copy in vim is to select texts using the visual mode and then pressing y will copy them. To yank (copy) a line, use yy. No need to select any text here. Similarly, To yank 2 lines without selecting them, use 2yy. To yank the characters of the word from the cursor position to the start of the next word, use yw. To yank to end of line y$. You can also copy all texts inside a pair of quotes or brackets. For example, there is a text The_Notebook_by_"Nicholas_Sparks". Your cursor is on some character between the double quotes. pressing yi" will yank in double-quotes. Similarly, execute yi(, yi{, etc.

To blink the yanked text, configure your neovim according to this.

📋 Cut: Here too, you can select the text using visual mode and then press d to cut them. Or, you can cut the text without selecting it. To cut a line, use dd. For 2 lines, use 2dd. To cut the word, use dw. To cut to the end of the line, use D or d$. To cut a single character use x. Like yi", yi{, etc., di", di{, etc. also holds true here.

📋 Paste: After yanking/cutting, the characters/words/lines go to clipboards. Now, you can paste them using p. To paste the clipboard in the line after the cursor, use p. To paste before the cursor, use P.

Keys in Insert mode

To get into the insert mode press i. To get out of it, press the Escape button.

Now, use i to insert before the cursor and I to insert at the beginning of line (or 0i).

To insert (append) after the cursor use a. And use A to insert (append) at the end of the line.

To open a new line below the current line, use o. To open a new line above the current line, use O. To insert (append) at the end of the word, use ea.

There are not many keys in the insert mode. But you can bind key(s) from the emacs in the insert mode. I have done such bindings. Just put them in your neovim/vim configuration .vimrc, init.vim

Keys in Visual Mode

To start visual mode, use v. Now, you can use the movement keys (h, j, k, l, arrow keys) to select lines, then execute any vim command (like y to copy).

To Visual Line mode, press V. Here, pressing the up and down keys selects/deselects a whole line instead of a single character.

To start visual block mode, press Ctrl + v. It is selects the lines rectangularly.

visual block mode in vim selecting texts rectangularly

figure 2: visual block mode in vim selecting texts rectangularly

Please note that all movement keys (h, j, k, l, etc.) work in the visual mode.

To exit visual mode, press the Escape button just like that in the insert mode.

Macros in Vim/Neovim

Macros are very powerful. Using them, you can record a group of keys at a single key and execute this key to execute the whole group at any time.

To start recording macros at a, press qa. To record at b, press qb and so on. Now, execute all the normal/insert/other mode commands (like j, k, y, v, x, etc.). After you are done, quit the recording by pressing q. Now, you can call forth recording at a by pressing @a, recording at b by pressing @b.

For example, suppose you want to remove 5 spaces at the end of each line. You can go to the beginning line and press this sequence of keys qa$xxxxxjq. Now, just press @a to remove 5 spaces at the end of any line.

To rerun the last run macro, use @@.

Keys in the console

Save and/or quit a file in Neovim/Vim

To write (save) the file, but don't exit use :w. To write out the current file using sudo, use :w !sudo tee %. Here, % means the current file, hence tee % will form a type of "T junction" into this file.

After saving changes to the file, quit from it using :q

If you try to quit a file that has been edited and you forgot to save it, quitting fails. Now, you can save it first using :w or quit without saving using :q! or ZQ.

To write and quit at the same time, use :wq or :x or zz.

To write (save) and quit on all tabs, use :wqa.

You can also save files with other names. This is akin to "save as" in Windows and other OSes. For this, use :saveas file or, :w new_name.

To close Current Pane, use :close

Search for a pattern in Vim/Neovim

To search for any pattern in Vim, use /pattern. Now, you can use any pattern like those involving *, etc. There can be more than one search result. Vim will put you at a result. To move to other results in a backward/forward direction, use n or N.

To search backward for pattern, use ?pattern. Here too you can use n or N to move over the results.

After searching for a pattern, you might see that your vim/neovim put special highlights over all the finds. After you are done with these search results, you can remove these highlightings. For that use the command :noh

highlights created by vim-search

figure 3: highlights created by vim-search

To replace all oldtext with newtext everywhere in the file, use :%s/oldtext/newtext/g.

For the above action but with confirmations :%s/oldtext/newtext/gc. Now, it will ask for y (for yes), n (for no) before replacing each finds.

Buffer and Split windows in Vim/Neovim

In simple terms, buffers are more like tabs. They allow you to open multiple files at once.

You can use the console to move around your buffers.

To go to the next buffer use :bnext or :bn. To go to the previous buffer, use :bprev or :bp. To delete a buffer (close a file) use :bd. To list all open buffers :ls. To open a file in a new buffer and split window, use :sp file. To open a file in a new buffer and vertically split window :vsp file.

To split the window, use Ctrl + ws. To switch windows, use Ctrl + ww. To quit a window Ctrl + wq. To split the window vertically, use Ctrl + wv. To move the cursor to the left window (vertical split), use Ctrl + wh. For the right window, use Ctrl + wl. And to move downwards, use Ctrl + wj.

To configure splits in Neovim/Vim, look here.

Way Ahead

That's all. To learn more, open the help page for any keyword using :help keyword. You can find a cheat sheet of vim commands over here. If you have any queries/suggestions, mention them below.