VIM Tips

vim vim

useful commands

  • :q to quit,
  • :e (edit) to open a new file
  • :E to show file list
  • :sp file to open a new file in split window
  • :w to write (i.e., save file)
  • :w filename, give a new name when writing to “save as”, but, remember, you're still working on the original file. (Perhaps, in many cases, it may be wise to write to $file.bak before beginning work?)
  • :wq save and quit
  • :q! quit without saving changes (:qa! if you have multiple buffers)
  • bd quit buffer without leaving vim
  • :set number (turn on line numbers, can be set in .vimrc)
  • :syntax on (turn on syntax highlighting, can be set in .vimrc)
  • :Sex opens file explorer in split window
  • change window title with :set title titlestring=My\ Title
  • :gUU Change the current line to uppercase (same as VU, or Vu to lowercase).
  • :%s/[A-Z]/[a-z]/g also changes upper to lower case for the whole document or lines indicated. Clearly, %s/[a-z]/[A-Z]/g goes the other way.
  • :TOhtml - writes an html file like this one.
  • :hardcopy - sends current file to printer.
  • :earlier 2m - returns file to the state is was in 2 minutes ago. This can be done with any number, of course. Say you've made a dozen changes in the last minute, and realize they were all wrong. You want to undo them. Rather than press “u” (for “undo”) a dozen times, just do “:earlier 1m” to return the file to its state 1 minute ago. AWESOME!
  • ma - mark “a”. Makes a bookmark named “a”. To return to this mark later, type 'a. You can do this with any letter from a-z and A-Z (thus giving you 52 potential bookmarks).
  • [I - displays all lines containing the word/string under the cursor.
  • :ab pubfun public function - abbreviate “public function” with just “pubfun”. Thus, when you type “pubfun”, vim will automagically change it to “public function”. (clearly, works with any abbreviation and referrent, e.g., “:ab st something” will abbreviate “something” with “st”, and when you type “st”, “something” will automagically appear.)
  • :w !sudo tee % - you opened a file for editing as user, but user doesn't have permissions to edit the file? Do this, and now you can edit/write to the file.
  • zz - set current line to middle of window
  • zb - set current line to bottom of window
  • zt - set current line to top of window
  • 0 (zero) goes to start of line.
  • $ goes to end of line.
  • h = left, j = down, k = up, l = right (weird… l is not left).
  • e goes to end of word, b goes back one word.
  • :number goes to line number
  • { prev paragraph, } next paragraph
  • :mark a sets bookmark or anchor “a”. :'a will go back to said bookmark/anchor.
  • ctrl+r is redo
  • u is for undo. ([n]u to undo n actions).
  • :red for redo.
  • H – Go to the first line of current screen.
  • M – Go to the middle line of current screen.
  • L – Go to the last line of current screen.
  • ctrl+f – Jump forward one full screen.
  • ctrl+b – Jump backwards one full screen
  • ctrl+d – Jump forward (down) a half screen
  • ctrl+u – Jump back (up) one half screen

writing

  • i is for insert mode, in which you can write, but disables all the navigation keys.
  • R is for replace mode. (writes over, instead of insert)
  • Esc to leave insert or replace mode.
  • I – Go to the beginning of the current line and open insert mode.
  • a  - (append) This adds text after the cursor.
  • A – Go to the end of the current line and open insert mode.
  • o – Opens up a new line below the current line and opens insert mode.
  • O – Opens up a new line above the current line and opens insert mode.
  • s or r - Substitutes/replaces the letter underneath the cursor with letter you type to  insert text.
  • S, C  or cc –  This Deletes the current line and opens insert mode.

cut-copy-paste

vim in action, hacking tcl

  • v is for selection (visual/marking mode),
  • V for selecting whole lines.
  • d for cut (delete). dap will “delete a paragraph”. dab “delete a block”
  • y for copy (yank). yap = yank a paragraph.
  • c for change (write over)
  • p for paste after cursor,
  • P for paste b4 cursor.
  • double d “dd” cuts a whole line, in normal mode (not insert mode).
  • d$ - delete to end of line.
  • dw = delete word
  • x will delete one character (not insert mode).
  • [n]x will delete n characters (ie. 5x will delete 5 characters).
  • d[h,j,k,l] will delete in the specified direction (ie. dj deletes down one line, dl deletes right one char, dh deletes left one char).
  • [n]d[h,j,k,l] delete n chars/lines in specified direction.
  • o makes new line below current line.
  • O makes new line before.
  • to paste from external clipboard use

:r!xsel -o

  • to copy from vim to paste externally, select text with mouse or in visual mode (v or V), then use middle-click to paste in external program.
  • f$ goes to next character “$” (ie. fu goes to next u in same line. [n]f$ go to nth next $, i.e., 2fo goes to 2nd next “o”).

searching, replacing, regexp

search and replace is with regexp, like this:

  • %s/foo/bar/g # search for foo, replace with bar ALL instances
  • s/foo/bar/g # changes all instance in current line
  • 5,12s/foo/bar/g # changes only in lines from 5 to 12 (awesome!)
  • / is for just searching, like in a man page.
  • * searches forwards for CURRENT WORD (where cursor is), and
  • # search backwards for same thing.
  • Use regex to comment out a secion (where xx is the first line number, and yy the last)

:xx,yy s/^/#\ /

buffers and windows

  • :e file opens file in a new buffer.
  • :bn = next buffer,
  • :bp previous buffer.
  • :sp filename # open filename in new buffer, and split window.
  • ctrl+ww to change between window/buffers.
  • ctrl+ws = split windows
  • ctrl+wq = quit window
  • ctrl+wv = split vertically
  • ctrl+w[h, j, k, l] to navigate between windows
  • :buffers to list all buffers
  • :resize 20 - resize window (with horiz split window) to 20 lines.
  • :vertical resize 80 - resize vsplit window to 80 columns

external commands

  • :!{cmd} Run a shell command, shows you the output and prompts you before returning to your current buffer. (ie. “:!ls ” to list contents of pwd).
  • % will represent current working file, so, for instance “:! wc %” will give the word count of the current file.
  • :r will read an external file, or :r !{cmd} will read (paste in) output from a command. For instance

:r !uname -a

will paste in the output from uname.

  • This will check a php script for syntax errors: “:! php5 -l %”
  • If you run :shell or just :sh while you're in the editor, Vim (or Gvim, if you're partial to Vim's GUI) will place you in an interactive shell. You can run whatever commands you want, and resume your Vim session by exiting the shell. This is a bit crippled in gvim, frankly.