Vi

From Christoph's Personal Wiki
Revision as of 04:10, 12 January 2007 by Christoph (Talk | contribs) (External links)

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.

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

External commands

Ctrl-Z   # pause
fg       # resume

Bang!

  • find out how many words are in the currently opened file (from the last save):
:! wc %
  • check the PHP syntax of the currently opened (php) file:
:! php5 -l %

Reading command output

  • to include a list of files from a specific directory, try this read command:
:r ! ls -1 /home/user/path/etc
  • grab a page, using lynx, and dump it right into your editing session without leaving vim:
:r ! lynx http://en.wikipedia.org/wiki/Vi -dump 
  • etc:
:r ! ls -1 /home/user/directory | sort -r
:r ! grep string /var/log/apache2/site-error.log
:set shell ?   # check the current shell

Appending data

  • append current line (under cursor) to external file:
:.w! >> /for/bar

Switching cases

In the replacement part of a substitution command, i.e. between the second "/" and third "/",

\u means make the following character upper case
\l means make the following character lower case
\U means make the rest of the replacement upper case
\L means make the rest of the replacement lower case
  • Make the first letter of every word from line 18 to 43 uppercase:
:18,43s/\<./\u&/g
  • Change "uPPeR" and "LoweR" in any mixture of cases to lowercase:
:s/[UuLl][PpOo][PpWw][Ee][Rr]/\L&/
  • Make the whole file uppercase:
:%s/.*/\U&/
  • Make the region from line m to line n all uppercase:
:'m,'ns/.*/\U&/
  • Make a paragraph all lowercase:
:?^$?,/^$/s/.*/\L&/
  • Make the first letter of every word in a paragraph uppercase:
:?^$?,/^$/s/\([^ ][^ ]*\)/\u&/g
  • Make the second word of each line uppercase:
:1,$s/^\([^ ]*\) \([^ ]*\) \(.*\)/\1 \U\2\e \3/

Abbreviations

If you are like me and use vi for everything, you will find that there are certain words, phrases, bits of code, etc. that you are constantly using. These can all be stored as abbreviations.

The syntax for storing an abbreviation is as follows:

esc:   # to go into command mode
ab abbr phrase

For an example, say you were editing xhtml, and you wanted 'xzml' to enter standard xml header tags of <?xml version="1.0" encoding="UTF-8" ?>, you could enter:

:ab xzml <?xml version="1.0" encoding="UTF-8" ?>

Then, any time you enter 'xzml' as a word, vi automatically replaces 'xzml' with your standard table tags. The idea is to choose an abbreviation that has no chance of being a real "word", but is short and easy to remember.

Note that the above abbreviation is stored for your current session only. If you wish for it to be universally applicable and available, put the abbreviation command line in your .vimrc file.

To list all of your abbreviations, enter

:ab

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

External links