vi Editor Commands Cheat Sheet

Command mode versus Input mode

Vi starts in command mode bydefault.
To switch vi editor to input mode you need to enter any one of several vi input commands.
Once in input mode, any characters you type is taken to be text and is added to the file.
You cannot execute any commands until you exit input mode.
To exit input mode, press escape (Esc) key.

Starting and Leaving vi

  • To enter vi editor
  • $ vi

  • To Exit, saving changes
  • :x

  • To Exit as long as there have been no changes
  • :q

  • To Exit and save changes if any have been made
  • ZZ

  • To Exit and ignore any changes
  • :q!

    Input commands

  • To Append after cursor
  • a

  • To Insert before cursor
  • i

  • To open line below
  • o

  • To open line above
  • O

  • To Insert file after current line
  • :r file

    Any of these commands leaves vi in input mode until you press Esc key.

    Change commands (Input mode)

    The change command is a deletion command. It leaves the editor in insert mode. It is performed by typing c followed by a motion.
  • To Change word
  • cw

  • Change to the end of the line
  • C

  • To Change the whole line
  • cc

    Changes during insert mode

  • To Back one character
  • Ctrl-h

  • To Back one word
  • Ctrl-w

  • To Back to beginning of insert
  • Ctrl-u

    Cursor and Window motions

  • Move left
  • h

  • Move down
  • j

  • Move up
  • k

  • Move right
  • l

  • Move to next word
  • w

  • Move to next blank delimited word
  • W

  • Move to the beginning of the word
  • b

  • Move to the beginning of blank delimited word
  • B

  • Move to the end of the word
  • e

  • Move to the end of blank delimited word
  • E

  • Move a sentence backward
  • (

  • Move a sentence forward
  • )

  • Move a paragraph backward
  • {

  • Move a paragraph forward
  • }

  • Move to the beginning of the line
  • 0

  • Move to the end of the line
  • $

  • Move to the first line of the file
  • 1G

  • Move to the last line of the file
  • G

  • Move to nth line of the file
  • nG

  • Move to top of screen
  • H

  • Move to middle of screen
  • M

  • Move to button of screen
  • L

  • Page up
  • Ctrl-u

  • Page down
  • Ctrl-d

    Deletion, Recovering and Undo commands

  • Delete n lines to general buffer
  • dd or ndd

  • Delete word to general buffer
  • dw

  • Delete n word
  • dnw

  • Delete to end of sentence
  • d)

  • Delete previous word
  • db

  • Delete to end of line
  • D

  • Put general buffer after cursor
  • p

  • Put general buffer before cursor
  • P

  • Undo last change
  • u

  • Undo all changes on line
  • U

    Rearrangement commands

  • Yank (copy) line to general buffer
  • yy or Y

  • Yank 6 lines to buffer z
  • "z6yy

  • Yank word to general buffer
  • yw

  • Delete 9 lines to buffer a
  • "a9dd

  • Delete 9 lines; Append to buffer a
  • "A9dd

  • Put text from buffer a after cursor
  • "ap

  • Put general buffer after cursor
  • p

  • Put general buffer before cursor
  • P

  • Join lines
  • J

    Regular expressions (search strings)

  • Matches beginning of line
  • ^

  • Matches end of line
  • $

  • Matches any single character
  • .

  • Matches any previous character
  • *

  • Matches any character
  • .*

    Search and replace commands

  • To Transpose (swap) adjacent characters
  • :[address]s/old_text/new_text/

    Where address parameter values can be: '.' Current line
    'n' Line number n
    '.+m' Current line plus m lines
    '$' Last line
    '%' Entire file
    Vi CheatSheet Home