Vim has been a Unix/Linux programmer’s friend since decades, once you get a strong grip on its commands you can do things much faster in a few keystrokes, for example : you need not even use arrow keys to move the cursor instead use ‘position by character‘ commands which is given in 1st table
Here is a list of useful vim commands arranged in groups, helpful for quick reference.
Commands for Positioning Cursor in the Window
Position by Character
Command | Function |
---|---|
h | Moves the cursor one character to the left |
Backspace | Moves the cursor one character to the left |
l | Moves the cursor one character to the right |
Space bar | Moves the cursor one character to the right |
0 | Moves the cursor to the beginning of the current line |
$ | Moves the cursor to the end of the current line |
Positioning by Line
Command | Function |
---|---|
j | Moves the cursor down one line from it’s present position, in the same column |
k | Moves the cursor up one line from it’s present position, in the same column |
+ | Moves the cursor down to the beginning of next line |
– | Moves the cursor upto to the beginning of previous line |
Enter | Moves the cursor down to the beginning of the next line |
Positioning by word
Command | Function |
---|---|
w | Moves the cursor to the right, to the first character of the next word |
b | Moves the cursor back to the first character of the previous word |
e | Moves the cursor to the end of the current word |
Positioning in the window
Command | Function |
---|---|
H | Moves the cursor to the first line on the screen, or “home” |
M | Moves the cursor to the middle line on the screen |
L | Moves the cursor to the last line on the screen |
Commands for Positioning in the file
Scrolling
Command | Function |
---|---|
Ctrl f | Scrolls the screen forward a full window, revealing the window of text below the current window |
Ctrl b | Scrolls the screen back a full window, revealing the window of text above the current window |
Positioning on a Numbered Line
Command | Function |
---|---|
G | Moves the cursor to the beginning of the last line in the file |
nG | Moves the cursor to the beginning of the nth line in the file |
Commands for Inserting Text
Commnad | Function |
---|---|
a | Enters text input mode and appends text after the cursor |
i | Enters text input mode and inserts text at the cursor |
A | Enters text input mode and appends text at the end of current line |
I | Enters text input mode and inserts text at the beginning of current line |
o | Enters text input mode by opening a new line immediately below the current line |
O | Enters text input mode by opening a new line immediately above the current line |
R | Enters text input mode and overwrites from current cursor position onwards |
Commands for Deleting text
Commnad | Function |
---|---|
^ | Deletes the character at current cursor position |
X | Deletes the character to the left of the cursor |
dw | Deletes a word (or part of a word) from the cursor to the next space or to the next punctuation |
dd | Deletes the current line |
nx, ndw, ndd | Deletes n characters, n words or n lines |
d0 | Deletes the current line form the cursor to the beginning of the line |
d$ | Deletes the current line from the cursor to the end of the line |
Miscellaneous Commands
Command | Function |
---|---|
Ctrl g | Gives the line number of current cursor position in the buffer and modification status of the file |
. | Repeats the action performed by the last command |
u | Undoes the effects of the last command |
U | Restores all changes to the current line since you moved the cursor to this line |
J | Joins the line immediately below the current line with the current line |
~ | Changes character at current cursor position form upper case to lower case or from lower case to upper case |
:sh | Temporarily returns to the shell to perform some shell commands. Type exit to return to vi |
Ctrl l | Clears and redraws the current window |
Commands for Quitting vi
Commnad | Function |
---|---|
ZZ | Writes the buffer to the file and quits vi |
:wq | Writes the buffer to the file and quits vi |
:w filename | writes the buffer to the filename (new) and quits vi |
:w! filename and :q | Overwrites the existing file filename with the contents of the buffer and quits vi |
:q! | Quits vi whether or not changes made to the buffer were written to a file. Does not incorporate changes made to the buffer since the last write (:w) command |
:q | Quits vi if changes made to the buffer were written to a file |
thanks yashwant kanetkar!