René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback
 

Useful VIM features

This is an overview of features of vim that I find are useful. I do not describe them in detail. This has already been made (and much better than I could do so) in the help files that come with vim.

Support for Sessions

:mksession mySession Store a session into the file mySession, so It can be used the next day.
:so mySession Load (source) yesterday's session.
vim -S mySession alternative way to source mySession
:set sessionoptions What needs to be stored with :mksession and what doesn't

Support for Views

:mkview myView Store the currentview into file mySession
:mkview Have vim determine the file (use :loadview when file re-opened)
:mkview 4 Store up to 9 configurations per file
:so myView Get the view stored with :mkview myView

Editing

Maps

Maps are a powerful features that recognizes specific sequences of keys and does something that would require more key presses. For example, I might define a map that is just rn and whenever type rn, it is expanded to my email adress.
hasmapto Check if a specific map already exists

Tabulators

expandtab Fills spaces when using the tabulator
:set listchars=tab:<- Shows tabs as <- when using :list

Setting options

More detailled help with :help options.txt. Get a table with a quick explanation on each option with :help Q_op.

Globally

Per File type basis

Whenever a new file is edited, see autocommands.
:augroup Tell me what autocommand groups exist
:filetype on Enables file type detection, by reading $VIMRUNTIME/filetype.vim. This file defines many, many autocommands, for example au BufNewFile,BufRead *.pl setf perl

Setting Options for a particular File

It's possible to set options for a specific file by including a line at the beginning of that file like this: // vim: shiftwidth: 44. When the file containing such a line is opened, the shiftwidth for that file is set to 44. Note that only set commands are allowed in order to prevent trojan horses.
:set modeline Enable the feature, don't forget :set modelines=xxx
:set modelines=5 Check the first five lines in a file for vim:

Searching

:set incsearch Search incrementally

Initialization

gvim -u NONE -U NONE foo.txtDon't read no stinky .vimrc, .gvimrc , and plugin files

Scripts

function! Name()Defines a function, function's first letter must be capitalized, end function with endfunction
exe "normal iString"Prepend typing in Normal Mode
exe "%s/X/Y/"Substituting in a function

Variables

let i=input('foo') Ask for foo, assign to i

Miscallany

:set ff=unix,dos Fileformat (end of line)
:set ffs=unix file formats
:set lazyredraw Useful for macros: the screen will not be redrawn if a macro is running.
ctrl-f If pressed in command mode: pops up a window to edit the history

Numbering

:g/^/exec "s/^/".line(".")Replacing start of line with line number

Built in functions

expand() Expands file names (for example expand("*.txt"). There are also modifiers that allow to extract parts of file names. expand("%:e") gives the extension of the file currently edited. Getting the name under the cursor: expand("<cfile>")
strpart()Getting substrings
line(".")Return current line number
col(".")Return current column

String Operations

By no means is this an exhuaustive list of string functions. I merly want to to have the ones that are not necessarly clear if coming from another language.
==? ==# =~ !~ Equal ignoring case, really equal, regex match, regex fail

The global command

The following global command deletes 10 lines before a line containing My search strin.
:g/My search string/-10,.d