Tech
Infected

Linux, OpenSource, Programming And Hacks

Jul 26, 2014

Vim Commands Cheat Sheet

Jul 26, 2014


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.

Note: You must be in command mode to use the following command





Commands for Positioning Cursor in the Window

Position by Character

CommandFunction
hMoves the cursor one character to the left
BackspaceMoves the cursor one character to the left
lMoves the cursor one character to the right
Space barMoves the cursor one character to the right
0Moves the cursor to the beginning of the current line
$Moves the cursor to the end of the current line

Positioning by Line


CommandFunction
jMoves the cursor down one line from it's present position, in the same column 
kMoves 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 
EnterMoves the cursor down to the beginning of the next line

Positioning by word

CommandFunction
wMoves the cursor to the right, to the first character of the next word
bMoves the cursor back to the first character of the previous word 
eMoves the cursor to the end of the current word

Positioning in the window


CommandFunction
HMoves the cursor to the first line on the screen, or "home"
MMoves the cursor to the middle line on the screen
LMoves the cursor to the last line on the screen

Commands for Positioning in the file

Scrolling

 CommandFunction
 Ctrl fScrolls 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
 CommandFunction
 GMoves the cursor to the beginning of the last line in the file
 nGMoves the cursor to the beginning of the nth line in the file

Commands for Inserting Text



CommnadFunction
aEnters text input mode and appends text after the cursor
iEnters text input mode and inserts text at the cursor
AEnters text input mode and appends text at the end of current line
IEnters text input mode and inserts text at the beginning of current line
oEnters text input mode by opening a new line immediately below the current line
OEnters text input mode by opening a new line immediately above the current line
REnters text input mode and overwrites from current cursor position onwards

Commands for Deleting text

CommnadFunction
^Deletes the character at current cursor position
XDeletes the character to the left of the cursor
dwDeletes a word (or part of a word) from the cursor to the next space or to the next punctuation
ddDeletes the current line
nx, ndw, ndd    Deletes n characters, n words or n lines 
d0Deletes 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

CommandFunction
Ctrl gGives the line number of current cursor position in the buffer and modification status of the file
.Repeats the action performed by the last command
uUndoes the effects of the last command
URestores all changes to the current line since you moved the cursor to this line
JJoins 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
:shTemporarily returns to the shell to perform some shell commands. Type exit to return to vi
Ctrl lClears and redraws the current window

Commands for Quitting vi


CommnadFunction
ZZWrites the buffer to the file and quits vi
:wqWrites the buffer to the file and quits vi
:w filenamewrites 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  
:qQuits vi if changes made to the buffer were written to a file 

                                                                                                                              thanks yashwant kanetkar!


No comments:

Post a Comment