Some very useful commands I use to make working with vim much more elegant and easier.

Settings

show / hide line numbers
:set number
:set nonumber
enable / disable syntax highlighting
:syntax on
:syntax off
If vim does not recognize the filetype, set the filetype manually
:set filetype=filetype
example
:set filetype=php
show ruler (cursor coordinates)
:set ruler
show vim mode (INSERT / REPLACE / ...)
:set showmode
show matching parentheses
:set showmatch
replace tabs with spaces
:set expandtab
number of spaces per tab
:set shiftwidth=2
As expandtab is not always desired (e.g. for Makefiles tabs are necessary and would lead to a syntax error when replaced by spaces) you can use autocmdto set it only for certain filetypes. example
:filetype on
:autocmd FileType php :set expandtab
automatic indentation (e.g. code blocks between braces)
:set cindent
Use this option with caution (same as with expandtab) case insensitive search
:set ignorecase
show invisible characters
:set list

Commands

custom shortcuts for string insertion
The following command sets a custom String that will be inserted every time shortcut and a word delimiter (like space or enter) is entered in insert mode.
:abbreviate shortcut string-to-be-inserted
example
:abbreviate iios #include <iostream><RETURN>

If you want to return directly into command mode after insertion, append

<ESC>to the string-to-be-inserted
custom shortcuts for complex commands
With the mapcommand you can store complex command sequences in a shortcut.
:map shortcut command-sequence
Unmap the command (free the shortcut)
:unmap shortcut
If you use :noremapinstead of :mapthe mapped command only applies in command mode and will not be evaluated at the right side of another map expression (this avoids that if you have some map expression that contains it its name the name of another map expression not both will be applied). example
:map FN o<ESC>30i*<ESC>0i/<ESC>o * function description<ENTER>
<ESC>i/<ESC>30i*<ESC>0i <ESC>ofunction {<ENTER><ESC>k$i
This example inserts with [SHIFT] + [F] + [N] a comment block and the skeleton for a new function definition and places the cursor after the function keyword. Consult some vim reference to understand the particular commands. The output looks like this:
/******************************
* function description
******************************/
function {
macros
To avoid having to type down the whole command above before seeing what comes out, you can "record" with macros what you're typing while usual work with vim.
Enable macro recorder a
qa
Disable macro recorder a
q
Apply recorded macro a
@a

Now everything you did between enabling and disabling the macro recorder has been recorded and can be reapplied.

formatting / editing
Export source code (or whatever) as syntax highlighted html
enable syntax highlighting
:syntax on
display line numbers in html
let html_number_lines=1
format html using css
let html_use_css=1
generate html code (opens in a new vim window)
:so $VIMRUNTIME/syntax/2html.vim
finally, export to a html file
:w filename.html
(to close the html window afterwards, enter :q) Code blocks indentation
To indent a whole code block (between braces), enter the following in command mode
>i{
unindent (shift to left) a code block
<i{
Find the corresponding parentheses (command mode)
%
Enter special characters
[CTRL] + [K] + character combination
find out the corresponding character combination
:digraphs
example (insert Ä)
[CTRL] + [K] + [A] + [:]
Autocompletion for keywords (e.g. for variable names)
[CTRL] + [N]

If there are more that one possibility, step through them with

[CTRL] + [N](forward) [CTRL] + [P](backward).
executing
Insert content of an external file
:r filename
Insert output of external shell command
:r !command
example (insert date)
:r !date
Set the shell to be used for !
:set shell=sh
Executing external commands
:! command
example (save and php syntax with the shortcut [SHIFT] + [T])
:noremap T :w <ENTER>:!php -l %<ENTER>
% will be replaced with the file name of the edited file.

Config file

To make settings persistent and to avoid having to enter all settings again and again everytime vim is started, you can put them all in a file called .vimrc in your home directory.
Lines beginning with " are comments.
To generate a .vimrc file with the current settings instead of entering them manually to the file, use the following command.
:mkvimrc
example .vimrc
" automatic indentation
set cindent

" replace tabs with spaces
set expandtab
" number of spaces per tab
set shiftwidth=2

" case insensitive search
set ignorecase

" whow line numbers
set number

" syntax highlighting
syntax on

" show cursor coordinates
set ruler

" executing shell
set shell=sh

" show matching parentheses
set showmatch

" show INSERT/REPLACE/...
set showmode

Random Quote

Liebe ist wie ein Verkehrsunfall. Man wird angefahren und fällt um. Entweder überlebt man - oder nicht.



Juliette Greco (*1927)

» ...

koffeinfrei on identi.ca

icon
2010-07-26 11:46:49
Everybody needs a fishbowl: “YouTube - Barry Schwartz: The paradox of choice” — http://www.youtube.com/watch?v=VO6XEQIsCoM
icon
2010-06-18 09:07:19
@kentbrew: what i'm i missing? the #identica-badge doesn't show my entries on http://koffeinfrei.org/
icon
2010-06-16 08:00:30
“YouTube - BP Spills Coffee” — http://www.youtube.com/watch?v=2AAa0gd7ClM
icon
2010-06-01 20:03:29
"The surprising truth about what motivates us”: http://www.youtube.com/watch?v=u6XAPnuFjJc&feature=player_embedded
icon
2010-05-20 21:11:48
“Sumatra PDF is a slim, free, open-source PDF viewer for Windows” — http://blog.kowalczyk.info/software/sumatrapdf/index.html

Ying and Yang

where I am
where I should be