See also: RegEx
Open a file for editing:
[user@host ~]$ vim /file/to/edit.txt
Screen should look like this (Command Mode, see modes below):
Enter in to "Insert Mode" to input text by pressing i
Note
-- INSERT --
to show that you are inserting text in to the file
Press Esc to exit Insert Mode.
Type :w to write (save) the file
Type :q to quit vim
Vim has multiple operating modes.
Mode | Visual indicator | How to enter |
---|---|---|
Command Mode | Row, Col count in bottom right | Default mode, can be returned to by pressing Esc |
Extended Command Mode | : in bottom left |
Press : |
Edit Mode | -- INSERT -- in bottom left |
Press i |
Visual Mode | -- VISUAL -- in bottom left |
Press v |
Visual mode in Vim is primarily used for copy and paste operations (or in Vim parlance yank and put).
To enter "Visual Mode", you must first be in "Command Mode" and to do that press Esc. If you are unsure what mode you are in, press it a couple of times it will return you to Command Mode. Once in Command Mode position your cursor where you want to start copying or pasting text and press v.
You can now move the cursor around and you will notice that text is being highlighted. If you wish to copy the highlighted text press y (for yank a.k.a. copy). Next position the cursor where you wish to insert the copied text and press p (for put a.k.a. paste). The copied text will now be inserted.
Visual mode also has a couple of sub-modes:
-- VISUAL LINE --
at the bottom left. This mode allows you to select whole lines at a time.-- VISUAL BLOCK --
at the bottom left. This mode allows you to select text in rectangles.From both of these modes, you can return to the standard Character Visual Mode by pressing v.
Vim in Visual Mode
Vim in Visual Line Mode
Vim in Visual Block Mode
The text editor vim
comes with a tutorial for getting used to the editor. You can launch it from the command line using vimtutor
.
vim command | action |
---|---|
Esc | Switches from input mode to command mode. Press this key before typing any commands |
i , a |
Switches from command mode to input mode at (i) or after (a) the current cursor position |
o |
Opens a new line below the current cursor position and goes to input mode |
A |
(shift+a) Switches to insert mode and moves the cursor to the end of the current line |
:wq |
Writes the current file and quits |
:q! |
Quits the file without applying any changes. The ! forces the command to do its work. Add the ! only if you really know and are SURE what you are doing |
:w filename |
Writes the current file with a new filename |
dd |
Cuts the current line |
yy |
Copies ('yanks') the current line |
p |
Pastes the current selection |
v |
Enters visual mode, which allows you to select a block of text using the cursor keys. Use d to cut the selection or y to copy it |
x |
To delete what is selected with visual mode or what is under the cursor in command mode |
u |
Undoes the last command. Repeat as often as necessary |
Ctrl+r | Redoes the last undo |
gg |
Goes to the first line in the document |
G |
Goes to the last line in the document |
/text |
Searches for "text" from the current cursor position forward. n can used to find the next result and N to find the previous |
?text |
Searches for "text" from the current cursor position backward |
^ |
Goes to the first position in the current line |
$ |
Goes to the last position in the current line |
!ls |
Adds the output of ls (or any other command) in the current file |
:%s/old/new/g |
Replaces all occurrences of old with new |
:%x |
Where x is a command. Replacing 'x' with d would delete all, and with y would be yank all |
:r filename |
This will read a file in to the current cursor position |
:! <shell command> |
Run a shell (not vim) command, e.g. ls a directory |
:e <file> |
Open a file for editing |
:bn |
Next buffer (file) |
:bp |
Previous buffer |
:split <filename> or :sp <filename> |
With a file open, this will open a second file and show it in a horizontal split |
:vsplit <filename> or :vs <filename |
As before, but a vertical split |
ctrl + ww | Switch between open files in split view |
vim +<number> <filename> |
From the command line, open the 'filename' and start at line 'number' |
In order to set up vim customisations, you will need to create or edit .vimrc.
Option | Explaination |
---|---|
set number | Adds line numbers |
syntax on | Adds syntax highlighting |