VIM

Vi vs VIM

VIM - Vi IMproved

Starting up

vi may be linked to vim by default:

$ which vi
/usr/bin/vi

$ ls -l /usr/bin/vi
... /usr/bin/vi -> vim

Vim will load ~/.vimrc by default. To specify another file, use

$ vim -u <vimrc>

To use factory setting:

$ vim -u NONE

-N: Not fully Vi compatible, i.e. load vim features

equivalent to ​

set​ nocompatible

Commands

  • :: Use Command-Line mode to execute an Ex command
  • /: Use Command-Line mode to perform a forward search
  • ?: Use Command-Line mode to perform a backward search
  • =: Use Command-Line mode to evaluate a Vim script expression

Modes

  • Normal Mode

  • Insert Mode

  • Replace Mode

  • Visual Mode

  • <Esc>: Switch to Normal mode

  • <C-[>: Switch to Normal mode

  • <C-o>: Switch to Insert Normal mode

Getting Help

Built-in vim tutorial:

$ vimtutor

or

:h vimtutor
  • :help
  • :help vi-differences
  • :help uganda
  • :help 'guioptions'

External Links

http://vimgolf.com/

vim :retab

Show whitespace Characters

:set list
:set nolist

Toggle

:set list!

In Vim, 'list' is a boolean option that defaults to off. If 'list' is on, whitespace characters are made visible. The'listchars' option can be used to customize the way whitespace characters are shown. The default displays ^I for each tab, and $ at each EOL (end of line, so trailing whitespace can be seen). :help 'list'

The following example toggles list, then sets listchars to not display an end-of-line character, and to display > for the first character occupied by a tab, and - for any subsequent characters that the tab may occupy.

:set list!
:set listchars=tab:>-
Text object Change Delete Copy
One word cw dw yw
Two words, not counting punctuation 2cW or c2W 2dW or d2W 2yW or y2W
Three words back 3cb or c3b 3db or d3b 3yb or y3b
One line cc dd yy or Y
To end of line c$ or C d$ or D y$
To beginning of line c0 d0 y0
Single character r x or X yl or yh
Five characters 5s 5x 5yl
Movement Commands
←, ↓, ↑, → h, j, k, l
To first character of next line +
To first character of previous line -
To end of word e or E
Forward by word w or W
Backward by word b or B
To end of line $
To beginning of line 0
Operations Commands
Place text from buffer P or p
Start vi, open file if specified vi file
Save edits, quit file ZZ
No saving of edits, quit file :q!
Editing action Command
Insert text at current position i
Insert text at beginning of line I
Append text at current position a
Append text at beginning of line A
Open new line below cursor for new text o
Open new line above cursor for new text O
Delete line and substitute text S
Overstrike existing characters with new text R
Join current and next line J
Toggle case ~
Repeat last action .
Undo last change u
Restore line to original state U
Movement Command
Scroll forward one screen ^F
Scroll backward one screen ^B
Scroll forward half screen ^D
Scroll backward half screen ^U
Scroll forward one line ^E
Scroll backward one line ^Y
Move current line to top of screen and scroll z ENTER
Move current line to center of screen and scroll z.
Move current line to bottom of screen and scroll z-
Redraw the screen ^L
Move to home—the top line of screen H
Move to middle line of screen M
Move to bottom line of screen L
Move to first character of next line ENTER
Move to first character of next line +
Move to first character of previous line -
Move to first nonblank character of current line ^
Move to end of word e
Move to end of word (ignore punctuation) E
Move to beginning of current sentence (
Move to beginning of next sentence )
Move to beginning of current paragraph {
Move to beginning of next paragraph }
Move to beginning of current section [[
Move to beginning of next section ]]
Search forward for pattern / pattern
Search backward for pattern ? pattern
Repeat last search n
Repeat last search in opposite direction N
Repeat last search forward /
Repeat last search backward ?
Move to next occurrence of x in current line f x
Move to previous occurrence of x in current line F x
Move to just before next occurrence of x in current line t x
Move to just after previous occurrence of x in current line T x
Repeat previous find command in same direction ;
Repeat previous find command in opposite direction ,
Go to given line n n G
Go to end of file G
Return to previous mark or context
Return to beginning of line containing previous mark ''
Show current line (not a movement command) ^G

in vim/vi, when advancing through a line of existing text, say to fix a typo, don't use space or arrow keys. Instead, use W or B, to advance or back up a "big" word at a time. Better still, use 'f' or 'F' to search forward or backwards for target character. Or best (sometimes), use fwd/reverse incremental search. For when you do use repeated cursor motion, ensure that key-repeat kicks in sooner and repeats faster.

" Highlight "bad" spaces
let c_space_errors=1

" Also highlight empty lines at EOF.
match ErrorMsg /\s\+$\| \+\ze\t/

" Enable incremental search.
set incsearch