Vi

From miki
Revision as of 14:16, 29 April 2010 by Mip (talk | contribs) (→‎Links)
Jump to navigation Jump to search

Links

See [Vimrc] for configuration settings to store in the file ~/.vimrc.

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
  • Avoiding the ESC key [2]
  • Vim map tutorial [3]
Manual & Doc

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. Works on AZERTY keyboard in Windows, but not on Linux.
Ctrl+c Same effect as Esc, but a bit faster/easier to type. Also works on AZERTY keyboard
Ctrl+Space Same effect as Esc, but a bit faster/easier to type (see [4]). Yes
Ctrl+L Omni and keyword completion. Yes
* 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
Ctrl + o to execute more commands (see [5])

Block Indenting

(ref: [6])

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!

Operator & motion

List of available operators:

c       change
d       delete
y       yank into register (does not change the text)
~       swap case (only if 'tildeop' is set)
g~      swap case
gu      make lowercase
gU      make uppercase
!       filter through an external program
=       filter through 'equalprg' or C-indenting if empty
gq      text formatting
g?      ROT13 encoding
>       shift right
<       shift left
zf      define a fold
g@      call function set with the 'operatorfunc' option

List of frequently used operator motion (see :help operator). They are used like diB (delete inner {} block)

iw inner word aw a word i[ inner [] block a[ a [] block i" inner "" string a" a "" string
iW inner WORD aW a WORD ib i( inner () block ab a( a () block i' inner '' string a' a '' string
is inner sentence as a sentence i< inner <> block a< a <> block i` inner `` string a` a `` string
ip inner paragraph ap a paragraph it inner tag block at a tag block
iB i{ inner {} block aB a{ a {} block

Commands

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

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.
  • Read-only viewer (with syntax highlighting):
$ vi -R sensitive_file
$ view sensitive_file
  • Retab
To convert tabs in current file to current tab settings, use command :retab [7]. For instance to convert tabs into space
:set expandtab
:retab

To Do

Find a way to prevent vim to mess up with my current register when deleting text.

See for instance [8].

vnoremap y "xy
nnoremap d "xd
vnoremap d "xd
nnoremap c "xc
vnoremap c "xc
vnoremap p "xp
nnoremap <C-p> :<C-u>set opfunc=OverridePaste<CR>g@
nnoremap <C-p><C-p> :<C-u>set opfunc=OverridePaste<CR>g@g@

function! OverridePaste(type, ...)
    if a:0
        silent execute "normal! `<" . a:type . "`>\"xp"
    elseif a:type == 'line'
        silent execute "normal! '[V']\"xp"
    elseif a:type == 'block'
        silent execute "normal! `[\<C-V>`]\"xp"
    else
        silent execute "normal! `[v`]\"xp"
    endif
endfunction