Vim Commands
In this post, I gather the necessary commands for using Vim editor. This will develop over time as I add more commands and descriptions to it as time passes. Any comments and ideas on how to make it better are welcome.
Vim is a free and open-source, screen-based text editor program for Unix.
Usually, people who work with remote servers through SSH connections find it useful.
To install it:
$ sudo apt update # Update package database$ sudo apt install vim
Check its version:
$ vim --version
Opening and Closing a File
For opening a file, it is simple like other Linux commands:
$ vim name_of_the_file
For closing the file, after pressing the ESC key, enter:
:q
In some cases, the editor will prevent the user from exiting a file. For forcing the editor to exit, the exclamation mark is needed like as follows:
:q!
Editing a File
For editing a file, first, you should find where you want to change (by the moving commands as follows in this post). Then by pressing Insert key or i key to be able to insert characters or remove.
For inserting text at the beginning of a line: shift+i
Saving a File
After Editing the file, press Esc, then enter the following command:
:wq
Moving the cursor
Single Character Movement
For moving the cursor, the easiest way is to use the arrow keys of the keyboard. The numeric pad also can be used when the Num Lock is off. If you want to look like old times use H, J, K, L keys for moving LEFT, DOWN, UP, RIGHT.
Single Word Movement
For moving in the granularity of word forward use w and for moving back use b.
Shift+w and shift+b are used when we want to jump over only based on spaces, not punctations.
e moves the cursor of each word forward stopping before the last character of the word. It can also be used with the shift to only consider spaces as separators.
Single Line Movement
Return or Enter key is for moving one line down.
Moving to the end of a line: $ (ctrl+4)
Moving to the start of a line: ^ (ctrl+6)
In a Page Movement
To move to the top, middle, and bottom of a current page, shift+h (H), shift+m (M), and shift+l (L) are used.
Page Movement
For moving a page down Ctrl+f
For moving half a page down Ctrl+d
For moving a page up Ctrl+b
For moving half a page up Ctrl+u
Undoing changes
u for undoing the command
shift+u (U) for undoing changes to a line.
Jumping to the end and beginning of a file or a specific line with its number
For jumping to the end of a file: shift+g (G), ctrl+End
For jumping to the start of a file: g-g, ctrl+Home
For jumping to a line with its number, first enter the line number then shift+g.
150G
Searching
For finding a word in vim after hitting Esc, enter ? or /, then enter the word.
/word_to_find OR ?word_to_find
Forgoing the other occurrences of the word, use n.
For searching for a word that is in the text, we just need to place the cursor over it, then press * (shift+8) or # (shift+3) to search for that.
Deleting a character, word, line
Deleting the character on the right side of the cursor: x
For deleting the character on the left side of the cursor: shift+x (X)
For deleting a word or part of it, place the cursor where you want the right part of it to be saved then press: d-w (hook: delete-word)
To delete a line: dd
To delete five lines: 5dd
Lines copy, move
For copying a line just move the cursor to that line (does not matter whereof that line is), then by yy or shift+y copy the line. Then, move to the place you want to put it and there press p.
For moving after a deletion with dd, we can move the cursor to the place we want to move the line and put it there by pressing p.
For more information on copying, the following link can be helpful!
https://linuxize.com/post/how-to-copy-cut-paste-in-vim/