Useful simple vi commands for DBAs COMMAND EFFECT :.= find out the current line number :1 go to line 1 Ctrl-d page down Ctrl-u page up Shift-g go to end of file i insert text at current position Shift-a append text after end of current line Shift-I insert text before start of current line Esc get out of edit mode, back into normal vi command mode dd delete current line 10dd delete 10 lines from current line on down d shift-g delete all lines from current line and below d 1 shift-g delete all lines from current line and above . repeat previous command shift-y yank (copy) current line p paste that copy into line below /data search forward for occurencies of string "data" / search forward for next occurrence of remembered search string ? search backward for next occurrence of remembered search string :set ic make searches case insensitive :1,$s/data/index/g replace all occurrencies of "data" with "index" :1,$s/"//g remove all " characters :1,$s/$/ ;/ append " ;" to the end of every line :1,$s/^/rem / insert "rem " to the start of every line :w write (save) file :q quit out of vi :q! quit out of vi without saving changes :wq write (save) file and quit out of vi shift-z shift-z same as above ":wq" except does not write (change file modification times) if you have not made any changes. :n next file (when vi'ing a series of files, e.g. with using "vi *" at the command prompt) u undo last command shift-j Join next line onto end of current line VI Quick Reference Guide ------------------------ GETTING STARTED vi filename - normal mode vedit filename - user friendly edit esc - return to command mode INSERTING SAVING AND QUITING o - insert a line after cursor O - insert a line before cursor ZZ - save and exit i - insert text at cursor :w - save without exiting a - insert text after cursor :w! - save write protected file I - insert text at start of line :q - quit editor A or $a - insert text at eol :q! - quit without saving CURSOR MOVEMENT DELETING k dd - delete current line ^ x - delete char at cursor h < > l X - delete char before cursor v dw - delete word j D or d$ - delete from cursor to eol REPLACING / CHANGING R - replace till eol r - replace one character C - change all text after cursor cw - change a word MOVING ABOUT c$ - change from cursor to EOL cG - change from cursor to EOF f - page forward b - page back d - 1/2 page forward MOVING TEXT u - 1/2 page back w - next word To move n lines of text from A to B b - previous word 1. move cursor to A $ - end of line 2. ndd - delete n lines ^ - start of line (shift 6) 3. move cursor to B G - go to end of file 4. P - pull text in after cursor 1G - go to beginning of file p - pull text in before cursor nG - go to nth line g - show position in file (,( next, previous sentence ^D - Goes half page down ]],[[ next, previous section ^U - Goes half page up },{ next, previous paragraph ^F - Goes one page DOWN % goto matching parenthesis () {} [] ^B - Goes one page UP SEARCHING COPING TEXT /abc - search for string "abc" To copy n lines of text from A to B n - find next occurrence 1. move cursor to A N - find previous occurrence 2. nyy - yank n lines 3. move cursor to B 4. P - pull text in after cursor p - pull text in before cursor YANK OPERATOR TO BUFFER yw - yank word y$ - yank from cursor to EOL yG - yank from cursor to EOF yy - yank complete current line p - paste from buffer after cursor P - paste from buffer before cursor USEFUL COMMANDS . - repeat last command u - undo last command U - undo changes made to current line J - join the next line to the current line ^G - Show the current filename and status ! - Ejecuta uan linea del shell :1,$s/old/new/g - global search and replace old with new * :.,$d - delete from current line to eof :r filename - read filename into this vi buffer :n,mw filename - write lines n through m to filename * special characters prefix with \ (backslash) Examples: --------- The following example performs a global search with replace. We want to search for "feo" and change it for "lindo": :g/feo/s//lindo/g If you want to use special characters you need to use \ (backslash). For example: to replace all the $ with **: :g/\$/s//**/g :1,5de Delete from line 1 to 5. :1,5co20 Copy from line 20 5 lines :1,5mo20 Same as before but cutting those lines :g/XXX/s//YYY/g Changes on the full text XXX with YYY. :s/old/new replace next occurence of old with new :s/old/new/g replace all occurences on the line :x,ys/old/new/g replace all ocurrences from line x to y :%s/old/new/g replace all occurrences in file :%s/old/new/gc same as above, with confirmation