{{{ VIM }}} {{{ Introduction - Brief introduction to a {{{ - Feature rich Quick navigation Mappings and abbreviations User defined commands Own scripting language Plugins Multiple windows Determines file type Syntax highlighting Spell checking Folding Can use external tools Make, ctags, grep Other Interpreters }}} - Multi platform {{{ - Modal editor normal mode commands motions insert mode Completion command line mode ex commands visual mode operator pending mode }}} }}} {{{ Motions h j k l - + w W b B f F t T , ; 0 ^ _ $ gg G % { } ( ) [[ ]] / ? n N # * marks `a 'a `A - combine with d, c, y (etc.) to work on text - You can specify a count - Text objects include iw, ap, i[, a{, a", it - Use v, V and to visualy select text - Use v, V and can also change motion style }}} {{{ Commands - Delete: x, X, d, dd and D (plus J and gJ) - Replace: r, R, gr, c, cc, C, s and S - Copy: y, yy and the special Y - Paste: p, P - Change: ~, g~, gu, gU, g?, and - Align: >, >>, :left, :right and :center - Filter: !, !!. =, == - The top section works with registers - Use "x for register x, eg "xdd and "xp - Append to register using upper case, eg. "Xdd - Special registers include unnamed: " buffers: % and # clipboard: * and + cmd. line: : blackhole: _ pattern: / - Registers can be executed with @x }}} {{{ ex commands :write :quit :q! :set :range command specific :1, :$, :%, 'a, :'<,'> Match :/bar/ Combos 'a,'b, :/foo/,/bar :substitute/foo/bar/g " replace foo with bar :global/foo/ :v/./,//-j }}} {{{ Buffere, Windows and Tabs - Buffers :args, :argadd, :argdelete :ls[!] :n, :N, :bnext, :bprev, :rew, :blast :file - Window commands :split :new, :enew :close :vertical {cmd} (eg. :vert resize +10) - Tabs :tabs, :tabedit, :tabnew gt or :tabnext and gT or :tabprev or :tabNext :tab {cmd} (eg. :tab split) :tabmove, :tabclose, :tabonly - Repeat commands in groups of buffers :bufdo :argdo :windo :tabdo (:tabdo windo) }}} {{{ Text formatting - Whitespace options: tabs, shiftwidth, softtabstop and expandtab commands: :retab - Indentation options: autoindent, cindent, smartindent and indentexpr commands: =, >>, << - Reformatting is based on recognised comments options: comments, formatoptions, textwidth (and indentation) commands: gq - Automatic reflow Normally lines are only broken according to 'textwidth' With "set fo+=a", paragraphs are reformatted automatically }}} {{{ Autocommands - Event based automatic actions command: autocommand Event Glob-Pattern Command events: BufNewFile, BufReadPre, BufRead, BufEnter, BufLeave, WinEnter, WinLeave, BufWrite, BufWritePost, BufWriteCmd, FileType, FileEncodingi, FileChangedShell, etc. - Widely used for setting options - Can update time stamps, check in files, etc. - Used in explorer plugin to browse directories (vim 6) - Used in netrw plugin to edit via ftp, http, sftp, etc. - ":help retab-example" for example (but don't use it!) }}} {{{ Configuration - vimrc and gvimrc - syntax files - plugins - ftplugins - indent scripts - compiler scripts - spell checking files - :help options.txt }}} {{{ Regular expressions Basics: ^ $ (dot) [a-z] * \+ \? \{-} \{m,n} \(...\) \| Classes: \a \d \s \S \k (iskeyword option), etc. Characters: \t \r \b \e Newline: \n \_s \_. \_X (some atom, but include newlines) Lookaround: \@= \@! \@<= \@ Screen: \%Nl \%Nc {{{ }}} {{{ Examples: Greediness: /foo.*bar/ vs. /foo.\{-}bar/ Newline: /foo\nbar/ and /foo\_s\+bar/ Limited match: /foo\zebar/ and /foo\zsbar/ Zero width: /foo\(bar\)\@=/ and /\(foo\)\@<=bar/ Negation: /foo\(bar\)\@!/ and /\(foo\)\@50v./ (vs. /\%>50c./) }}} }}} {{{ Folding - Limits the view on the buffer - Can give better overview - Folded lines can be moved as if one line - Toggled on and off with the foldenable option {{{ - Folds can be created in a number of ways by hand: Full control, but tedious indent: Follows indentation (and options) marker: User defined positions (foldmarker option) syntax: Defined by syntax definitions expression: Use vim script for complex tasks }}} - Expands with zo and zR - Movements into closed folds open them (foldopen option) - Closed by zc, zm and zM - The foldlevel option is set by zm, zM and zR - Folded lines get a header (foldtext option) - Fold levels are indicated on the left (foldcolumn option) - Other options are foldignore, foldminlines, foldlevelstart }}} {{{ Automating editing Mappings with :map, :nmap, :vmap, :omap (They have :no friends) {{{ Mapping examples: :nnoremap \K1 zoj[zztj:map K \K2:redraw! :nnoremap \K2 zcgg'':map K \K1:redraw! :nmap K \K1 :nnoremap h1 :t.s/\S\\%(\S.*\)\@<=./=/g :nnoremap h2 :t.s/\S\\%(\S.*\)\@<=./-/g :let mapleader = ',' :vmap ,q :s/^> // :onoremap S V/\n^-- $\\|\%$/ }}} Abbreviations: :ab foo for :ab for( for (;;)F(a Recording macros with q qadawwwq :let @q = @q . '@a' }}} {{{ make, grep og andre vaerktoejer - vim can (usually) handle make's output - Run :make to invoke 'makeprg' - Navigate with :cn, :cp, :cnf, :crew, etc. - vim can search included files ('include' and 'path') - Display previous uses of keyword with [i and [I - Jump to Nth definition with [ - Macros are similar, use [d, [D and [ - Validate paths with :checkpaths - Jump back and forth with and - tags can be generated with (excuberant) ctags(1) - Navigate with and :tselect - Jump back with - Vim interfaces with cscope, too - :grep is like :make, but greps files with 'grepprg' - :vimgrep is like :grep, except internal }}} {{{ Diffmode - Visual presentation of file differences - Invoke vim as vimdiff or use :diffsplit - Relies on external diff(1) and patch(1) or vim scripts - Navigate with ]c and [c - Simple merge with "do" and "dp" to current position - Advanced merge with :diffget and :diffput :[range]diffg[et] [bufspec] :[range]diffpu[t] [bufspec] - Merge single file patches with :diffpatch - Synchronise with :diffupdate }}} {{{ Spell Checking - Turned on with the 'spell' option - Set language(s) with the 'spellang' option - Non-English language requires file download - New in vim7 - Navigate with ]s and [s (]S and [S skips rare words) - Correct with z= or 1z= (normal mode) - Correct with s (insert mode) - Repeat correction with :spellrepall - Add good or bad (wrong) words (to 'spellfile' or memory) - Add entries with with zg, zG, zw and zW, undo with zug, zuw - Alternatively :spellgood[!], :spellwrong[!] and :spellundo[!] - Checks for missing capitalisation (option 'spellcapcheck') - Turn spell checking on for certain file type :au FileType * set nospell :au FileType mail set spell }}} {{{ Regular expressions (extended) How to remove the same number of X's at the end of all lines: :1s/^\%(.*[^X]\)\=\(X*\)X*\%(\n.*\1\)*\%$/%s,\1$,,\r&/|1d|@" {{{ First a simple pattern... /^ start of line \%(.*[^X]\)\= skip to just before last lot of Xs (if any) \(X*\)X* capture as many Xs as possible + more Xs (determined by how the \1 that follows matches) \%(\n.*\1\)* skip \n then all chars up to captured Xs \%$ all the way to the end of the buffer / (thus we determine in \1 max num of Xs at end of every line) }}} {{{ Then replace with a command to remove the X's: :1 on line 1 s/pat/ replace the pattern above %s,\1$,,\r& use \1 to construct a substitute command / }}} {{{ And finally delete those X's | 1d delete the constructed substitute command | @" then execute it. }}} "uniq(1)" using whole pattern match for efficiency: :%s/^\(.*\)\@>\%(\n\1\)\+$/\1/ }}} {{{ A touch of vim7 - Spell Checking - Tabs - :vimgrep - :sort [iurnxo] [/pattern/] - gF and F reads line number, too (cf. gf and f) - Omni Completion (set by filetype, use ) - More objects (a", a', a`, i", i', i` and at, it ) - Undo branches (g-, g+, :earlier, :later) - Better Unicode support (more comb. chars, individual/illegal chars) - Highlighting: matching parenthesis and current column or line - Scroll back in messages - User operator (define 'operatorfunc', call it with g@{motion}) - Mappings can be expressions (:map lhs rhs) - Mappings specifically for visual and select mode - Script enhancements (:for, :sandbox, lists, dictionaries, +=) - POSIX compatibility improvements }}} {{{ Ex Circus - Deleting all but every 10th line :g/^/+d9 :g/^/d9|m. - Make every 5th line all upper case {{{ :g/^/ka|s/.*/\U&/|+,+4d b|'a put b :g/^/ For every line ka Mark line with mark a | ... then ... s/.*/\U&/ Make line upper case | ... and ... +,+4d b Delete 4 lines to register b | ... and ... 'a put b Insert them again }}} - Reversing all lines in vi (traditional) {{{ :g/./t-|s/.*//|+s/./&\r/g :g/./ For each line with a character t- Copy the line as previous line | ... then ... s/.*// Clear that line | ... and ... +s/./&\r/g Split next line into a line per char }}} {{{ :v/^$/m?? v/^$/ For all lines that are not empty m?? Move line after previous empty line }}} {{{ :v//-,//j! v// For all lines that are not empty (again) -,//j! Join from previous (empty) line until the next empty line }}} {{{ - Reversing all lines in vim (special) :%s/.\(.*\)\@=/\=getline('.')[strlen(submatch(1))]/g }}} }}} {{{ :help! :help :help 'option' :help :ex-command :help some-topic (jumps to first likely entry) :help part ( lists possible entries) :help intro :help user-manual http://www.vim.org/ mailing lists comp.editors newsgroup http://www.ungerhu.com/jxh/vi.html http://regex.info/ (Jeffrey Friedl's Mastering Regular Expressions) }}} Preben Guldberg July 26th, 2006