Linux: vi commands PDF Print E-mail
Friday, 07 September 2007 23:42
A list containing commands to use with the linux vi editor.

 

Starting

    * vi /filename/.....start vi, edit specified file
    * vi -R /filename/..start vi read-only, edit specified file
    * view /filename/...start vi read-only, edit specefied file

Stopping

    * :q!...............stop without saving data
    * zz................save data and stop
    * :wq...............save data and stop
    * :x................save data and stop

Recovering After System Failure

    * vi -r .............display names of files that can be recovered
    * vi -r /filename/...start vi, recover specified file

Controlling the Display

    * ^L................redisplay the current screen
    * :set number ......display internal line numbers
    * :set nonumber.....do not display internal line numbers

Moving the Cursor

    * h.................move cursor one position left
    * j.................move cursor one position down
    * k.................move cursor one position up
    * l.................move cursor one position right
    * LEFT..............move cursor one position left
    * DOWN..............move cursor one position down
    * UP................move cursor one position up
    * RIGHT.............move cursor one position right
    * BACKSPACE.........move cursor one position left
    * SPACE.............move cursor one position right
    * -.................move cursor to beginning of previous line
    * +.................move cursor to beginning of next line
    * RETURN............move cursor to beginning of next line
    * O.................move cursor to begining of current line
    * $.................move cursor to end of current line
    * ^.................move cursor to first non-space/tab in current line
    * w.................move cursor forward to first character of next word
    * e.................move cursor forward to last character of next word
    * b.................move cursor backward to first character of previous word
    * W.................same as w; ignore punctuation
    * E.................same as e; ignore punctuation
    * B.................same as b; ignore punctuation
    * ).................move forward to next sentence beginning
    * (.................move backward to previous sentence beginning
    * }.................move forward to next paragraph beginning
    * {.................move backward to previous paragraph beginning
    * H.................move cursor to top line
    * M.................move cursor to middle line
    * L.................move cursor to last line

Moving Through the Editing Buffer

    * ^F................move down(forward) one screenful
    * ^B................move up(backward) one screenful
    * n^F...............move down n screenfuls
    * n^B...............move up n screenfuls
    * ^D................move down a half screenful
    * ^U................move up a half a screenful
    * n^D...............move down n lines
    * n^U...............move up n lines

Searching for a Pattern

    * /rexp.............search forward for specified regular expression
    * /.................repeat forward search for previous pattern
    * ?rexp.............search backward for specified regular expression
    * ?.................repeat backward search for previous pattern
    * n.................repeat last / or ? command, same direction
    * N.................repeat last / or ? ? command, opposite direction

Special Characters to Use in Regular Expressions

    * /./...............match any single character except newline
    * *.................match zero or more of the preceding characters
    * ^.................match the beginning of a line
    * $.................match the end of a line
    * /<................match the beginning of a word
    * />................match the end of a word
    * []................match one of the enclosed characters
    * [^]...............match any character taht is not enclosed
    * \.................interpret the following symbol literally

Line Numbers

    * nG................jump to line number n
    * 1G................jump to first line in editing buffer
    * G.................jump to last line in editing buffer
    * :map g1G..........define macro so g will be the same as 1G

Inserting

    * i.................change to insert mode: insert before cursor position
    * a.................change to insert mode: insert after cursor position
    * I.................change to insert mode: insert at start of current line
    * A.................change to insert mode: insert at end of current line
    * o.................change to insert mode: open below current line
    * O.................change to insert mode: open above current line
    * ESCAPE............leave insert mode, change to command mode

Making Changes

    * r.................replace exactly 1 character (do not enter input mode)
    * R.................replace by typing over
    * s.................replace 1 character by insertion
    * C.................replace from cursor to end of line by insertion
    * cc................replace entire current line by insertion
    * S.................replace entire current line by insertion
    * cmove.............replace from cursor to move by insertion
    * ~.................change the case of a letter

Replacing a Pattern

    * :s/pattern/replace/.............substitute, current line
    * :lines/pattern/replace/ ........substitute, specified line
    * :line, lines/pattern/replace/..substitute, specified range
    * :%s/pattern/replace/...........substitute, all lines

Undoing or Repeating a Change

    * u.................undo last command that modified the editing buffer
    * U.................restore current line
    * /./...............repeat last command that modified the editing buffer

Controlling the Length of Lines

    * rReturn...........replace a character with a newline
    * J.................join lines
    * :set wm=n.........auto line break within n positions of right margin

Deleting

    * x.................delete character at cursor
    * X.................delete character to left of cursor
    * D.................delete from cursor to end of line
    * dd................delete entire current line
    * dmove .............delete from cursor to move
    * dG................delete from current line to end of editing buffer
    * d1G...............delete from current line to start of editing buffer
    * :/lined/..........delete specified line
    * :/line/, /lined/..delete specified range

Copying the Last Deletion

    * P.................copy buffer; insert after/below cursor
    * p.................copy buffer; insert before/above cursor
    * xp................transpose two characters
    * deep..............transpose two words(start to the left of first word)
    * ddp...............transpose two lines

Copying and Moving Lines

    * :linecotarget.........copy specified line; insert below target
    * :line, linecotarget...copy specified range; inster below target
    * :linemtarget..........move specified line; insert below target
    * :line,linemtarget move.specified range; insert below target

Executing Shell Commands

    * :!command...........pause vi, execute specified shell command
    * :!!.................pause vi, execute previous shell command
    * :sh.................pause vi, start a shell
    * :!csh ...............pause vi, start a new C-Shell

Reading Data

    * :liner file .........insert contents of file after specified line
    * :r file..............insert contents of file after current line
    * :liner !command......insert output of command after specified line
    * :r !command .........insert output of command after current line
    * :r !look pattern.....insert words that begin with specified pattern

Using Shell Commands to Process Data

    * n!!command...........execute command on n lines
    * !move command........execute command from cursor to move
    * !movefmt.............format lines from cursor to move

Writing Data

    * :w...................write data to original file
    * :w file..............write data to specified file
    * :w>> file............append data to specified file

Changing the File While Editing

    * :e file..............edit the specified file
    * :e! file.............edit the specified file, omit automatic check

Abbreviations

    * :ab short long....set short as an abbreviation for long
    * :ab...............display current abbreviations
    * :una short........cancel abbreviation short 

 

Add comment


Security code
Refresh

More KB Articles