I have an alias which points "vi" to "vim -p". The "-p" option turns on tabbing. Buffers and Tabs are the same thing in VIM: both allow you to have multiple files open in one VIM session. The only difference is how they are displayed on the screen and the short cuts used to move between files. Because tabs in VIM closely resemble tabs in Firefox, I tend to use tabs.

Also: I have uploaded my vimrc configuration file.



Cursor movement

hmove left
jmove down
kmove up
lmove right
wjump by start of words (punctuation considered words)
Wjump by words (spaces separate words)
ejump to end of current word (punctuation considered words)
Ejump to end of current word (no punctuation)
bjump backward by words (punctuation considered words)
Bjump backward by words (no punctuation)
(jump to start of current sentence.
)jump to start of next sentence.
0(zero) start of line
^first non-blank character of line
$end of line
(number)GGo to line (number). (Leave off number for last line of file.)

Note: Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.

Starting Insert Mode

istart insert mode at cursor
Iinsert at the beginning of the line
aappend after the cursor
Aappend at the end of the line
oopen (append) blank line below current line (no need to press return)
Oopen blank line above current line
eaappend at end of word
Escexit insert mode

Editing

rreplace a single character (does not use insert mode)
Jjoin line below to the current one
ccchange (replace) an entire line
cwchange (replace) to the end of word
c$change (replace) to the end of line
sdelete character at cursor and subsitute text
Sdelete line at cursor and substitute text (same as cc)
xptranspose two letters (delete and paste, technically)
uUndo
ctrl+rRedo
.repeat last command

Inserting External Text

r filenameInsert filename into the current buffer.
r !commandExecute command and insert the output into the current buffer.

Visual commands

vstart visual mode, mark lines, then do command (such as y-yank)
Vstart Linewise visual mode
>shift right
<shift left
yyank (copy) marked text
ddelete marked text
uChange selected text to lowercase
UChange selected text to lowercase
~switch case for selected text
omove to other end of marked area
Ctrl+vstart visual block mode
Omove to Other corner of block
awselect a word
abselect a () block (with braces)
aBselect a {} block (with brackets)
ibselect inner () block
iBselect inner {} block
Escexit visual mode

Cut and Paste

yyyank (copy) a line
2yyyank 2 lines
ywyank word
y$yank to end of line
pput (paste) the clipboard after cursor
Pput (paste) before cursor
dddelete (cut) a line
dwdelete (cut) the current word
d(delete (cut) beginning of sentence to current cursor position.
d)delete (cut) current cursor position to end of sentence.
xdelete (cut) current character

Saving and Exiting

:e filenameEdit a file in current buffer
:e directoryOpen directory in interactive window to select file
:e .Open current directory in interactive window to select file
:wWrite (save) the file, but don't exit
:w!Write to a document that was opened in read-only mode
:wq (or ZZ)Write (save) and quit
:qQuit. Only works if document has not changed.
:q!Quit and throw away changes

Ranges

A range is something that denotes lines of text in a document. A "#" (pound sign) is used below to denote where you should type a number instead.

%Entire document
n,mLines n to m
1,$Also means entire document
.The current line of the cursor only
1,.The start of the document to the current line.
.,$The current line to the end of the document
+#The nth line after the current line of the cursor
-#The nth line before the current line of the cursor
.,+3The current line plus the next three lines (4 lines total)
g/regex/Each line containing this regular expression
g/regex/+#The nth line after each line containing this regular expression

Marking Text

m(label)Marks current cursor position
'(label)Reference label (To be used in some command context.)
g'(label)Move cursor to label
d'(label)Delete from current line to label
y'(label)Yank from cursor to label position
p'(label)Paste the yanked text after the label position

Search/Replace

/patternSearch for pattern
?patternSearch backward for pattern
f(character)Search forward for character on current line. (Great for long lines.)
F(character)Search backward for character. (Great for long lines.)
nRepeat search in same direction
NRepeat search in opposite direction
:%s/old/new/gReplace all old with new throughout file (% can be replaced with any range)
:%s/old/new/gcReplace all old with new throughout file with confirmations (% can be replaced with any range)

Buffers

:bnext (or :bn)Go to next buffer
:bprev (or :bp)Go to previous buffer
:bdDelete a buffer (close a file)
:bufdoPrefix this to any VIM command to run in all buffers

Windows

:sp filenameOpen a file in a new buffer and split window
ctrl+w sSplit windows
ctrl+w wswitch between windows
ctrl+w qQuit a window
ctrl+w vSplit windows vertically

Tabs

:tabn (or :tn or gt)Go to next tab
:tabp (or :tp or gT)Go to previous tab
:tabfirst (or :tabfir)Go to first tab
:tablastGo to last tab
:tabedit (or :tabe)Open a new tab
:tabedit filename (or :tabe filename)Open a new tab with filename
:tabdoPrefix this to any VIM command to run in all tabs

Folds

To use folds as soon as the program starts, you need to set up your "vimrc" file correctly with "set foldenabled".

set foldenable " Turn on folding
set foldmethod=indent " Use line indention for folding
set foldmethod=marker " Use markers for folding
set foldmarker=nnn,mmm " Set marker to preference. nnn is the beginning marker and mmm is the ending marker
set foldlevel=100 " Don't autofold anything (but I can still fold manually)
set foldopen-=search " don't open folds when you search into them
set foldopen-=undo " don't open folds when you undo stuff

By default, the fold markers are "{{{" for beginning and "}}}" for end. I thought folds would improve my programming abilities, but the more I used them, the more I felt they got in the way.

:%fold (or :%fo)Fold entire document. The % can be replaced with any range.
zf#jCreates a fold from the cursor down # lines.
zf/stringString creates a fold from the cursor to string.
zjMoves the cursor to the next fold.
zkMoves the cursor to the previous fold.
zoOpens a fold at the cursor.
zOOpens all folds at the cursor.
zmIncreases the foldlevel by one.
zMCloses all open folds.
zrDecreases the foldlevel by one.
zRDecreases the foldlevel to zero -- all folds will be open.
zdDeletes the fold at the cursor.
zEDeletes all folds.
[zMove to start of open fold.
]zMove to end of open fold.

Spell Check

To use the spell checker, you need to set up your "vimrc" file correctly with "set spell". I typically use just "z=" and don't mess with the other options.

z=Suggest a new spelling for a misspelled word.
]sMove to next misspelled word.
s[Move to previous misspelled word.
zgPermanently add word to word list.
zGTemporarily add word to word list. Word is forgotten when vim closes.
zwPermanently mark word as bad.
zWTemporarily make word as bad.
zugUndo zg: Remove word from permanent word list.
zuGUndo zG: Remove word from temporary word list.
zuwUndo zw: Remove bad word marking from permanent list.
zuWUndo zW: Remove bad word marking from temporary list.


Page Information

  • 1 week ago [history]
  • View page source
  • You're not logged in
  • Tags: vi shortcuts

Wiki Information

Recent PBwiki Blog Posts