Here I will explain a few commonly used commands in Vim. Vim (vi) is an editor with modes of operation. There are three modes of operation in vim. They are as follows :
- Command mode - This is the default when you start vi. In command mode, the keys entered are treated as commands, rather than as text input. Most commands are single letters. Case (upper or lower) is significant. Some commands can be combined, or prefixed with qualifiers.
- Input Mode - Input mode means that what ever you type (except the Escape key) is understood by vi to be the text that you want to enter into the file, and is not a command. There are many ways of entering input mode. But only one way of exiting Input Mode - Which is by typing Escape, which puts you back into the Command mode.
- Last Line Mode - In this mode, you are actually entering commands to the ex editor, on which vi is built. You get into the last line mode from the command mode by typing a colon (:) character. The colon, and whatever you type after it, is displayed on the last line of the screen (which is how this mode gets its name). All characters you type after the colon, until the carriage return, are treated as ex commands.
Cursor Movement Commands
l- These commands move the cursor left - down - up - right in that order.
n|- Moves the cursor to the
n'th character on the line. If the character position is invalid, vi beeps. Just "
|" by itself moves the cursor to the begining of the line.
0- Moves the cursor to the begining of the line.
^- Moves the cursor to the first non blank character in the line.
$- Moves the cursor to the end of the line.
xis any character) Searches forward for the next occurence of that character from the cursor position, on the current line only.
Fx- Works the same as
fxbut in reverse direction.
bB] - Move one word forward and backward respectively.
H- Moves the cursor to the top (highest) line on the screen.
M- Moves the cursor to the middle line on screen
L- Moves the cursor to the lowest line on screen
Ctrl-d- Scrolls down by half page.
Ctrl-u- Scrolls up by half page
Ctrl-b- Scroll forward and backward a page.
nG- Where "
n" is a number. The cursor goes to that line number in the file. Example,
1Ggoes to the top of the file.
157Gto the 157th line and so on.
Mx- Where "
x" is any single letter, marks the current line with a marker called
x. Later you can move back to that line with the command
'x. Note : To go to the exact cursor position of the mark, use
`instead of the forward single quote.
``- (This is a single quote typed twice, not a double quote) acts as a toggle and takes you to the position you were at before. So you can repeatedly switch between two positions in the file.
Text Modification Commands
x- Deletes the character under the cursor.
rn- (where "
n" is any character) replaces the character under the cursor with the character
cw- (change word) puts you into input mode by replacing the word that the current cursor is on.
dw- (delete word) deletes the current word you are on.
dd- (delete line) deletes the current line you are on.
yy- yanks (copies) a line into the buffer.
p- pastes the yanked line at the position of the cursor.
i- Which means insert text before the cursor. This command puts you in Input Mode.
I- Same as above but it starts at the begining of the line.
a- Which means apend text after the cursor. This command puts you in Input Mode.
A- Same as above but starts appending at the end of the current line.
R- Replace characters.Puts you in another kind of input mode (called replace mode) in which what ever you type overwrites whatever is under the cursor. This goes on till you type Esc key.
o- Which means open mode. Opens a new line below the current line.
O- Opens a new line above the current line.
Last Line Mode Commands
:w- Saves the file in the same name that you opened it with.
:wfilename - Similar to "save as" command. In vim, you can also use the
:savfilename command to accomplish the same function.
wfilename - This saves the contents of line numbers
mto the given filename. Use
mto refer to the last line number without knowing what its value is.
:efilename - This is to start editing another file instead of the current one. Once you have used the
ecommand, you can toggle back and forth between the two files by the command
:e#. If you have made a lot of changes (and not saved), and now want to discard all of them, type
:r filename- The read command, reads in the file and inserts its contents below the current line.
What I have explained above is only a tiny sample of commands which a beginner in vi (vim) can start using. You can rest assured that there are many many more . If you want to learn more advanced commands, the best way to do is to read the Vim manual which gets installed along with the editor. Just type
:helpin the command mode and start reading the manual.