Some very useful commands I use to make working with vim much more elegant and easier.
Settings
show / hide line numbers
:set number
:set nonumberenable / disable syntax highlighting
:syntax on
:syntax offIf vim does not recognize the filetype, set the filetype manually
:set filetype=filetypeexample
:set filetype=phpshow ruler (cursor coordinates)
:set rulershow vim mode (INSERT / REPLACE / ...)
:set showmodeshow matching parentheses
:set showmatchreplace tabs with spaces
:set expandtabnumber of spaces per tab
:set shiftwidth=2As 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 expandtabautomatic indentation (e.g. code blocks between braces)
:set cindentUse this option with caution (same as with
expandtab) case insensitive search :set ignorecaseshow 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-insertedexample
: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 themapcommand you can store complex command sequences in a shortcut. :map shortcut command-sequenceUnmap the command (free the shortcut)
:unmap shortcutIf 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$iThis 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
qaDisable macro recorder a
qApply 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 htmlenable syntax highlighting
:syntax ondisplay line numbers in html
let html_number_lines=1format html using css
let html_use_css=1generate html code (opens in a new vim window)
:so $VIMRUNTIME/syntax/2html.vimfinally, export to a html file
:w filename.html(to close the html window afterwards, enter
:q) Code blocks indentationTo 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 combinationfind out the corresponding character combination
:digraphsexample (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 filenameInsert output of external shell command
:r !commandexample (insert date)
:r !dateSet the shell to be used for !
:set shell=shExecuting external commands
:! commandexample (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.
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.
:mkvimrcexample .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
