Vimrc

From miki
Jump to navigation Jump to search

This page details the settings to store in the ~/.vimrc file. The settings given below can be copied verbatim in your file (note that commands in ~/.vimrc file must not be prefixed with a colon ':')

See Vim for details.

Settings from Others

My Settings

  • To enable syntax highlighting + numbering:
syntax enable
set bg=light          " Use set bg=dark if console background color is dark
set number
  • To have the 'usual' backspace/delete behaviour regarding EOL (see [1] and [2]):
set bs=2              " Delete at EOL joins line / BS at BOL joins line
  • To enable mouse double-click on hyperlink in help screen:
" Enable mouse double-click on hyperlink in help screen
" Double click or CTRL-] on any word to find help on that topic
" g<right-mouse>, CTRL-T, CTRL-O to go back
set mouse=a
  • Remap the standard cursor keys hjkl to jklm, so that they are right under the fingers (AZERTY keyboard). The command m. set mark is now available with h.
"#### Remap cursor keys to keys under the fingers! #######
noremap j h
noremap k j
noremap l k
noremap m l
noremap h m
  • In normal mode, scroll by visual lines
" Moreover, in normal mode, we scroll by visual lines (if lines wrap) (see tip http://vim.wikia.com/wiki/VimTip979)
" (only for normal mode, not for visual mode, or operator-pending mode)
nnoremap k gj
nnoremap l gk
  • There is no difference in Vim between deleting a text, and cutting it for pasting it back later. The setting below fixes this.
"#### Remap delete (d) / change (c,C) / delete char (x,X) so that it doesn't change the unnamed register ############
" c: same as before but text discarded             C: same as before but text discarded
" d: same as before but text discarded             D: delete, text saved in unnamed buffer / DD: delete current line
" x: same as before but text discarded             X: delete current line in normal mode (dd), text discarded
" <BS>: backspace (X)

nnoremap c "_c
vnoremap c "_c 
nnoremap C "_C
vnoremap C "_C 
nnoremap d "_d
vnoremap d "_d
nnoremap <silent> D :set opfunc=DeleteToUnnamed<CR>g@
nnoremap <silent> DD dd
vnoremap <silent> D :<C-U>call DeleteToUnnamed(visualmode(), 1)<CR>
nnoremap x "_x
vnoremap x "_x
nnoremap X "_dd
vnoremap X "_X
nnoremap <BS> "_X

function! DeleteToUnnamed(type, ...)
  let sel_save = &selection
  let &selection = "inclusive"

  if a:0                                     " Invoked from Visual mode, use '< and '> marks.
	silent exe "normal! `<" . a:type . "`>x"       
  elseif a:type == 'line'                
	silent exe "normal! '[V']x"
  elseif a:type == 'block'
	silent exe "normal! `[\<C-V>`]x"
  else
	silent exe "normal! `[v`]x"
  endif

 let &selection = sel_save
endfunction
  • Fast Macro
" #### Fast Macro - use qq to register the fast macro, and @@ to replay the fast macro ######
noremap @@ @q
  • Cursor movements wrap over EOL
"#### Cursor movements wrap over end-of-line ##############
set whichwrap+=<,>,h,l
  • Regarding indentation (see [[3]) and tabs (see [4]):
"#### Indentation ########################################
set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab
set autoindent                  " Indent is based on the previous line
" set smartindent               " Same as above but also recognize some C syntax
" set cindent                   " Even clever C indent mode
" set cinkeys=0{,0},:,0#,!,!^F  " For cindent - specifies which keys trigger reindenting
Note that tabs settings can also be specified on a per-file basis by adding in the file to edit a special vim incantation:
/* vim: tabstop=8:softtabstop=8:shiftwidth=8:noexpandtab */
  • Enable omni and keyword completion in vim (see [5]) (here remapped to Ctrl-L, use <C-@> or <nul> to remap to Ctrl-Space):
"#### Auto-Complete ######################################
" Press Ctrl-L to have the drop down menu with auto-completion proposals
inoremap <expr> <C-L> pumvisible() \|\| &omnifunc ==  ?
\ "\<lt>C-n>" :
\ "\<lt>C-x>\<lt>C-o><c-r>=pumvisible() ?" .
\ "\"\\<lt>c-n>\\<lt>c-p>\\<lt>c-n>\" :" .
\ "\" \\<lt>bs>\\<lt>C-n>\"\<CR>"
  • Smart-TAB, inspired from here [6]. Here Tab does auto-completion if the character before cursor is a word character. Otherwise it inserts a tab as usual:
" Alternative solution, using "clever" tabs, ie. completion is triggered only if word on the left of the cursor is an identifier
" inoremap <expr> <Tab> CleverTab()
" function! CleverTab()
"   if pumvisible()
"     return "\<C-N>"
"   endif
"   let line=strpart( getline('.'), 0, col('.')-1 )
"   if !(line =~ '^.*[a-zA-Z0-1_]$')               "completion only if last char is a word char
"     return "\<Tab>"
"   elseif exists('&omnifunc') && &omnifunc != 
"     return "\<C-X>\<C-O>"
"   else
"     return "\<C-N>"
"   endif
" endfunction
  • Avoid the ESC key:
"#### Avoid the ESCAPE Key ###############################
" Press Ctrl-Space instead of ESC (because Shift-Space only work in GUI).
" See http://vim.wikia.com/wiki/Avoid_the_escape_key
" Causes Ctrl-Space to cancel any prefix keys
nnoremap <nul> <Esc>
" Causes Ctrl-Space to cancel any selection (gV is required to prevent automatic reselection)
vnoremap <nul> <Esc>gV
" Causes Ctrl-Space to cancel any operator pending commands (like y) 
onoremap <nul> <Esc>
" Causes Ctrl-Space to exit insert mode and `^ restore cursor position (cursor does not move left)
inoremap <nul> <Esc>`^
" Prevent Ctrl-C to move one character on the left
inoremap <C-C> <Esc>`^