Vi

From miki
Revision as of 07:58, 27 July 2011 by Mip (talk | contribs) (→‎External Links: Uninstall plugin Trinity, AutoClose / Install ClosePairs, minibufexp, LustyExplorer, LustyJuggler)
Jump to navigation Jump to search

Related Pages

This page is about the editor vim itself. The other related pages are

  • Vimrc, which is dedicated to vim configuration settings and to the file ~/.vimrc.

External Links

General

Other General

Cheat sheets

Guides & Cheat sheets

Vim in Windows/Cygwin

Tips

Plugins (installed)

Plugins (not yet installed)

Some videos that illustrates those plugins:

Plugins (uninstalled)

Vimrc examples

Invocation

vi -p file1.txt file2.txt                  # Open each file in a different tab
vi -t tagname                              # Open file and move cursor at specified tag (requires ctags)
view file.txt                              # View file in vim, with syntax highlighting... (read-only)
vimdiff file1.txt file2.txt                # View differences in vim

The following is a description of the start up sequence as I understood by experience. It's certainly far from complete. For more information:

:version
:echo $HOME
:echo $VIM
:set runtimepath?
:help vimrc
:help gvimrc
:help runtimepath
  1. First, load the system config files:
  2. File description OS Location
    vimrc System config file for vim and gvim Unix & Win $VIM/vimrc
    gvimrc System config file for gvim only Unix & Win $VIM/gvimrc
  3. Second, load the user config files:
  4. File description OS Location
    vimrc User config file for vim and gvim Unix $HOME/.vimrc
    Win $HOME/_vimrc
    gvimrc User config file for gvim only Unix $HOME/.vimrc
    Win $HOME/_vimrc


  5. Search for runtime files in path 'runtimepath'. Basically vim looks for directories in that path for further runtime files to execute (see :help runtimepath for more information)

Windows

  • Tab Edit with &Vim — The following registry setting add a context menu item in explorer to open a file in a new tab in a running instance of gvim (see [1]):
  • REGEDIT4
    [HKEY_CLASSES_ROOT\*\Shell\Tab Edit with &Vim\command]
    @="\"C:\\Program Files\\vim\\vim71\\gvim.exe\" -p --remote-tab-silent \"%1\" \"%*\""
    
  • Share config with Cygwin — To have win32 gvim and cygwin vim, use the same config files
    • Simply define the env. var HOME to point to user home in cygwin:
    HOME=c:\cygwin\home\username
    
    • Create the file C:\Program Files\Vim\vimrc
    set runtimepath-=~/vimfiles
    let tmp=&runtimepath
    let &runtimepath="~/.vim,".tmp
    

    Win32 gvim will read the standard configuration files .vimrc and .gvimrc (although this is not reported by :version). It will also fetch the plugins from cygwin home directory.

Managing Options

See :help options for more details

"Show all options that differ from default
:set
"Show value of {option}
:set {option}?
"Set {option} to value
:set {option}={value}
"Add value to {option}
:set {option}+={value}

Scripting

Some basic stuff on Vim scripts:

# let is for variables
let myvar="Hello"
echo myvar
let myvar=call func(param1,param2)
# set is for vim options:
set runtimepath=~/.vim         # set option
set runtimepath?               # read option
# let <-> set
let myvar='batch'        
let &filetype=myvar            # &{option} returns option value in an expression
echo &filetype
if &filetype == 'java'         
    ...
endif
# But these does not give the same:
set runtimepath?               # No expansion
echo &runtimepath              # Expansion occurs
# Another example, use variable to prepend a path to runtimepath:
set runtimepath-=~/vimfiles
let tmp=&runtimepath
let &runtimepath="~/.vim,".tmp

Simple C/C++ IDE using plugin TagList

Thanks to plugin TagList, it is possible to turn Vim into a simple yet efficient development IDE. The basic idea is to use the TagList window as a simple file explorer. We use a session file to add all files in the project to the TagList window. For C/C++ projects, we add our common settings/mapping defined in our cpp.vim.

Here an example session file project.vim:

" Project specific settings
set tags=./tags
TlistAddFiles src/*.cpp
TlistAddFiles src/*.h
" Source our custom cpp script
source ~/.vim/cpp.vim
" Session Persistence
au VimLeave * mksession!
set sessionoptions+=blank,buffers,curdir,folds,help,options,resize,tabpages,winpos,winsize
" Find in files
command! GREP :execute "grep -R <cword> ./src"|:cw
source Session.vim

Create the tag and cscope files:

ctags -R .
cscope -Rb

Then start the IDE session with:

gvim -S project.vim

Alternatively, one can use the filename Session.vim, and use the shorter command gvim -S.


Use the mappings A-Up / A-Down to switch the current window to the next/previous file. These mappings simply move and center the cursor in the TagList window to the previous/next file, open the file and put the cursor to its last position.

To build the project (assuming you have a valid Makefile in the project directory), just issue the command

:make

To easily navigate between the compilation errors, open the quickfix window:

" Open the window when there are compilation errors:
:cw
" Or open it always:
:cope

Finally you can run the program directly from Vim with:

:!./bin/myprogram
"To repeat the last :!{cmd}, do:
:!!

Persistent session

Using :mksession one can save the current session state for later:

" Save session in Session.vim
:mksession
" Save session in lastsession.vim
:mksession lastsession.vim

For ease add the following lines at the end of our project.vim:

set sessionoptions+=blank,buffers,curdir,folds,help,options,resize,tabpages,winpos,winsize
source ./Session.vim

Now start editing with:

gvim -S project.vim

And before quiting Vim, do:

" Overwrite! Session.vim and quit
:mksession!
:wqa

This can be done automatically (see [2]). Add to your configuration file either of these autocmd:

" Update the session file only if one already exists
au VimLeave * if v:this_session != "" | exe "mksession! " . v:this_session | endif
" Or always save the session file
au VimLeave * mksession!

Keyboard & Mouse Shortcuts

  • ! If keys HJKLM have been remapped to MHJKL, shortcut below must be changed accordingly !
  • Mouse shortcuts requires :set mouse=a.
  • Customer shortcuts are underlined

Define new shortcuts

  • Note that not all combinations are possible (see [3] and help :keycodes)
    • S-b and S-B are the same
    • C-b and C-b (both mean 0x02)
    • A-b and A-B are different, but are represented by vim by setting the high bit (0x80), so A-b is same as 'â', and A-B is same as 'Â'
  • Note that GUI Vim some Alt key shortcuts select the menu. To disable that:
set guioptions-=m

Custom Cheatsheets

Plugin Cheatsheet

Plugin - a

:A
:AS
:AV
:AT
:AN
:IH Leaderih
:IHS
:IHV
:IHT
:IHN Leaderihn
Leaderis

Switch between header / source file
Split and Switch
Vertical split and Switch
New Tab and Switch
Cycles through matches
switches to file under cursor
splits and switches
vertical splits and switches
new tab and switches
cycles through matches
switches to the alternate file of file under cursor

Plugin - autoclose

{{

Insert & push closing brace down a new line

Plugin - cscope_vim

^\s ^Spaces
^\g ^Spaceg
^\c ^Spacec
^\t ^Spacet
^\e ^Spacee
^\f ^Spacef
^\i ^Spacei
^\d ^Spaced
^Space^Space s
:cs f s foo

symbol: Find all references to token, split
global: Find all global definitions, split
call: Find all calls to function, split
text: Find all instance of text, split
egrep: egrep search, split
file: open filename, split
includes: Find all files that include fname, split
called: Find functions that function calls, hsplit
Same but vsplit
Same but as command (use :scs for split)

Plugin - diffchanges

Leaderdcd
Leaderdcp

DiffChangesDiffToggle
DiffChangesPatchToggle

Plugin - MRU

:MRU
:MRU p
CR
o
t
v

Open MRU file list
Open MRU file list, only files containing p
Open file in previous window if possible
Open file in a new window
Open file in a new tab
Open file read-only (view)

Plugin - Surround

cs"'
cs'<q>
cst"
ds"
ysiw]
yssb or yss)
VmS<p id="a">
ysibspace

Change surrounding " to '
Change surrounding ' to <q>...</q>
Change surrounding tag back to "
Remove the surrounding "
Add [ ] around word (motion iw)
Add ( ) around current line (ignoring leading ws)
Add surrounding tag to selected lines
Add surrounding blank inside current () block

Plugin - taglist window (:help taglist-keys)

[[ or BS
]] or Tab
Space
- or zc
+ or zo
* or zR
=
x
CR or LMouse*2
o O
P
p
t
u
s

Previous file
Next file
Show tag prototype
Close a fold
Open a fold
Open all folds
Close all folds
Maximimze/Restore window
Jump to tag location
Jump to tag location (new window, vertical)
Jump to tag location (in previous window)
tag preview (cursor remains in taglist window)
Jump to tag (new tab)
Update tags
Change the sort order

Plugin - tComment

gcm
gcc
gCm
gCc

Toggle comments motion
Toggle comment for the current line
Comment region motion
Comment the current line

Cheatsheet

Window management

^W_ ^W| ^W=
^Wh ^Wl
^Wj ^Wk
^Wp
^Wo
^Wt

Max. h/w current window or tile
Move to left, right window
Move to down, up window
Move to previous window
Show current window only
Promote window to tab

Mouse

MouseL
gMouseR ^MouseR

Jump to tag
Return from tag

Command-line Mode (:)

^U
^R
^W

Remove auto-range (:help omap-info)

Insert register / object (:help c_CTRL-R)
Delete previous word

Insert Mode

^Space
^O

Same as Esc, but easier (see [4])
Execute one command, back to insert mode

Normal Mode

n^O
n^I
:ju[mps]
^R

Go to n older pos. in jump list
Go to n newer pos. in jump list
Print jump list
Redo

Operator & motion

Operators in Vim acts

  • on the current selection (visual mode like v, V or ^v) when there is such a selection,
  • or must be followed by a motion indicating which part of the text must be modified.

Operators

c
d
y
~
g~
gu
gU
!
=
gq
g?
>
<
zf
g@

change
delete
yank into register (does not change the text)
swap case (only if 'tildeop' is set)
swap case
make lowercase
make uppercase
filter through an external program
filter through 'equalprg' or C-indenting if empty
text formatting
ROT13 encoding
shift right
shift left
define a fold
call function set with the 'operatorfunc' option

The motion is either one of the motion key (like >% for shift right until match) or an operator motion (like diB for delete inner {} block). See :help operator.

Frequently-used operator motion

iw

iW
is
ip

inner word

inner WORD
inner sentence
inner paragraph

aw

aW
as
ap

a word

a WORD
a sentence
a paragraph

i[

ib i(
i<
it
iB i{

inner [] block

inner () block
inner <> block
inner tag block
inner {} block

a[

ab a(
a<
at
aB a{

a [] block

a () block
a <> block
a tag block
a {} block

i"

i'
i`

inner "" string

inner '' string
inner `` string

a"

a'
a`

a "" string

a '' string
a `` string

Commands

" Search & replace - current line
:s/search/replace/g                               
" Search & replace - global scope
:%s/search/replace/g                              
" Set Vim option (here textwidth)
:set {option}=70                                  
" Show value of {option}
:echo &{option}                                   
:set {option}?                                    
" Search / replace in all opened buffers
:bufdo %s/pattern/substitution/ge | update
" Replace current line with register content
VP

Tips for Efficient Editing

  • Substitute current line — Use S ( or cc) to replace the current line, while keeping current indentation (instead of ddO to delete current line and open new one)
  • Quick variable renaming — Use the following mapping to search for last deleted word
  • " F3 (an improved UltraEdit F3 ;-) ) search for next occurrence of replaced word (actually register -) - handy for refactorizing code
    " Also mapped to ù (AZERTY power!)
    nnoremap <F3> /\<<C-R>-\><CR>
    nnoremap ù /\<<C-R>-\><CR>
    

    Using this mapping, one can:

    1. Rename a variable with ciw for instance, and type the new variable name (use ^R- to start from old name)
    2. Look for next occurent of the replaced variable with F3 (or ù)
    3. Repeat replacement with . (dot key)
    4. Repeat from step 2 (or use n), or N for previous occurrence.

This tip works better than using range:s/pattern, because you don't have to type the name of replaced variable, nor use the word delimiter \<...\>, and you don't have to specify a range in advance.

Miscellaneous Tips and Tricks

  • Inserting only a single character (http://vim.wikia.com/wiki/Insert_a_single_character):
  • :nnoremap <space> :exec "normal i".nr2char(getchar())."\e"<CR>
    
  • 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 [5]. For instance to convert tabs into space
    :set expandtab
    :retab
    
  • View differences in vim
  • vimdiff file1.txt file2.txt
    
  • Interaction with X Clipboard
  • vim can use the X clipboard if it has been compiled with the clipboard feature (run vim --version and see if you have +clipboard in the output). In that case, yanking to the + register, or simply selecting with the mouse, will actually copy in the X clipboard, hence allowing other applications, or even other instances of vim to exchange text snippets. Also, it will ease the copy-paste of indented text, since in that case, vim will first disable autoindentation before pasting the text (see option paste). On system like Ubuntu, you need to install an instance of gvim (like package vim-gnome or vim-gtk) to have feature +clipboard turned on (i.e. installing package vim is not enough). When set mouse=a, use shift-mouse to still use the xterm copy/paste (see mouse-using).
    :set paste            " Enable paste mode, i.e. disable autoindent, mapping... "
    :set nopaste          " Disable paste mode (back to normal...) "
    
  • set incsearch, moves cursor as search pattern is typed. Ctrl-L to type letter under cursor, Ctrl-R Ctrl-W to type current word.
  • References:
  • http://vim.wikia.com/wiki/Best_Vim_Tips
  • type :help ctrl<C-D>, to get a list of all ctrl sequence. Type <C-D> in command line for auto-completion.
  • Support 256 colors in gnome-terminal: add set t_Co=256 in ~/.vimrc ([6])
  • Use :make and :grep instead of :!make or :!grep. Then use :cwin or :copen to view the results of either make or grep in a coloured list (from [7]).
  • (from [8]), The :g command is useful to apply a command to all lines matching a search.
  • " delete all lines matching pattern
    :g/pattern/d
    " delete all lines *NOT* matching pattern (:v same as :g!)
    :g!/pattern/d
    :v/pattern/d
    
  • Write the following in files ~/.vim/after/syntax/c.vim and ~/.vim/after/syntax/cpp.vim (or symlink) to highlight C/++ delimiters (see [9]):
  • syn match cDelimiter "[&\.!^,;:<>=|+%*-]"
    syn match cParenDelimiter "[][(){}]"
    hi def link cDelimiter Delimiter
    hi def link cParenDelimiter Delimiter
    
  • Replace a word with yanked text (see tip [10])
  • yiw         " Yank word under cursor "
    ...         " Move to next word "
    viwp        " Replace current word with yanked text. "
    ...
    viwp
    ...
    
  • Quickly switch between opened buffers. The commands :b and :sb accepts partial buffer name when specifying which buffer to edit:
  • :ls                    " List buffer list, say you have a file verylongfilename_spec.c and anotherverylong_code.c"
    :b spec                " Will switch to buffer verylongfilename_spec.c"
    :sb code               " Will split the window and switch to buffer anotherverylong_code.c"
    
  • Various command to interact with the shell:
  • :!{cmd} Execute {cmd} with the shell.
    :!! Repeat last ":!{cmd}".
    :[range]r[ead] !{cmd} Execute {cmd} and insert its standard output below the cursor or the specified line.
    :[range]w[rite] [++opt] !{cmd} Execute {cmd} with [range] lines as standard input.
  • Save a file you edited without the needed permissions (source [11])
  • :w !sudo tee %
    
  • Delete all trailing spaces ([12]):
  • :%s/\s\+$//                     " Remove all trailing spaces
    :%s/\s\+$                       " We can omit substitution text if blank
    

    Add this to .vimrc to have vim remove automatically trailing spaces before saving

    autocmd BufWritePre * :%s/\s\+$//e
    
  • Promote window to tab (see [13])
  • :noremap <C-W>t :tabedit <C-R>%<CR>gT<C-W>qgt
    
  • Find in files within Vim (commands :grep, :lgrep, :vimgrep, :lvimgrep)
  • Define this custom command to quickly look for current word, and displays the quicklist window:
    " We must embed :grep in :execute or otherwise vim will append /dev/null 
    " to :cw command and will complain of E488: trailing characters
    command! GREP :execute "grep -R <cword> ./src"|:cw
    

Command-line mode Tips

  • Ex special characters — These are special character that can be used in executed command line (:! ...) or when ex is expecting a filename (see also :help <cword>):
  • <cword>         replaced with word under cursor
    <CWORD>         replaced with WORD under cursor
    <cfile>         replaced with pathname under cursor
    ...
    
  • Insert register / object <C-R> — these can be used to insert some register content or text from context (see also :help c_CTRL-R):
  • <C-R>*          insert X11 clipboard content
    <C-R>+          insert clipboard content
    <C-R>=          expression register. You are prompted to enter an expression
    <C-R><C-W>      insert word under cursor
    <C-R><C-A>      insert WORD under cursor
    

    Example of expressions:

    nmap <C-@><C-@> :cs find s <C-R>=expand("<cword>")<CR><CR>
    

Clipboard support

  • On Linux, install gvim to get full clipboard support
    • On Terminator, use also shortcuts ^S-c and ^S-V to copy & paste.
  • On Cygwin, install gvim and a X server.
    • vim will not have clipboard support. But similar result can be obtained by using :set paste, then i, followed by S-Insert (under mintty)
    • gvim under Cygwin/X will have clipboard support.
    • There is also some plugin (e.g. [14])
  • Alternatively, install Win32 gvim (but this versions is not correctly integrated in cygwin, or requires use of wrapper)

Plugins

SnipMate

C

main  main()
inc   #include <...>
Inc   #include "..."
Def   #ifndef ... #define ... #endif
def   #define
ifdef #ifdef ... #endif
#if   #if ... #endif
once  #ifndef HEADER_H .... # define HEADER_H ... #endif (Header Include-Guard)
if    If (...) { ... } 
el    else { ... } 
t     ... ? ... : ... (tertiary conditional)
do    do ... while ( ... )
wh    while (...) { ... }
for   for (... = 0; ...; ...) { ... }
forr  for (... = ...; ...; ...) { ... }
fun   ... function(...) { ... }
fund  ... function(...;)
td    typedef
st    struct
tds   typedef struct
tde   typedef enum
pr    printf
fpr   fprintf
.     [ ... ]
un    unsigned

CPP

readfile  snippet for reading file
map       std::map<... , ...> map...
vector    std::vector<...> v...
ns        namespace ... { ... }
cl        class { public: ... private: };

To Do

http://www.faqs.org/docs/Linux-HOWTO/C-editing-with-VIM-HOWTO.html
http://www.ibiblio.org/pub/linux/docs/howto/translations/nl/onehtml/Vim-HOWTO-NL.html

Bugs

  • [2010-07-22] - (to bugs@vim.org) 'winfixheight' not honored when botright split is closed.
  • (2011-07-27) - infinite loop when using :bn after a :edit dir/ (breaks plugin minibufexpl.vim)