bembry.org
Home / Notes / Books

Learning the vi Editor

Title: Learning the vi Editor
Author: Linda Lamb and Arnold Robbins
There is a lot in this book. I've only got about half the notes so far.
  • To get vim to start with your favorite colorscheme (darkblue), add the following line to the .vimrc file
    colorscheme darkblue
  • You can precede commands with numerical arguments for repetitive tasks. For example, "6j" will take the cursor down six lines, just like pressing "j" six times.
  • The command ":set wm=10" will set the editor to automatically convert a wrapped line to a line break at 10 characters from the right margin. This is handy for avoiding long wrapped lines.
  • The ":set nu" command turns on line numbers along the left column. To turn off, use ":set nonu".
  • Following keys are movement keys:
    • j Down
    • k Up
    • h Left
    • l Right
    • 0 Beginning of line
    • $ End of the line
    • w Forward one word, punctuation as separate words
    • W Forward one word, ignore punctuation
    • b Back one word, punctuation as separate words
    • B Back one word, ignore punctuation
    • e to the end of the word, punctuation as separate words
    • E end of the word, ignore punctuation
  • In general, vim commands take the form (command)(number)(text object) or (number)(command)(text object). For example, y4w will yank four words or 2dd will delete two lines.
  • Insert Keys:
    • i Insert before the cursor
    • I Insert text at beginning of line
    • a Insert after the cursor
    • A Insert text at end of line
    • o Open a blank line below the cursor
    • O Open a blank line above the curor
    • You can prefix the append command with a number to do a repeated insert. For example, 10a* [Esc] will insert ten asterisks.
  • Editing Keys:
    • c Change. Erases identified section and enters insert mode. Requires a text object to tell what to change. For example:
      cw -- change word, or latter portion of word if cursor in mid-word.
      cc -- change whole line
      C or c$ -- change from current cursor to end of line.
      c2w -- change two words.
    • r Replace a single character
    • R Replace mode (like "typeover")
    • s Substitute a character.
    • S Substitute a whole line. Erases entire line.
    • ~ Change case of character. Can be preceded by number for multiple changes.
    • d Delete. Requires a text object to tell what to change. For example:
      dw -- delete word
      dd -- delete entire line
      D or d$ -- delete line from current cursor position
      d4w -- delete four words
    • x Delete character under the cursor
    • X Delete character before the cursor
    • p Puts the text in the buffer after the cursor
    • P Puts the text in the buffer before the cursor
    • xp Can use this to tanspose two letters, if you use "x" over the first letter.
    • y Copy text into the buffer. Requires a text object to tell it what to yank. For example: yw -- yank the word
      yy or Y -- Yank whole line
      y$ -- yank to the end of the line
    • . Repeat the last command
    • u Undo the last command
    • U Undo all edits on current line. Only works if you stay on that line.
    • [Ctrl] r Redo something you just undid
    • J Join two lines. Can be typed from anywhere on the line
  • Saving Files:
    • ZZ Save and exit the file
    • :e! Return to last saved version of the file (undo all edits since last save)
  • Moving Around:
    • ^F Forward one whole screen
    • ^B Backward one whole screen
    • ^D Down one half screen
    • ^U Up one half screen
    • z [Return] Move to top of screen, keep cursor on this line.
    • z. Move to center of screen, keep cursor on this line.
    • z- Move to bottom of screen, keep curson on this line.
    • H Home. Move to top line on the screen.
    • M Middle. Move to middle line on the screen.
    • L Last. Move to last line on the screen.
    • [return] Move to first character of next line
    • ( Move to beginning of current sentence.
    • ) Move to beginning of next sentence.
    • { Move to beginning of current paragraph
    • } Move to beginning of next paragraph.
    • [[ Move to beginning of current section.
    • ]] Move to beginning of next section
  • Basic Searches:
    • /pattern Search forward in file for the given pattern
    • ?pattern Search backward in file for the given pattern
    • /[Return] or ?[Return] Repeat search
    • n Repeat search in same direction
    • N Repeat search in opposite direction
    • Search expressions can be combined with other commands. For example, d?word will delete everything that comes before the designated word.
    • Use :set wrapscan to have vim continue searches when it reaches end of the document (if searching from middle to end, continue search at beginning, for example).
    • fc Move to the next instance of character c on the line
    • Fc Move to previous instance of characeter c on the line
    • ; Repeat previous f command in same direction
    • , Repeat previus f command in opposite direction
    • numG Go to line number num.
    • `` Return to previous position. (Those are backquotes, not apostrophes)
    • nohlsearch turns off the highlighting from a search.
  • Markers and Named Buffers:
    • Named buffers are created using double-quotes " followed by a single lower-case letter used the buffer name. These are then followed by the desired commands on what to cut or copy into the buffer.
    • "lyy Copy line into named buffer l
    • "lp Paste contents of named buffer.
    • You can append to an existing named buffer by referring to it using a capital letter. For example:
      "ayy -- copies the current line into the buffer named "a"
      "Ayy -- appends the current line to the named buffer "a"
    • ml Mark the current position with the bookmark named l
    • 'l Move to the beginning of the line where mark l is.
    • `l Move to the character where the named mark l is.
    • `` Return to exact position of the previous mark
    • '' Return to the beginning of the line where the previous mark was.
Restricted access