Home > scripts > So you want to learn VI?

So you want to learn VI?

I like vi.  Yes I do.  Not Emacs.  But VI.  Here is a list of some basic commands to move around and some serious stuff.

h l k j character left, right, line up, down
b w word/token left, right
ge e end of word/token left, right
{ } beginning of previous, next paragraph
( ) beginning of previous, next sentence
0 gm beginning, middle of line
^ $ first, last character of line
nG ngg line n, default the last, first
n% percentage n of the file (n must be provided)
n column n of current line
% match of next brace, bracket, comment, #define
nH nL line n from start, bottom of window
:g/RE/p Where grep came from (RE being Regular Expression)
:10,20d Delete lines 10 to 20 inclusive
:g/pattern/d Delete lines that contain pattern
:g/^$/d Delete all empty lines:
:20,30/pattern/d Delete lines in range that contain pattern
:%s/pattern/new/ Substitute all lines for first occurance of pattern
:%s/pattern/new/g Substitute all lines for pattern globally (more than once on the line)
:%s/\(.*pattern.*\)/\1-new/g Find all lines containing pattern and then append -new to the end of each line
:20,30s/pattern/new/g Substitute range
:s/\(pattern1\)\(pattern2\)/\2\1/ Swap two patterns on a line
:s/\([a-z]\)/\u\1/ Capitalize the first lowercase character on a line
:s/[a-z]/\u&/ more concisely
:s/\([a-z]\)/\u\1/g Capitalize all lowercase characters on a line
:s/[a-z]/\u&/g more concisely
:s/\(.*\)/\U\1\E/ Capitalize all characters on a line:
:s/\<[a-z]/\u&/g Capitalize the first character of all words on a line:
:s/\<[A-Z]/\l&/g Uncapitalize the first character of all words on a line
~ Change case of character under cursor
g~~ Change case of all characters on line
g~w Change case of remaining word from cursor
:set nu Turn on line numbering
:set nonu Turn it off
:%!cat -n Number lines (filter the file through a unix command and replace with output)
:%!sort Sort lines
:%!sort -u Sort and uniq
:r !ls -l Read output of command into buffer
:e! Refresh file from version on disk
n Open a new window
s Open a new window with the same file (split)
v Split window vertically
c Close current window
:set textwidth=80 Set textwidth for automatic line-wrapping as you type
:syn on Turn on syntax highlighting
:set filetype=php Force the php for syntax highlighting:
:set background=dark Use lighter coloring scheme for a dark background
:set filetype=php Force the php for syntax highlighting:

Here are some external VI references:

Categories: scripts Tags:
  1. No comments yet.
  1. No trackbacks yet.