vi commands

Hi, My name is Jimmy Sam. If you need DBA help, please email to for further discussion. Thanks.

vi basic commands

takes out extra ^M

:%s/[ctrlkey+v and ctrl-key+M]//g

With Vim on Windows use Ctrl+Q in place of Ctrl+V
to get the ^M, enter the key combination ctl-V
ctrl-m ctrl-v
ctrl-v ctrl-m

set ic          --> ignore case when search, or use /mysearch\c /\cmysearch
set noic        --> undo set ic,             or use /mysearch\C /\Cmysearch 
set nu          --> show line numner
set nonu        --> not show line numner

:set list        --> Show Hidden Characters  :set nolist go back

split screen

vi -help
vi -o2 file1.txt file2.txt  # -o 2 split into 2 windows horizontally

:split  or ctrl-w J --> split screen
ctrl-w ctrl-w
ctrl-w ctrl-r: rotate windows

:o my_filename.txt --> edit multiple files
:n --> go to next file

C-w L --> split horizontally 


enable vi status line

vi status line:

ESC :set laststatus=2

ESC :help 'laststatus'

:set ruler
:help ruler

:set rulerformat=%l,%v

:syntax on
:syntax off

vi filename.txt

ESC: command mode, h l j k
i,a: insert mode

ESC : w --> save a file
ESC : q --> exit
ESC : q! --> exit without save

ESC : wq --> save and exist
ZZ   --> same as ESC : wq

ESC : undo --> undo last command
u          --> continuous undo

ESC : set nu --> show line number
ESC : set nonu

ESC U -->  undo current line

ESC dd --> delete current line
ESC x,X  -->delete a character

ESC r,R --> replace

ESC % --> match (  ) or { }

ESC ~ ---> switch uppercase(A) lower case(a)

ESC xp --> change two characters, ab xp ba

ESC /pattern --> find a string, n --> find next one  N --> previous
ESC ?pattern --> find a string(reverse), n --> find next one  N --> previous

ESC :%s/s1/s2/g 
ESC :234,236s/s1/s2/g 

ESC :89 --> goto line 89
nG  --> goto line n
1G  --> beginning of the file
G   --> end of the file
CTRL + g ---> show status

ESC :set nu    --> set line number

-- --------------------
h --> left
l --> right
j --> down
k --> up
$ --> end of line
^ --> begin of line
0 --> begin of line

shift+j or J : merge two lines ( in command mode )

h              move cursor one character to left
j              move cursor one line down
k              move cursor one line up
l              move cursor one character to right
w              move cursor one word to right
b              move cursor one word to left
0              move cursor to beginning of line
$              move cursor to end of line
nG             move cursor to line n
control-f      scroll forward one screen
control-b      scroll backward one screen
i              insert to left of current cursor position (end with ESC)
a              append to right of current cursor position (end with ESC)
dw             delete current word (end with ESC)
cw             change current word (end with ESC)
r              change current character
~              change case (upper-, lower-) of current character
dd             delete current line
C  or c$       change to the end of current line from current cursor
D  or d$       delete portion of current line to right of the cursor
x              delete current character
ma             mark currrent position
d`a            delete everything from the marked position to here
`a             go back to the marked position
p              dump out at current place your last deletion (``paste'')
u              undo the last command 
.              repeat the last command 
J              combine (``join'') next line with this one
:w             write file to disk, stay in vi
:q!            quit VI, do not write file to disk,
ZZ             write file to disk, quit vi

:r filename    read in a copy of the specified file to the current
/string        search forward for string (end with Enter)
?string        search backward for string (end with Enter)
n              repeat the last search (``next search'')
:s/s1/s2       replace (``substitute'') (the first) s1 in this line by s2
:lr/s/s1/s2/g  replace all instances of s1 in the line range lr by s2
               (lr is of form `a,b', where a and b are either explicit
               line numbers, or . (current line) or $ (last line)
:map k s       map the key k to a string of vi commands s (see below)
:unmap k
:abb s1 s2     expand the string s1 in append/insert mode to a string 
               s2 (see below)
%              go to the "mate," if one exists, of this parenthesis
               or brace or bracket (very useful for programmers!)

cc: Change the whole line
C: Change to the end of the line

dG: delete from the current line to the end of file
d<C-End>: d CTRL-End  delete fro the cursor to the end of the file

mc	Set marker c on this line
`c	Go to beginning of marker c line.
'c	Go to first non-blank character of marker c line.

/string	Search forward for string
?string	Search back for string
n	Search for next instance of string
N	Search for previous instance of string

:.	Range - Current line
:$	Range - Last line

:syntax on 
:syntax off 
vi ref