{"id":2784,"date":"2023-04-08T19:35:52","date_gmt":"2023-04-08T14:05:52","guid":{"rendered":"https:\/\/smarttech101.com\/?p=2784"},"modified":"2023-09-05T07:51:28","modified_gmt":"2023-09-05T02:21:28","slug":"how-to-use-vim-neovim","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/how-to-use-vim-neovim\/","title":{"rendered":"How to Use Vim\/Neovim"},"content":{"rendered":"\n
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.<\/a> Vim keys allow you to stay on keys in the home rows<\/strong> 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<\/strong>. For example, \ud83d\udcd2<\/strong> Note: <\/strong>I have boldened the important keys here. You need to start with them. At a later stage, you can learn the rest.<\/p>\n\n\n\n Vim has many types of modes some of which are:<\/p>\n\n\n\n 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.<\/p>\n\n\n\n To move the cursor up, down, left, and right, use k, j, h, and l<\/strong>. You can also use the arrow keys.<\/p>\n\n\n\n To move to the top, middle, or bottom of the screen, use H, M, or L respectively.<\/p>\n\n\n\n To move to the start, and end of an upcoming word, use w and e <\/strong>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.<\/strong> For punctuation including words, use B instead.<\/p>\n\n\n\n To jump to the start, and end of the line, use 0, and Press gg to reach the first line of the file. For moving to the last line, use G.<\/strong> For n’th line press nG.<\/p>\n\n\n\n You can also move to the upcoming characters in a line<\/strong>. For this, use f and F. For example<\/strong>, 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.<\/p>\n\n\n\n To repeat the previous f, t, F, or T movement, use But I find a plugin To jump to the next paragraph\/function\/block in code or texts, 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 To move to a matching character, use %. For example<\/strong>, if you are at To open the man page for word under the cursor, use To replace a single character r. For example, <\/strong>pressing rx on a character will replace that character by x.<\/p>\n\n\n\n To change (replace) the entire line, use cc. To change (replace) to the end of the line c$<\/strong>. To change (replace) the entire word, use ciw.<\/strong> To change (replace) to the end of the word cw. After all these change actions, you will reach the insert mode to type something.<\/p>\n\n\n\n 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.<\/p>\n\n\n\n To undo<\/strong> any action, use u. For redo<\/strong> use Ctrl + r.<\/p>\n\n\n\n To Repeat the last command or group of commands, use To shift text right, use the keys To convert lowercase to uppercase and vice-versa, use the key \ud83d\udcd2 Note:<\/strong> It involves selecting a line. You can use the visual mode (explained below) for this purpose.<\/p>\n\n\n\n \ud83d\udccb Yank(Copy):<\/strong> The simplest method to copy in vim is to select texts using the visual mode and then pressing y will copy<\/strong> 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<\/strong>, there is a text To blink the yanked text, configure your neovim<\/a> according to this.<\/p>\n\n\n\n \ud83d\udccb Cut:<\/strong> Here too, you can select the text using visual mode and then press d to cut <\/strong>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 \ud83d\udccb Paste<\/strong>: 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.<\/strong><\/p>\n\n\n\n To get into the insert mode press Now, use i to insert before the cursor and I to insert at the beginning of line (or 0i). <\/p>\n\n\n\n To insert (append) after the cursor use a. And use A to insert (append) at the end<\/span> of the line.<\/p>\n\n\n\n To open a new line below <\/span>the current line, use o. To open a new line above<\/span> the current line, use O.<\/strong> To insert (append) at the end of the word, use ea.<\/p>\n\n\n\n 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<\/a> To start visual mode, use v<\/strong>. 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).<\/p>\n\n\n\n To Visual Line mode, press V<\/strong>. Here, pressing the up and down keys selects\/deselects a whole line<\/strong> instead of a single character.<\/p>\n\n\n\n To start visual block mode, press Ctrl + v<\/strong>. It is selects the lines rectangularly<\/strong>.<\/p>\n\n\n\n Please note that all movement keys (h, j, k, l, etc.) work in the visual mode.<\/p>\n\n\n\n To exit visual mode, press the Escape button just like that in the insert mode.<\/p>\n\n\n\n Macros are very powerful. Using them, you can record a group of keys<\/strong> at a single key and execute<\/strong> this key to execute the whole group at any time.<\/p>\n\n\n\n To start recording macros at For example<\/strong>, 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 To rerun the last run macro, use To write (save) the file, but don’t exit use :w.<\/strong> To write out the current file using sudo, use After saving changes to the file, quit from it using 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 To write and quit <\/strong>at the same time, use To write (save) and quit on all tabs, use You can also save files with other names. This is akin to “save as” in Windows and other OSes. For this, use To close Current Pane, use To search for any pattern in Vim, use To search backward for 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 To replace all For the above action but with confirmations In simple terms, buffers are more like tabs. They allow you to open multiple files at once.<\/p>\n\n\n\n You can use the console to move around your buffers.<\/p>\n\n\n\n To go to the next buffer use 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.<\/p>\n\n\n\n To configure splits in Neovim\/Vim, look here.<\/a><\/p>\n\n\n\nd$<\/code> and
y$<\/code>,
3j<\/code>,
3d<\/code>. So, you can memorize them easily. These keys should work on vim, neovim, vi, and any other plugin which provides vim key mappings.<\/p>\n\n\n\n
Table of Contents<\/h2>\n\n\n\n
\n
\n
\n
What are the Modes in Vim<\/h2>\n\n\n\n
\n
:<\/code> or
\/<\/code> to enter the console mode.<\/li>\n<\/ol>\n\n\n\n
Keys in Normal Mode in Vim\/Neovim<\/h2>\n\n\n\n
Movement Keys in Vim\/Neovim<\/h3>\n\n\n\n
$<\/code> <\/strong>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
^<\/code>, and
g_<\/code> respectively.<\/p>\n\n\n\n
;<\/code>. To do the same, but in the backward direction use
,<\/code>.<\/p>\n\n\n\n
justinmk\/vim-sneak<\/a><\/code> to be more useful.<\/strong> 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.<\/p>\n\n\n\n
}<\/code>. For such movements but in the reverse direction, use
{<\/code>.<\/p>\n\n\n\n
Ctrl + e<\/code>, and
Ctrl + y<\/code> respectively. To move back, and forward one full screen, use
Ctrl + b<\/code>,
Ctrl + f<\/code><\/strong> 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.<\/strong><\/p>\n\n\n\n
(<\/code> and want to move to the
)<\/code>, you need to press the
%<\/code>. Similarly, you can jump to
}<\/code>, and
]<\/code> from
{<\/code> and
[<\/code> respectively. See the help page
:h matchpairs<\/code> for more information.<\/p>\n\n\n\n
Other Keys<\/h3>\n\n\n\n
K<\/code>. For example,<\/strong> you are editing a shell script file and there is a term
cd<\/code>. If you want to open the man page of the
cd<\/code> in neovim itself, you can move your cursor right on cd, and then press K.<\/p>\n\n\n\n
.<\/code> But if you are finding it difficult to use it, try macros which are explained in the below paragraph.<\/p>\n\n\n\n
><\/code> and to shift text left press
<<\/code><\/strong>. 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).<\/p>\n\n\n\n
~<\/code>.<\/p>\n\n\n\n
Cut, Copy, and Paste in Neovim\/Vim<\/h3>\n\n\n\n
The_Notebook_by_\"Nicholas_Sparks\"<\/code>. Your cursor is on some character between the double quotes. pressing
yi\"<\/code> will yank in double-quotes. Similarly, execute
yi(<\/code>,
yi{<\/code>, etc.<\/p>\n\n\n\n
yi\"<\/code>,
yi{<\/code>, etc.,
di\"<\/code>,
di{<\/code>, etc. also holds true here. <\/p>\n\n\n\n
Keys in Insert mode<\/h2>\n\n\n\n
i<\/code><\/strong>. To get out of it, press the Escape button.<\/p>\n\n\n\n
.vimrc<\/code>,
init.vim<\/code><\/p>\n\n\n\n
Keys in Visual Mode<\/h2>\n\n\n\n
Macros in Vim\/Neovim<\/h2>\n\n\n\n
a<\/code>, press qa. To record at
b<\/code>, 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<\/code> by pressing
@a<\/code>, recording at
b<\/code> by pressing
@b<\/code>.<\/p>\n\n\n\n
qa$xxxxxjq<\/code>. Now, just press
@a<\/code> to remove 5 spaces at the end of any line.<\/p>\n\n\n\n
@@<\/code>.<\/p>\n\n\n\n
Keys in the console<\/h2>\n\n\n\n
Save and\/or quit a file in Neovim\/Vim<\/h3>\n\n\n\n
:w !sudo tee %<\/code>. Here,
%<\/code> means the current file, hence
tee %<\/code> will form a type of “T junction” into this file. <\/p>\n\n\n\n
:q<\/code><\/strong><\/p>\n\n\n\n
:w<\/code> or quit without saving using :q!<\/strong> or
ZQ<\/code>. <\/p>\n\n\n\n
:wq<\/code> or
:x<\/code><\/strong> or
zz<\/code>.<\/p>\n\n\n\n
:wqa<\/code>.<\/p>\n\n\n\n
:saveas file<\/code> or,
:w new_name<\/code>. <\/p>\n\n\n\n
:close<\/code><\/p>\n\n\n\n
Search for a pattern in Vim\/Neovim<\/h3>\n\n\n\n
\/pattern<\/code>.<\/strong> Now, you can use any pattern like those involving
*<\/code>, 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.<\/strong><\/p>\n\n\n\n
pattern<\/code>, use
?pattern<\/code>. Here too you can use n or N to move over the results.<\/p>\n\n\n\n
:noh<\/code><\/p>\n\n\n\n
oldtext<\/code> with
newtext<\/code> everywhere in the file, use
:%s\/oldtext\/newtext\/g<\/code>.<\/p>\n\n\n\n
:%s\/oldtext\/newtext\/gc<\/code>. <\/strong>Now, it will ask for y (for yes), n (for no) before replacing each finds.<\/p>\n\n\n\n
Buffer and Split windows in Vim\/Neovim<\/h3>\n\n\n\n
:bnext<\/code> or
:bn<\/code>. To go to the previous buffer, use
:bprev<\/code> or
:bp<\/code>. To delete a buffer (close a file) use
:bd<\/code>. To list all open buffers
:ls<\/code>. To open a file in a new buffer and split window, use
:sp file<\/code>. To open a file in a new buffer and vertically split window
:vsp file<\/code>.<\/p>\n\n\n\n
Way Ahead<\/h2>\n\n\n\n