You can open Vim by typing vim and then a filename at your command line. That filename can be a file that already exists, or a new file you want to create
$ vim filename
By default, you start in command mode. In most modern versions of Vim, you should be able to browse the document if it exists with the arrow keys (or use Ctrl-D to scroll down more quickly).
You can also navigate using the keyboard strokes:
- h – move left
- l – move right
- k – move up
- j – move down
To insert, or edit text, we need to switch from command mode into Insert mode. You do that by pressing the letter i. You should see this near the bottom of the screen:
– INSERT –
Now, you can use the keyboard as you would normally to type text into the document. Once you’re done making the edit and want to switch back to Normal mode, press Esc.
The INSERT message at the bottom should go away, and anything you type will be interpreted as a command, not as text to type into the document.
While in command mode (remember – Esc will get you back to it at any time), typing these commands in and pressing Enter will do the following:
- :w – save the file (the ‘w’ stands for write).
- :q – quit the file. If you haven’t saved the file since you last changed it, you will be warned and it will not quit. If you want to delete the changes…
- :q! – quit the file, not saving your changes.
- :wq – save and quit the file in one fell swoop!
To switch from the command mode to the insert mode type one of the following:
- i – switch to insert mode before the current position
- a – switch to insert mode after the current position (append)
- I – jump to the first non-blank character in the current line and switch to the insert mode
- A – jump to the last character of the current line and switch to the insert mode
- So here the most important commands
- 0 – first column of the line
- ^ – first non-blank character of the line
- w – jump to next word
- W – jump to next word, ignore punctuation
- e – jump to word-end
- E – jump to word-end, ignore punctuation
- b – jump to word-beginning
- B – jump to word-beginning, ignore punctuation
- ge – jump to previous word-ending
- gE – jump to previous word-ending, ignore punctuation
- g_ – jump to last non-blank character of the line
- $ – jump to the last character of the line
Inserting text is pretty simple in Vim, just type i and start typing. But Vim offers quite sophisticated text-editing commands.
- d – delete the characters from the cursor position up the position given by the next command (for example d$ deletes all character from the current cursor position up to the last column of the line).
- c – change the character from the cursor position up to the position indicated by the next command.
- x – delete the character under the cursor.
- X – delete the character before the cursor (Backspace).
- y – copy the characters from the current cursor position up to the position indicated by the next command.
- p – paste previous deleted or yanked (copied) text after the current cursor position.
- P – paste previous deleted or yanked (copied) text before the current cursor position.
- r – replace the current character with the newly typed one.
- s – substitute the text from the current cursor position up to the position given by the next command with the newly typed one.
- . – repeat the last insertion or editing command (x,d,p…).
Doubling d, c or y operates on the whole line, for example yy copies the whole line.