Vi: Difference between revisions

From miki
Jump to navigation Jump to search
Line 121: Line 121:
* To enable syntax highlighting + numbering:
* To enable syntax highlighting + numbering:
syntax enable
syntax enable
set bg=light ''# Use set bg=dark if console background color is dark''
set bg=light ''" Use set bg=dark if console background color is dark''
set number
set number
* To have the 'usual' backspace/delete behaviour regarding EOL (see [http://comments.gmane.org/gmane.editors.vim/33436] and [http://www.cygwin.com/ml/cygwin/2005-06/msg00541.html]):
* To have the 'usual' backspace/delete behaviour regarding EOL (see [http://comments.gmane.org/gmane.editors.vim/33436] and [http://www.cygwin.com/ml/cygwin/2005-06/msg00541.html]):
set bs=2 ''# Delete at EOL joins line / BS at BOL joins line
set bs=2 ''" Delete at EOL joins line / BS at BOL joins line
* To remap movement key bindings to JKLM (instead of HJKL, so that they are right under right hand fingers on ''AZERTY'' keyboards). Commands on key 'M' are now on key 'H'.
* To remap movement key bindings to JKLM (instead of HJKL, so that they are right under right hand fingers on ''AZERTY'' keyboards). Commands on key 'M' are now on key 'H'.
nnoremap j h
nnoremap j h
Line 143: Line 143:
</source>
</source>
* Indentation stuff - Use one of the following (ref: [http://blogs.gnome.org/johannes/2006/11/10/getting-cool-auto-indent-in-vim/]):
* Indentation stuff - Use one of the following (ref: [http://blogs.gnome.org/johannes/2006/11/10/getting-cool-auto-indent-in-vim/]):
set smartindent ''Indent is based on the previous line''
set smartindent ''" Indent is based on the previous line''
set autoindent ''Same as above but also recognize some C syntax''
set autoindent ''" Same as above but also recognize some C syntax''
set cindent ''Even clever C indent mode''
set cindent ''" Even clever C indent mode''
set cinkeys=0{,0},:,0#,!,!^F ''For cindent - specifies which keys trigger reindenting''
set cinkeys=0{,0},:,0#,!,!^F ''" For cindent - specifies which keys trigger reindenting''


== Miscellaneous Tips and Tricks ==
== Miscellaneous Tips and Tricks ==

Revision as of 12:19, 25 August 2009

Links

  • General
  • Fun
    • Vi would not be vi without a bit of fun...
  • Guides & Cheat sheets
    • Very good post explaining why vi is superior and defeating common misconception (with examples) [1].
    • Very good graphical cheatsheet

Keyboard Shortcuts

Miscellaneous

! If keys HJKLM have been remapped to MHJKL, shortcut below must be changed accordingly !

Shortcut Description Custom
Ctrl+[ Same effect as Esc, but a bit faster/easier to type. Also works on AZERTY keyboard (but apparently not for AndLinux, snif!)
* Search next occurence of word under cursor (Here more like this...)
# Search previous occurence of word under cursor
gd Search first occurence of current search
^d<BS> Concatenate current line at the end of previous lines (assuming <BS> does wrap-around. See option whichwrap)
J Concatenate current line with next line
/ Recall previous search string
: Recall previous command
. Redo last command

Block Indenting

(ref: [2])

Shortcut Description Custom
>> Indent current line
5>> Indent 5 lines
Vjjjj>> Indent 5 lines - same as above but using visual mode
>% (while cursor is on a curly brace) Indent a curly-braces block
]p paste & indent block based on surrounding text
={ (C-indenting) Auto-indent the current block (:help = for more info)
== (C-indenting) Auto-indent the current line
gg=G Intends everything!

Commands

Shortcut Description Custom
:s/search/replace/ Search & replace - current line
:%s/search/replace/ Search & replace - global scope

Some Stuff to Add in ~/vimrc file

(remarks: command in ~/vimrc file must not be prefixed with a colon :)

  • 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 [3] and [4]):
set bs=2              " Delete at EOL joins line / BS at BOL joins line
  • To remap movement key bindings to JKLM (instead of HJKL, so that they are right under right hand fingers on AZERTY keyboards). Commands on key 'M' are now on key 'H'.
nnoremap j h
nnoremap k j
nnoremap l k
nnoremap m l
nnoremap h m
vnoremap j h
vnoremap k j
vnoremap l k
vnoremap m l
vnoremap h m
  • Regarding tabs (ref: [5])
set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab 
Note that tabs settings can also be specified on a per-file basis using special vim incantation:
 /* vim: tabstop=8:softtabstop=8:shiftwidth=8:noexpandtab */
  • Indentation stuff - Use one of the following (ref: [6]):
set smartindent                     " Indent is based on the previous line
set autoindent                      " 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

Miscellaneous Tips and Tricks

  • Inserting only a single character (http://vim.wikia.com/wiki/Insert_a_single_character).
  • Macro
    • qq to start recording a macro q. End macro with q again.
    • @q to replay macro, followed by . to replay it again.
  • Visual Block
    • Ctrl-v to start VISUAL BLOCK mode.
    • Shift-I to insert some text at the start of each line of selected block.
  • wrap-around
    • Set option whichwrap or ww that allows specified keys that move the cursor left/right to move to the previous/next line when the cursor is on the first/last character in the line.
    • In Vim, <space> and <backspace> are set to wrap-around by default.