Monday, April 6, 2009

vi/vim quick reference

Movement Commands:

h move one character left
j move one line down
k move one line up
l move one character right
H move to Home (start of first line displayed
M move to start of Middle line displayed
L move to start of Last line displayed
O or ^ move to start of current line
$ move to end of current line
tc move forwards to just before character c
Tc move backwards to just after character c
fc move forwards on to character c
Fc move backwards on to character c
; repeat last t, T, f, or F command
w move forwards to the start of the next word
e move forwards to the end of the next word
b move backwards a word
% move to matching bracket: () [] {}

Movement Searching:

/ Search forwards for regular expression
? Search backwards for regular expression
n repeat last search in the same direction
N repeat last search in the reverse direction

Insert Mode

i Insert before the current character
a Insert after the current character
o Open a new line after the current line
O Open a new line before the current line
I Insert before the first non white character on the current line
A Insert after the last non white character on the current line

Deleting text:

x Delete the character under the cursor
dM Delete to where the movement M would take the cursor
dd Delete the current line
D Delete from the current position to the end of line

Changing text:

rc Replace the current character with c
s Substitute the current character: enter insert mode
cM Change to where the movement M would take the cursor and enter insert mode
cc Change the entire current line and enter insert mode
C Change from the current position to the end of last line
R Replace text, end with Escape

Yank buffers store text:

yM Copy text into yank buffer to where the movement M would take the cursor
yy Copy the current line into the yank buffer
Y Copy from the current position to the end of line into the yank buffer
p Paste the text into the yank buffer after the current line
P Paste the text into the yank buffer before the current line

Colon commands are based upon ed(l) commands. In many of these % will be substituted with the name of the current file, and # with the name of the previous file. Some commands may be prefixed with line number ranges. . means the current line, S means the last line in the file: 2,5 s/fred/joe/ changes the first fred to joe on lines 2,3,4,5; l,$ s/fred/joe/g changes every fred to joe in the buffer. % may be used as a number range and is short for l,$. Number ranges may also be: .+N (N lines after current line), $—N(N lines before end of bufer), fred/+n (N lines after next fred). Tf you only want one line, omit the comma and second number: .-4 s/fred/joe.

:q Quit
:q! Quit without the sanity checks (unsaved buffers, etc)
:w [f] Write the buffer to file f
:w !cmd Write the buffer as stdin to shell command cmd
:wq Write and quit
:x Like :wq but only write if changes have been made
:r file Read file into the current buffer after current line
:e f Edit file f
:e # Edit the previous file, often bound to C-
:n Edit next file
:p Edit previous file
:rew Rewind to first file
:! cmd Execute shell command cmd
:s7a/b/ Substitute a with b on the current line.
:s/a/b/g Substitute every a with b on the current line. (g means global)
:s/a/b/gc Substitute every a with b on the current line, ask for confirmation
:d Delete the current line

Display (screen) control:

C—e scroll line with cursor up a line
C—y scroll line with cursor down a line
C—d scroll display down l/2 a screen
C—u scroll display up l/2 a screen
C—f scroll display forward a screen
C—b scroll display backward a screen
C—l redisplay the screen

Misc:

J Join the next line to the current line
u undo the last change. Repeat for previous change.
C—r Redo a change undone by u .
U Undo all the changes made to the current line
. Repeat the last change command
~ Invert case (upper/lower) of the current character
!!cmd Replace the current line with the otput of shell command cmd
C—g Display current line number
G Move to the last line in the buffer
nG Move to line number n in the buffer
ml Mark the current line with the label
'l Go to the line with label l
''Go to the last line that you jumped from

Labels may be used in : commands:

's,'e:s/fred/joe/g changes all fred to joe for all lines between those labeled s and e
nC Repeats the command C n times
qr Start recording into register r. If r is upper case, append
q Stop recording
@r Execute keystrokes in register r
<<
Move text left by an indent
>> Move text right by an indent
C—z Suspend (shell job control)

Visual (block) mode

v Start character visual mode
V Start line visual mode
c·V Start block visual mode

Once a visual area is marked

b delete
c change
y yank
~ swap case (upper/lower>
u make lower case
U make upper case
! filter through external program
:dis Display the contents of all yank buffers

Split windows and multiple buffers:

C—ws Split the window in two
C—wc Close the current window
C-wM Move to window in direction M, e.g. k means up
NC—w+ Increase display size of current window by N lines
NC—w— Decrease display size of current window by N lines
:files Display files currently being edited and buffer numbers
:N b Switch to buffer N in the current window

Useful options (put in ~/.exrc):

set ic Ignore case in searches
set number Line number node
set showmode Display INSERT on bottom line in insert
mode

Handy sequences

xp swaps two adjacent characters
lOdd Deletes lO lines to the buffer
:.,$d Deletes from the current line to the end of the buffer
:. w ! od —c Filter the current line through od to see if it contains any strange characters
:. w ! sh Execute the current line as a shell command
Redhat

0 comments:

Post a Comment

Comments are moderated. No spam please.