sponsor Vim development Vim logo Vim Book Ad

basic Tip #422: A Quick Reference

 tip karma   Rating 305/109, Viewed by 20584 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   February 12, 2003 13:38      complexity:   basic
author:   William Natter      as of Vim:   5.7

After using vi and similar for a few years, I have accumulated a list of commands I use most often, and pass it on to people starting with vi.  The available quick reference tends to be verbose, but is useful for people with more vi experience (type ":help quickref" or go to  http://vim.sourceforge.net/htmldoc/quickref.html).  For beginners, here is my file:

VI is a text editor.  Its idea is that you manipulate text (as opposed to enter it all the time).  Almost all commands can be "repeated" a number of times, which you specify before typing the command itself (to delete a line, type dd; to repeat the deletion of a line 55 times, type 55dd).

At almost any time, "u" means "undo", and "Esc" stops all command or text entering.  For help, type :help<enter>.  There is a difference between what I call direct and indirect commands: "u" is a direct command, ":h" is an indirect one (it uses an underlying program).

:q to quit, :q! to quit without saving

Entering and manipulating text:

  Command           Interpretation

  .                 repeat the last command
  i<text>Esc        enter <text> on current line, at current position
  I<text>Esc        enter <text> on current line, at beginning of line
  a<text>Esc        enter <text> on current line, at next character position
  A<text>Esc        enter <text> on current line, at the end of the line
  o<text>Esc        enter <text> on a new line below
  O<text>Esc        enter <text> on a new line above
  r<character>      replace  characters by <character> repeated  times,
                    starting from cursor towards end of line
  s<text>Esc        substitute characters by <text>, starting from cursor
                    towards end of line
  R<text>Esc        substitute characters by <text>, starting from cursor
                    towards end of line
  ~                 change the case
  d                 delete (combine with a movement: dl deletes to the
                    right, d0 deletes to beginning of line, 5dw deletes 5
                    words forward)
  dd                delete line
  D                 delete to the end of the line (like d$)
  c                 change (same combinations as with d)
  cc                change line
  C                 change to the end of the line (like c$)
  S                 change line, like cc
  x                 delete the character at the current position
  X                 delete the character at the left (backspace)
  J                 join the next line to the current line
  y                 yank (copy, same combinations as with d)
  p, ]p             paste the result of the last deletion or yanking command
                    after the cursor
  P, [p             paste the result of the last deletion or yanking command
                    before the cursor
  /, ?              find forward, backward (then, n means next in the same
                    direction, N means next in the opposite direction)
  Ctrl-a, Ctrl-x    increase, decrease by 1 the number under the cursor (5Ctrl-a
                    increases by 5)
  :<number1>,<number2>g/<pattern>/<command>
                    on all lines within <number1>,<number2> range and matching
                    <pattern>, execute <command>.
                    :3,$g/^  table/d
                        deletes all lines from 3 to end of buffer that start
                        with "  table"
                    :%g/[a-z0-9]$/s/boo/table/
                        on all lines in buffer that end with a lowercase letter
                        or a digit, replace "boo" with "table"
                    :%g/[^RT]umble.*cran[0-6]/m'a
                        move all lines in buffer of the kind "Aumble...cran4"
                        or "Gumble...cran6" (NOT Tumble or Rumble) underneath
                        line marked as a.
  :<number1>,<number2>v/<pattern>/<command>
                    on all lines within <number1>,<number2> range and not
                    matching <pattern>, execute <command>.
  :<number1>,<number2>!<command>
                    execute shell <command> on lines <number1> to <number2>

Moving around:

  Command           Interpretation

  0, $              jump to the beginning, end of the line
  h, j, k, l        left, down, up, right (you can also use the arrow keys)
  H, M, L           jump to the highest, middle, lowest line on screen
  {, }              move up, down to the next empty line
  %                 jump to the corresponding parenthesis, square bracket or
                    curly brace
  [[, ]]            jump to the beginning, end of the file
  :1, :$            jump to the beginning, end of the file
  <number>G         jump to line <number> (without the number, jumps to the
                    end of the file)
  Ctrl-f, Ctrl-b    next page down, up
  Ctrl-d, Ctrl-u    next half-page down, up
  Ctrl-e, Ctrl-y    shift all the text by one line down, up
  w, b              go forward, backward to the next word
  e, ge             go to the end of the word, backwared to the end of the previous word
  f<char>           goes to the next <char> in the current line
  t<char>           goes to the character just before the next <char>
  m<character>, '<character>
                    set mark <character>, go to mark <character>
  ''                go to the line that was last modified

Manipulating files:

  Command           Interpretation

  :r <file>         read file in
  :w <file>         write current buffer to <file> (default: write current
                    buffer to current file, if defined)
  :w! <file>        same as write file, overriding permissions
  :wq, ZZ           write to file and quit
  :wq!              write to file and quit, no matter what
  :sp <file>        split window and edit file
  :e <file>         edit <file>
  :n                edit next file in list
  :ls               list buffers
  :buf <number>     edit buffer number <number>

Executing a command easily several times:

  Mapped keys:

    To map a key to a command to execute, type:

      :map <key> <command>

    Then, type <key> to execute <command>.

  Macros:

    To enter a macro, type:

      q<character><commands>q

    To execute the macro <number> (default: 1) times, type:

      <number>@<character>

    Important note: macros can contain calls to mapped keys.

  Registers:

    To use a register named <character>, type "<character> before your command.

      Example: copying the word under the cursor and saving into register z:

        "zyw

      Example: pasting the result of register c before this word or line:

        "cP

    Important note 1: register characters are independent of marks ('a is not affected by "a)
    Important note 2: a macro named <character> is actually stored in the register of same name.  To edit the macro f, just create a new line (o<Esc>), paste the contents of register f ("fp), edit the commands (...), go to the beginning of the line and delete/store the line into register f (0"fD), and remove the temporary line (dd).

Additions to this file are welcome, but make sure it's concise...

William

 rate this tip  Life Changing Helpful Unfulfilling 

<< the simplest map to highlight the current line | Finding more available keys to map >>

Additional Notes

Anonymous, February 12, 2003 22:31
To vi beginners I always say
"Think in English what you want to do, then type the first letters of that words and just do it."

For Windows users it could be useful to have most of these commands entered in the menu
until they get used to the keyboard shortcuts.

Thomas
Anonymous, February 13, 2003 9:23
Just a note about Ctrl-A and Ctrl-X. William says these work to increment or decrement the number under the cursor, but I have found that VIM's behavior is even more handy: You don't have to have the cursor ON the number; the incrementing or decrementing works for the next number on the line on or after the cursor.
nonline@kocharhook.com, April 19, 2003 0:54
You're right about the next number after the cursor part, but it appears Vim sticks to the same line. Still useful as all heck. I like doing things such as "35^A".
Anonymous, June 1, 2005 20:55
[[ and ]] Move to the previous/next { with scope 0, which is typically the beginning of a function.

So they are not supposed to be used for moving to the beginning or end of the file, and only do so when there are no functions declared (there are no open curly braces in the left-most column).
joppinkaru@mail.com, June 3, 2005 20:22
Very good reference.

Another simple VIM reference (good for beginners) can be found at

http://simpletutorials.com/vim/index.php

(There's a pdf reference called Simple VIM Reference that is useful)
frioux [~(a@@@t)~] gmail (dot) com, April 26, 2006 10:53
I have used vi for over six years and this is one of the best quick references I have seen so far.  Awesome!
bhayden@emdeon.com, September 19, 2006 13:18
Is there a way to go to a particular character position in a file?  Getting to a particular line is easy enough, but I've never seen a quick way to get a particular character position (e.g. jump to character 234).
Anonymous, November 1, 2006 20:04
<i>Is there a way to go to a particular character position in a file?  Getting to a particular line is easy enough, but I've never seen a quick way to get a particular character position (e.g. jump to character 234).<i>

Type "234[space bar]"
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo