Vi

From Christoph's Personal Wiki
Revision as of 01:02, 23 May 2006 by Christoph (Talk | contribs) (+links/Cat)

Jump to: navigation, search
The correct title of this article is vi. The initial letter is capitalized due to technical restrictions.

vi is a screen-oriented text editor computer program run from the command line.

Configuring vi/vim

It is possible to extensively configure vi (or vim) to suit your personal needs.

For an example, if you would like syntax-highlighting to be used by default for most of your code files, edit your .vimrc file (located in your "home" directory. If one does not exist, create it) and add the following line:

syn on

Commands

Below is a very short list of the most useful commands:

Command mode
ESC
Movement command
h, j, k, l left, down, up, right
w, W, b, B forward, backward by word
H top of the screen
M middle of the screen
L last line of the screen
Ctrl-F forward one screen
Ctrl-B backward one screen
Ctrl-D forward half screen
Ctrl-U backward half screen
0 (zero), $ start, end of current line
Inserting text
a append after cursor
i insert before cursor
A append to end of line
I insert at start of line
o open a line below current line
O open a line above current line
r replace char
Delete text
x current character
dh previous character
dw current word
db previous word
dd entire line
d$ to end of line
d0 (zero) to start of line
ndd next n lines
Undelete
p insert after cursor
P insert before cursor
Goto line
:linenumber  
nG Goto line n
:7 Goto line 7
Save and exit
ZZ write if changes and quit
:wq write and quit
:w filename save to new file
:q! quit vi
Search
/pattern <RETURN> forward for a pattern
?pattern <RETURN> backward for a pattern
n repeat previous search
N repeat previous search in reverse direction
Search and replace
Example:
  • Search from current line and replace first occurance
    :s/search_string/replace_string/
  • Search from current line and replace all matches
    :s/search_string/replace_string/g
  • Search from every line, replace confirmation (with [y]es)
    :%s/search_string/replace_string/gc
    :1,$s/search_string/replace_string/gc
  • Search lines from 10 to 20
    :10,20s/search_string/replace_string/g
Undo
u the latest change
U all changes on a line
Concatenate
J concatenate two lines