Vi: Difference between revisions

From miki
Jump to navigation Jump to search
Line 141: Line 141:
'''g@''' call function set with the 'operatorfunc' option
'''g@''' call function set with the 'operatorfunc' option


List of frequently used ''operator motion'' (see <tt>:help operator</tt>), grouped from small to big objects. Here the motion is illustrated for operator '''delete''' ('''d''') but it can work with any other operator.
List of frequently used ''operator motion'' (see <tt>:help operator</tt>). They are used like <tt>diB</tt> (''delete inner {} block)


{| class="wikitable"
'''dl '''   delete character (alias: x)
|-
'''diw'''   delete inner word
'''daw'''   delete a word
|<tt>'''iw'''</tt>||inner word
|<tt>'''aw'''</tt>||a word
'''diW'''   delete inner WORD
'''daW'''   delete a WORD
|<tt>'''i['''</tt>||inner [] block
'''dd '''   delete one line
|<tt>'''a['''</tt>||a [] block
'''dis'''   delete inner sentence
|<tt>'''i"'''</tt>||inner "" string
'''das'''   delete a sentence
|<tt>'''a"'''</tt>||a "" string
|-
'''dib'''   delete inner () block
|<tt>'''iW'''</tt>||inner WORD
'''dab'''   delete a () block
|<tt>'''aW'''</tt>||a WORD
'''dip'''   delete inner paragraph
|<tt>'''ib'''</tt> <tt>'''i('''</tt>||inner () block
'''dap'''   delete a paragraph
'''diB'''   delete inner {} block
|<tt>'''ab'''</tt> <tt>'''a('''</tt>||a () block
|<tt>'''i<nowiki>'</nowiki>'''</tt>||inner <nowiki>''</nowiki> string
'''daB'''   delete a {} block
|<tt>'''a<nowiki>'</nowiki>'''</tt>||a <nowiki>''</nowiki> string
|-
|<tt>'''is'''</tt>||inner sentence
|<tt>'''as'''</tt>||a sentence
|<tt>'''i<'''</tt>||inner <> block
|<tt>'''a<'''</tt>||a <> block
|<tt>'''i`'''</tt>||inner `` string
|<tt>'''a`'''</tt>||a `` string
|-
|<tt>'''ip'''</tt>||inner paragraph
|<tt>'''ap'''</tt>||a paragraph
|<tt>'''it'''</tt>||inner tag block
|<tt>'''at'''</tt>||a tag block
| ||
| ||
|-
| ||
| ||
|<tt>'''iB'''</tt> <tt>'''i{'''</tt>||inner {} block
|<tt>'''aB'''</tt> <tt>'''a{'''</tt>||a {} block
| ||
| ||
|}


== Commands ==
== Commands ==

Revision as of 09:24, 29 April 2010

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
  • 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

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 [7] and [8]):
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: [9])
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: [10]):
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
  • Enable omni and keyword completion in vim (see [11]) (here remapped to Ctrl-L, use <C-@> or <nul> to remap to Ctrl-Space):
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 [12]. Here Tab does auto-completion if the character before cursor is a word character. Otherwise it inserts a tab as usual:
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
inoremap <expr> <Tab> CleverTab()
  • Avoid the ESC key:
"Press Ctrl-Space instead of ESC (because Shift-Space only work in GUI).
"See http://vim.wikia.com/wiki/Avoid_the_escape_key
"- nnoremap causes Ctrl-Space to cancel any prefix keys
nnoremap <nul> <Esc>
"- vnoremap causes Ctrl-Space to cancel any selection (gV is required to prevent automatic reselection)
vnoremap <nul> <Esc>gV
"- onoremap causes Ctrl-Space to cancel any operator pending commands (like y) 
onoremap <nul> <Esc>
"- inoremap causes Ctrl-Space to exit insert mode and `^ restore cursor position (cursor does not move left)
inoremap <nul> <Esc>`^

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 [13]. For instance to convert tabs into space
:set expandtab
:retab