Vi: Difference between revisions

From miki
Jump to navigation Jump to search
Line 7: Line 7:


=== General ===
=== General ===
* My links on '''vim''' on [http://delicious.com/m1p/vi del.icio.us].
* '''Official vim [http://www.vim.org/ homepage]'''
* Official '''vim''' [http://www.vim.org/ homepage].
* '''[http://vim.wikia.com/wiki/Vim_Tips_Wiki Vim Tips Wiki]'''
* '''[http://vim.wikia.com/wiki/Vim_Tips_Wiki Vim Tips Wiki]'''
* '''[http://vimdoc.sourceforge.net/htmldoc/help.html Vim documentation: help]'''
* '''[http://vimdoc.sourceforge.net/htmldoc/help.html Vim documentation: help]'''
* [http://vimdoc.sourceforge.net/htmldoc/map.html Vim documentation: map]
* [http://vimdoc.sourceforge.net/htmldoc/map.html Vim documentation: map]
* My links on '''vim''' on [http://delicious.com/m1p/vi del.icio.us]
* List of all commands for each mode: <code>:help index</code>
* List of all commands for each mode: <code>:help index</code>



Revision as of 07:47, 16 July 2010

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

Tips

Plugins (installed)

Plugins (not yet installed)

Some videos that illustrates those plugins:

Plugins (uninstalled)

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

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. Also we define 2 mappings to ease navigation between files.

Here an example session file Session.vim:

set tags=/win/d/projects/noekeon64/nk_fasttrails/tags
TlistAddFiles src/*.cpp
TlistAddFiles src/*.h
TrinityToggleTagList
nmap <A-Up> <C-W><Left>[[zz<CR>`"
nmap <A-Down> <C-W><Left>]]zz<CR>`"

Create the tag file:

ctags -R .

Then start the IDE session with:

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 error window:

:cope

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

Custom Cheatsheets

Cheatsheet & plugin summary

Window management

^W_
^Wh
^Wj
^Wk
^Wl
^Wp
^Wo

Maximize current window
Move to left window
Move to down window
Move to up window
Move to right window
Move to previous window
Show current window only

Plugin - taglist window (:help taglist-keys)

[[ or BS
]] or Tab
Space
- or zc
+ or zo
* or zR
=
x
CR or LMouse*2
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)
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

Plugin - Surround

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

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

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

leaderdcd
leaderdcp

DiffChangesDiffToggle
DiffChangesPatchToggle

Mouse

MouseL
gMouseR ^MouseR

Jump to tag
Return from tag

Insert

^Space

Same as Esc, but more finger friendly (see [1])

Miscellaneous

:^U

Remove the automatically inserted range (see :help omap-info)

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

:s/search/replace/g             Search & replace - current line
:%s/search/replace/g            Search & replace - global scope
:set {option}=70                Set Vim option (here textwidth)
:echo &{option}                 Show value of {option}
:set {option}?                  Show value of {option}

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 [2]. 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 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 ([3])
  • 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 [4]).
  • (from [5]), 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 [6]):
syn match cDelimiter "[&\.!^,;:<>=|+%*-]"
syn match cParenDelimiter "[][(){}]"
hi def link cDelimiter Delimiter
hi def link cParenDelimiter Delimiter
  • Replace a word with yanked text (see tip [7])
yiw         " Yank word under cursor "
...         " Move to next word "
viwp        " Replace current word with yanked text. "
...
viwp
...

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