Vi (VIM) - Text Editor Commands (VI Cheat Sheet)(UNIX)

 

Visual Mode

Insert Mode

Editing in Insert Mode

Command Mode

Shell

 

 

 

 

 

Settings

Windowing

File Explorer

Undo / Redo

Tips / Tricks

 

 

 

 

 

Search

Indent Lines

Other

 

 

 

Visual / Highlight Mode

  • Cut = c (c$ to end of line)
  • Copy = y (y$ to end of line)
  • Paste = p (P = before cursor)
      Note: if, when pasting, your text is pasted as comments, you can: before you paste: :set paste, type :set nopaste to exit paste mode.
  • Delete = d (d$ to end of line)
  • V = visual Line mode
  • Ctrl-V = visual Block mode

 

Indent block of Lines

  • Put the cursor anywhere in the first line
  • Press V then jj to visually select the lines you want to indent
  • Press > to Indent, or press < to Shift Left
  • Press . to repeat the indent or u to undo if you shifted too far
  • or if you know the number of lines (n) you want to indent, Type n>> (Indent) or n<< (outdent). ex. 3>> indents 3 lines starting with the line your cursor is on.

Insert Mode

  • a = Append, moves cursor 1 position right before inserting (A = appends at end of line)
  • o = Inserts a blank line under (O = above) current cursor position

 

Editing as you Type in Insert Mode

  • Back Space / Erase = Ctrl-H
  • Line Kill = Ctrl-U
  • Word Kill = Ctrl-W

 

Command Mode

  • Editing Commands:
    • Delete a Character = x
    • Delete a Word = dw
    • Delete 3 Words = 3dw
    • Delete a Line = dd
    • Delete 3 Lines = 3dd
    • Delete Contents of the line after cursor = D
    • Undo a command = u
    • Change Case of a character = ~
    • Replace a character = r
    • Overwrite characters from cursor onward = R
  • Navigation:
    • Move to Beginning of File (BOF) = 1G
    • Move to End of File (EOF) = G
    • Move to top of screen = H
    • Move to middle of screen = M
    • Move to lower portion of screen = L
    • Move forward a page = Ctrl-F
    • Move backward a page = Ctrl-B
    • Move to Beg of Line = 0 (zero)
    • Move to End of Line = $
    • Move to First Line of the file = 1G
    • Move to Last Line of the File = G
    • Move to Line 10 = :10
    • Scroll Down Half a Window = Ctrl-D
    • Scroll Up Half a Window = Ctrl-U
  • Save certain lines = :1,10w stuff.txt
  • Status line = :f or Ctrl-G
  • Read a file = :r stuff.txt
  • Execute command = :! ls
  • Redraw Screen = Ctrl-L
  • Get Version of VI = :ver

 

Command Mode: Undo / Redo

  • Undo = :und, u
  • Redo = :red, Ctrl-R

 

Shell

  • Enter Shell = :sh

 

Settings

  • Show Line Numbers = :set number
  • No Line Numbers = :set nonumber
  • Show Current Line Number = :.=
  • Show Number of Lines = :=
  • Show Status Line = :set laststatus=2
  • Stop Status Line = :set laststatus=0
  • Show Special Characters = :set list
  • Stop Special Charesters = :set nolist
  • Change all Tabs to Spaces
    • :set tabstop=5
    • :set expandtab
    • :retab

 

Windowing

  • Split Window = :sp
    • Quick Jump between windows = Ctrl-W
    • Move up a window = Ctrl-W j
    • Move down a window = Ctrl-W

 

File Explorer

  • File Explorer = :Ex
  • File Explorer in a Split Window = :Sex

 

Search / Replace (Regular Expressions)

  • For Visual Highlight Search / Replace = highlight, press :, search and replace: /s/smith/jones/g
  • Ignore Case when Searching = :set ic
  • Stop Ignoring Case = :set noic
  • Highlight Search = :set hlsearch or :set hls
  • Stop Highlight Search = :set hls!
  • Incremental Searches = :set incsearc

 

  • Search Forward for a String:
    • /string
  • Search Backward for a String:
    • ?string
  • Search for Lines Containing 'editer' and substitute 'editor' globally:
    • :%s/editer/editor/g
  • Delete all Blank (empty) Lines globally:
    • :%g/^$/d
  • Delete all occurrences of the word 'the' globally:
    • :%s/the//g
  • Delete the ^M characters at the end of all lines globally:
    • :%s/Ctrl-VCtrl-M//g (displays: :%s/^M//g)

 

Other

  • Edit 2 Files :
    • vi <file1>
      • :e <file2>
      • :e# - Switches between files

 

 


by Marcus Jeffers, 2008