Vi: Difference between revisions

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


=== General ===
=== General ===
* My links on vi on [http://delicious.com/m1p/vi del.icio.us].
* 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/map.html Vim documentation: map]

=== Other General ===
=== Other General ===
* [http://thomer.com//vi/vi.html Vi Lovers home page]
* [http://thomer.com//vi/vi.html Vi Lovers home page]
* [http://www.viemu.com/a-why-vi-vim.html Good post] explaining why vi is superior and defeating common misconception (with examples).
=== Fun ===
* Vi would not be vi without a bit of [http://www.dina.kvl.dk/~abraham/religion/index.html fun]...
* Vi would not be vi without a bit of [http://www.dina.kvl.dk/~abraham/religion/index.html fun]...

=== Cheat sheets ===
* [http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html Very good graphical keyboard cheatsheet ]
* [http://tnerual.eriogerg.free.fr/vim.html Another compact & complete cheatsheet (as folded reference card)].
* [http://mysite.verizon.net/astronaut/vim/vimcmd.txt.gz Real Short Vim Normal-Mode List of Commands (from Dr Chip's)]

=== Guides & Cheat sheets ===
=== Guides & Cheat sheets ===
* Very good post explaining why vi is superior and defeating common misconception (with examples) [http://www.viemu.com/a-why-vi-vim.html].
* [http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html cheatsheet Very good graphical]
* [http://tnerual.eriogerg.free.fr/vim.html Another compact & complete cheatsheet].
* [http://vim.wikia.com/wiki/Avoid_the_escape_key Avoiding the ESC key]
* [http://vim.wikia.com/wiki/Avoid_the_escape_key Avoiding the ESC key]
* [http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_(Part_1) Vim map tutorial]
* [http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_(Part_1) Vim map tutorial]
* [http://mysite.verizon.net/astronaut/vim/index.html Dr Chip's Vim Page] — A tremendous amount of Vim treasures from an expert user
* [http://mysite.verizon.net/astronaut/vim/index.html Dr Chip's Vim Page] — A tremendous amount of Vim treasures from an expert user
** [http://mysite.verizon.net/astronaut/vim/vimcmd.txt.gz Real Short Vim Normal-Mode List of Commands]
* [http://jmcpherson.org/editing.html Efficient editing with vim]
* [http://jmcpherson.org/editing.html Efficient editing with vim]
* [http://tldp.org/HOWTO/C-editing-with-VIM-HOWTO/index.html C editing with VIM HOWTO]
* [http://tldp.org/HOWTO/C-editing-with-VIM-HOWTO/index.html C editing with VIM HOWTO]
Line 28: Line 33:
* [http://vim.wikia.com/wiki/Using_bash_completion_with_ctags_and_Vim Using Bash completion with ctags and Vim]
* [http://vim.wikia.com/wiki/Using_bash_completion_with_ctags_and_Vim Using Bash completion with ctags and Vim]


=== Manual & Doc ===
* '''[http://vimdoc.sourceforge.net/htmldoc/help.html Vim documentation: help]'''
* [http://vimdoc.sourceforge.net/htmldoc/map.html Vim documentation: map]
=== Plugins (installed) ===
=== Plugins (installed) ===
* [http://www.vim.org/scripts/script.php?script_id=2540 snipMate : TextMate-style snippets for Vim]
* [http://www.vim.org/scripts/script.php?script_id=2540 '''snipMate''' : TextMate-style snippets for Vim]
* [http://www.vim.org/scripts/script.php?script_id=31 a.vim : Alternate Files quickly (.c --> .h etc)]
* [http://www.vim.org/scripts/script.php?script_id=31 '''a.vim''' : Alternate Files quickly (.c --> .h etc)]
* [http://www.vim.org/scripts/script.php?script_id=2347 trinity.vim : Build the trinity of srcexpl, taglist, NERD_tree to be a good IDE]
* [http://www.vim.org/scripts/script.php?script_id=2347 '''trinity.vim''' : Build the trinity of srcexpl, taglist, NERD_tree to be a good IDE]
** [http://www.vim.org/scripts/script.php?script_id=2179 Source Explorer (srcexpl.vim) : A Source code Explorer based on tags works like context window in Source Insight]
** [http://www.vim.org/scripts/script.php?script_id=2179 '''Source Explorer (srcexpl.vim)''' : A Source code Explorer based on tags works like context window in Source Insight]
** [http://www.vim.org/scripts/script.php?script_id=273 taglist.vim : Source code browser (supports C/C++, java, perl, python, tcl, sql, php, etc)]
** [http://www.vim.org/scripts/script.php?script_id=273 '''taglist.vim''' : Source code browser (supports C/C++, java, perl, python, tcl, sql, php, etc)]
** [http://www.vim.org/scripts/script.php?script_id=1658 The NERD tree : A tree explorer plugin for navigating the filesystem]
** [http://www.vim.org/scripts/script.php?script_id=1658 '''The NERD tree''' : A tree explorer plugin for navigating the filesystem]
* [http://www.vim.org/scripts/script.php?script_id=1343 AutoTag : Updates entries in a tags file automatically when saving]
* [http://www.vim.org/scripts/script.php?script_id=1343 '''AutoTag''' : Updates entries in a tags file automatically when saving]


=== Plugins (not yet installed) ===
=== Plugins (not yet installed) ===
* [http://www.vim.org/scripts/script.php?script_id=1520 OmniCppComplete : C/C++ omni-completion with ctags database]
* [http://www.vim.org/scripts/script.php?script_id=1520 '''OmniCppComplete''' : C/C++ omni-completion with ctags database]
* [http://www.vim.org/scripts/script.php?script_id=213 c.vim : C/C++ IDE -- Write and run programs. Insert statements, idioms, comments etc.]
* [http://www.vim.org/scripts/script.php?script_id=213 '''c.vim''' : C/C++ IDE -- Write and run programs. Insert statements, idioms, comments etc.]
* [http://cscope.sourceforge.net/ CScope] with the [http://cscope.sourceforge.net/cscope_vim_tutorial.html tutorial here]
* '''[http://cscope.sourceforge.net/ CScope]''' with the [http://cscope.sourceforge.net/cscope_vim_tutorial.html tutorial here]


=== Plugins (uninstalled) ===
=== Plugins (uninstalled) ===
* [http://www.vim.org/scripts/script.php?script_id=3114 easytags.vim : Automated tag file generation and syntax highlighting of tags in Vim]
* [http://www.vim.org/scripts/script.php?script_id=3114 '''easytags.vim''' : Automated tag file generation and syntax highlighting of tags in Vim]
** Replaced by '''AutoTag'''


== Invocation ==
== Invocation ==

Revision as of 09:58, 24 June 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

Plugins (installed)

Plugins (not yet installed)

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

Keyboard Shortcuts

Help

Shortcut Description Custom
Ctrl+]
<LeftMouse>
Jump to a subject. On azerty keyboard, do Ctrl-AltGr-] (but doesn't with Synergy).
The mouse shortcut requires :set mouse=a
Ctrl+O
Ctrl-T
g<RightMouse>
<C-RightMouse>
Back.

Splitting windows

Shortcut Description Custom
:{height}split [filename] Split the screen into two windows, optionally opening another file (with the optional specified height)
:new Open a new window on a new empty file
:close Close the current windows
:only Close all other windows
Ctrl-W w Switch between windows
Ctrl-W +
Ctrl-W -
Increase / decrease window height
{height}Ctrl-W _ Set window height, or maximize it if no height specified
:qa[ll]
:wa[ll]
Quit all windows / Write change for all windows


Tabs

Shortcut Description Custom
:tab {command} Execute the command in a new tab
:tabonly Close all tabs but the current one
:tabn[ext]
C-PageDown
gt
Go to the next tab page
:tabp[ext]
C-PageUp
gT
Go to the previous tab page

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 [1]). 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 [2])

Block Indenting

(ref: [3])

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 [4]. 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 ([5])

To Do

http://www.faqs.org/docs/Linux-HOWTO/C-editing-with-VIM-HOWTO.html
http://www.vim.org/scripts/script.php?script_id=213
http://www.ibiblio.org/pub/linux/docs/howto/translations/nl/onehtml/Vim-HOWTO-NL.html