Learning to escape VIM
I have a confession, the first I launched vim I could not even work out how to quit it so I took the lazy way out and just closed the terminal. This put me right off vim for quite a while. Now its time to really try to learn it.
Vim has two modes: insert and normal, when you launch vim it will go straight into normal mode, from normal mode type i to switch to insert mode and then press ESC to return back to normal mode.
Normal mode basics
You can delete the character under the cursor using x and using :w to save. To cut the current line use dd, this will delete the text on the current line and also delete the line itself, the cursor will move to the next line, which is now where the original line was. To copy the current line use yy. Now to paste the recently cut line, use p to insert the text onto the line below where the cursor currently is or P to insert text before cursor.
Instead of the arrow keys, you can make use of hjkl to move the cursor, but personal I prefer to use the arrow keys
Entering Insert mode
So far we have entered insert mode using i, there are some alternative ways to enter insert mode, which allow you to move to insert mode and perform a useful operation in one key stoke, some of the alternative methods to enter insert mode are:
- a – insert mode and move cursor back one charactor
- o – insert mode and insert a new line after the current one
- O – insert mode and insert a new line before the current one
- cw – insert mode and delete the current word
Moving around normal mode
As well as using the arrow keys (or hjkl) to navigate around normal mode, there are plenty of useful extra shortcuts for navigating around. 0 jumps to first character in the row and $ goes to the last character in the row. ^ jumps to the first non-blank character of a line and g_ goes to the last non_blank character of line.
To search for a particular work or character, use / and enter search term. This will move the cursor to the next occurrence of the search term after the cursor, so to search for the first occurrence of a term in a file, put the cursor at the start of the file and then use / to search for the term.
To jump to the start of the next word use w and to go to end of this word use e, when a word is composed of letters and underscores only, if you want to word to include special characters use W and E instead
Text editor basics
u is used to undo and Crtl-r to redo. To open a file use :e , :w to save, :saveas , 😡 to save and quit. :q! to quit without saving. . will repeat last command and N