Vi: Difference between revisions
Line 263: | Line 263: | ||
function! s:escapepair(right) |
function! s:escapepair(right) |
||
</source>}} |
</source>}} |
||
More auto-close plugins or similar: |
|||
* https://github.com/cohama/lexima.vim — Does not break undo / dot-repeat. |
|||
* delimitMate by Israel Chauca Fuentes, https://github.com/Raimondi/delimitMate [http://www.vim.org/scripts/script.php?script_id=2754] |
|||
* AutoClose by Karl Guertin, https://github.com/vim-scripts/AutoClose [http://www.vim.org/scripts/script.php?script_id=1849] |
|||
* AutoClose by Thiago Alves, https://github.com/vim-scripts/AutoClose--Alves [http://www.vim.org/scripts/script.php?script_id=2009] |
|||
* Auto Pairs by Miao Jiang, https://github.com/jiangmiao/auto-pairs |
|||
: '''Does not''' break undo / dot-repeat (<code>.</code>) on neovim and vim 7.4.849+ ('''{{green|tested}}''' on neovim). |
|||
* ClosePairs by Edoardo Vacchi, https://github.com/vim-scripts/ClosePairs [http://www.vim.org/scripts/script.php?script_id=2373] |
|||
* Smartinput by Kana Natsuno, https://github.com/kana/vim-smartinput |
|||
:breaks undo / dot-repeat (tested on neovim). Nice support of triple apostrophe. |
|||
* Simple pairs by Orestis Markou, https://github.com/vim-scripts/simple-pairs [http://www.vim.org/scripts/script.php?script_id=2339] |
|||
:Requires Python. |
|||
* vim-autoclose by Townk, https://github.com/Townk/vim-autoclose |
|||
:A new plugin. |
|||
More on auto-close: |
|||
* Explanations on the issue by [http://sharats.me/vim-undo-breaks-with-auto-close-plugins.html sharat78]. |
|||
* All tips and plugins on [http://vim.wikia.com/wiki/Automatically%5Fappend%5Fclosing%5Fcharacters wikia.com] |
|||
More on the issue of dot-repeat (with list of plugins): |
|||
* http://stackoverflow.com/questions/883437/how-do-i-get-vim-to-automatically-put-ending-braces |
|||
<li>[https://github.com/rson/vim-bufstat '''Vim BufStat''']</li> |
<li>[https://github.com/rson/vim-bufstat '''Vim BufStat''']</li> |
||
*[http://vim.1045645.n5.nabble.com/MiniBufExplorer-questions-closing-a-buffer-making-it-invisible-etc-td1181293.html MiniBufExplorer questions (closing a buffer, making it invisible, etc.)] (use '''d''' in minibufexpl, :set hidden to navigate to other buf even if current modified) |
*[http://vim.1045645.n5.nabble.com/MiniBufExplorer-questions-closing-a-buffer-making-it-invisible-etc-td1181293.html MiniBufExplorer questions (closing a buffer, making it invisible, etc.)] (use '''d''' in minibufexpl, :set hidden to navigate to other buf even if current modified) |
Revision as of 12:49, 25 October 2016
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.
- Neovim, dedicated to new port of Vim.
External Links
General
- Official vim homepage
- Vim Tips Wiki
- Vim documentation: map
- My links on vim on del.icio.us
- List of all commands for each mode:
:help index
Vi clones
General - Vim Help
- Vim documentation: help
- Built-in help:
" Get help on a command
:h map
" Get help on a mapping, NORMAL or INSERT mode
:h CTRL-R
:h i_CTRL-R
Other General
- Vi Lovers home page
- Why, oh WHY, do those #?@! nutheads use vi? — explains why vi is superior and defeats common misconceptions (with examples).
- Vi would not be vi without a bit of fun...
- vimsf discussion forum
Cheat sheets
- Very good graphical keyboard cheatsheet
- Another compact & complete cheatsheet (as folded reference card).
- Real Short Vim Normal-Mode List of Commands (from Dr Chip's)
Productivity & Setup Guides
Get the best Vim configuration
- NEW — :best of Vim
- NEW — Level up your workflow with Vim and Tmux
Tuned for Neovim
- NEW — A guide to neovim
Guides & Cheat sheets
- New To Vim
- Quick Tips
- Avoiding the ESC key
- Vim map tutorial
- Dr Chip's Vim Page — A tremendous amount of Vim treasures from an expert user
- Efficient editing with vim (link dead, wayback machine, mirror)
- C editing with VIM HOWTO
- Using vim as an IDE all in one
- Browsing programs with tags
- Using Bash completion with ctags and Vim
- Alternative tab navigation
- Seven habits of effective text editing (from author of Vim)
- Scripting the Vim editor, Part 1: Variables, values, and expressions (a great introduction to Vim scripting)
- NEW — Turn Vim & Tmux into an IDE like environment
- Uses ^h ^j ^k ^l to navigate between pane (also in Tmux apparently)
- Tips: Use CapsLock as ESC, Copy Paste, Tab movements, Relative numbering
- Plugins: NERDTree, Ctrl-P, Surround.vim, Comments, Snippets, Vim Wiki
- ColbyCheeze dot files
- NEW — Vim as XML Editor
Vim in Windows/Cygwin
- Use cygwin shell
- Running the Win32 version of Vim from Cygwin
- viEmu — vim for Word, Outlook, Visual Studio...
- cyg-wrapper.sh
- FAQ / 33.6 What are the recommended settings for using Vim with cygwin?
- Using Cygwin Bash as your Win32-Vim shell
- A patch to use Win32 clipboard under Cygwin ([1], [2])
Tips
- Use Ctrl-O instead of Esc in insert mode mappings
- How do I indent multiple lines quickly in vi?
- Replace a word with yanked text
Here we learn that Deleting, Changing and Yanking text copies the affected text to unnamed buffer ("").
But Yanking text also copies the text to buffer 0 ("0) - Search and replace the word under the cursor
- Find in files within Vim
About commands:grep
,:lgrep
,:vimgrep
,:lvimgrep
- Power of g
Examples on how to use the command:rg/pattern/cmd
- Keystroke Saving Substituting and Searching
- Quickly adding and deleting empty lines
- Very basic session persistence
Using mksession! and :au VimLeave... - Automatically fitting a quickfix window height
- Highlight unwanted spaces
- Using Vim's tabs like buffers
:set hidden
to have vim behave like any other multi-file editors.- Use
:bn
,:bp
,:b #
,:b name
, ^6 and #^6 to switch between buffers. ^6 are very useful to switch to previously used buffer (or #^6 to switch to buffer#
). - Use
:ls
to list buffers.
Vimrc examples
- https://github.com/mitechie/pyvim (author of the screencast on LustyJuggler)
- https://github.com/colbycheeze/dotfiles
- Use vim-plug to manage plugins.
Releated projects
Universal ctags
A maintained version of exuberant ctags.
First uninstall exuberant-ctags if installed:
sudo apt-get remove exuberant-ctags
To build (see docs/autotools.rst):
git clone https://github.com/universal-ctags/ctags.git
cd ctags
./autogen.sh
./configure
make
sudo make install
Plugin Managers
Various plugin manager. My current favorite is vim-plug.
apt-vim
Fully-automated, Cross-platform Plugin Manager for Vim.
Pathogen
Plugin manager from Tim Pope.
VIM (vim-addon-manager)
VAM manage and install vim plugins (including their dependencies) in a sane way.
vim-plug
Plugin manager from junegunn, author of fzf, vim-easy-align.
- Troubleshooting
- Using short path may fail if plugin name has an extension. For instance:
" The following FAILS because of trailing .vim
Plug 'ctrlpvim/ctrlp.vim'
" Use the following instead.
Plug 'https://github.com/ctrlpvim/ctrlp.vim.git'
- Conditional plugin activation.
- See the FAQ.
Plugins
Repositories
- A mirror of all plugins originally found on http://www.vim.org/scripts/ ;
- GitHub.
Getting help
- Most plugins come with local help files. Do
:help local-additions
to get a list of help files for locally installed plugins.
Installed
- snipMate : TextMate-style snippets for Vim
- Engine in VimL: https://github.com/garbas/vim-snipmate (garbas' fork), http://github.com/msanders/snipmate.vim (original, stalled)
- Engine in Python: https://github.com/SirVer/ultisnips
- Snippets definition: https://github.com/honza/vim-snippets
- Another engine, also very powerful but different syntax (not compatible with vim-snippets): https://github.com/drmingdrmer/xptemplate
- a.vim : Alternate Files quickly (.c --> .h etc)
- AutoTag : Updates entries in a tags file automatically when saving
- surround.vim : Delete/change/add parentheses/quotes/XML-tags/much more with ease (github)
- tComment : An extensible & universal comment plugin that also handles embedded filetypes
- mru.vim : Plugin to manage Most Recently Used (MRU) files
- Smart Tabs : Use tabs for indent, spaces for alignment
- See also Indent with tabs & align with spaces
- cpp.vim: General C++ Settings (no indentation for namespace...) (disabled highlight of leading tabs, line length overruns, unit test header test + nice
- diffchanges.vim : Show changes made to current buffer since the last save
- CScope
- See the tutorial here
- Cscope on Vim Wiki
- One can also use cscope and ctags together
- ClosePairs : Auto closes pairs of characters (simpler than AutoClose, and can also delete pair of braces at once with <BS>).
Below is a patch that learns ClosePairs to push down the closing brace when typing { twice (inspired from AutoClose plugin). - https://github.com/cohama/lexima.vim — Does not break undo / dot-repeat.
- delimitMate by Israel Chauca Fuentes, https://github.com/Raimondi/delimitMate [3]
- AutoClose by Karl Guertin, https://github.com/vim-scripts/AutoClose [4]
- AutoClose by Thiago Alves, https://github.com/vim-scripts/AutoClose--Alves [5]
- Auto Pairs by Miao Jiang, https://github.com/jiangmiao/auto-pairs
- Does not break undo / dot-repeat (
.
) on neovim and vim 7.4.849+ (tested on neovim). - ClosePairs by Edoardo Vacchi, https://github.com/vim-scripts/ClosePairs [6]
- Smartinput by Kana Natsuno, https://github.com/kana/vim-smartinput
- breaks undo / dot-repeat (tested on neovim). Nice support of triple apostrophe.
- Simple pairs by Orestis Markou, https://github.com/vim-scripts/simple-pairs [7]
- Requires Python.
- vim-autoclose by Townk, https://github.com/Townk/vim-autoclose
- A new plugin.
- Vim BufStat
- MiniBufExplorer questions (closing a buffer, making it invisible, etc.) (use d in minibufexpl, :set hidden to navigate to other buf even if current modified)
- Can we remap :q to :bd only when 2 or more buffer opened?
- An alternative is buftabs: Minimalistic buffer tabs saving screen space (more at StackOverflow - An alternative to minibufexplorer (vim)?)
- StackOverflow - MiniBufExplorer and NERD_Tree closing buffers unexpected behavior: See tip165 - bclose + custom mapping below, :bd might just be good enough though...
- LustyExplorer : Dynamic filesystem and buffer explore
- FuzzyFinder : buffer/file/command/tag/etc explorer with fuzzy matching
- Alternative, Command-T (inspired from TextMate)
- See also Super-Finder in Vim (using FuzzyFinder)
- After
:set path=/path/to/project/**
, one can do:find filename.ext
(See [8], more details at [9]) - To find a file in a sub-directory, use the fuzzy query **/filename
- git:file.vim : open any version of a file in git For instance, to open file filename.c at commit HEAD~4:
- vim-latex (documentation
- Align : Help folks to align text, eqns, declarations, tables, etc (see examples)
- Sparkup — A parser for a condensed HTML format Use ^e in edit mode to extend sparkup text, and ^n to go to next empty item. Some examples (more at Zen Coding:
--- mru-old.vim 2010-06-29 00:45:59.000000000 +0200
+++ mru.vim 2010-07-22 14:14:19.000000000 +0200
@@ -657,7 +657,12 @@
let wcmd = '+buffer' . bufnum
endif
- exe 'silent! botright ' . g:MRU_Window_Height . 'split ' . wcmd
+ " MIP PATCH BEGIN - :botright conflicts with TagList + QuickFix windows
+ " if I have TagList + QuickFix window opened, calling :MRU multiple times makes the QuickFix window to grow
+ " endlessly
+ " exe 'silent! botright ' . g:MRU_Window_Height . 'split ' . wcmd
+ exe 'silent! ' . g:MRU_Window_Height . 'split ' . wcmd
+ " MIP PATCH END
endif
endif
AlterColour
function that computes new colour...)
--- a/.vim/plugin/closepairs.vim
+++ b/.vim/plugin/closepairs.vim
@@ -10,7 +10,7 @@
inoremap ( ()<left>
-inoremap { {}<left>
+inoremap <expr> { <SID>openpair('{','}')
inoremap [ []<left>
vnoremap <leader>" "zdi"<c-r>z"
@@ -44,6 +44,16 @@ function! s:delpair()
return "\<bs>"
endf
+function! s:openpair(left,right)
+ let l:col = col('.')
+ let l:lchr = getline('.')[l:col-2]
+ let l:rchr = getline('.')[l:col-1]
+ if a:left == l:lchr && a:right == l:rchr
+ return "\<esc>a\<CR>;\<CR>".a:right."\<esc>\"_xk$\"_xa"
+ endif
+ return a:left.a:right."\<left>"
+endf
+
function! s:escapepair(right)
More auto-close plugins or similar:
More on auto-close:
More on the issue of dot-repeat (with list of plugins):
" GRB: use fancy buffer closing that doesn't close the split
cnoremap <expr> bd (getcmdtype() == ':' ? 'Bclose' : 'bd')
vim HEAD~4:./filename.c
div#page.section.main div[title] a[title="Hello world" rel] td[colspan=2] ul > li*5 li.item$$$*3 div#page>(div#header>ul#nav>li*4>a)+(div#page>(h1>span)+p*2)+div#footer #content>.section div#content>div.section p>{Click }+a{here}+{ to continue}
- S-CR to continue list.
{{{ ... }}}
in visual mode, add to ~/.vim/ftplugin/vimwiki_custom.vim:
" Quote a block of text with {{{ }}} in visual mode
:vmap gq DO {{{<CR><ESC>P
- Add this to bundle/ag/plugin/ag.vim to add command
:ag
instead of:Ag
[10]:
cboreabbrev ag <c-r>=(getcmdtype()=':' && getcmdpos()==1 ? 'Ag' : 'ag')<CR>
- And combined with plugin Ctrl-P (see below):
" CtrlP
" -----
" Use AG for look-up. See https://robots.thoughtbot.com/faster-grepping-in-vim
if executable('ag')
" Use ag over grep - See ag man page for option '--vimgrep'
set grepprg=ag\ --vimgrep\ $*
set grepformat=%f:%l:%c:%m
" Use ag in CtrlP for listing files. Lightning fast and respects .gitignore
let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'
" ag is fast enough that CtrlP doesn't need to cache
let g:ctrlp_use_caching = 0
" AG
" --
" bind K to grep word under cursor
nnoremap K :grep! "\b<C-R><C-W>\b"<CR>:cw<CR>
endif
:nmap <F5> :wa<CR>:Make<CR>:cw<CR> # Note the capital M to Make
To build a custom target:
:Make <targetname> # first invocation. Store targetname for future use
<F5> # next
Not yet installed
- OmniCppComplete : C/C++ omni-completion with ctags database
- c.vim : C/C++ IDE -- Write and run programs. Insert statements, idioms, comments etc.
- winmanager : A windows style IDE for Vim 6.0 (Referenced in TagList plugin help)
- bufexplorer.zip : Buffer Explorer / Browser
- neocomplcache : Ultimate auto completion system for Vim
- project.tar.gz : Organize/Navigate projects of files (like IDE/buffer explorer)
- AutoClose : Auto close pair of characters (Another version with same name than the one I tried below)
- See also dedicated tip entry: Automatically append closing characters
- SuperTab continued : Do all your insert-mode completion with Tab
- Tim Pope's commentary: comment stuff out (referenced from Practical Vim)
- textobj-entire: Text objects for entire buffer (referenced from Practical Vim)
- LaTeX Box : Lightweight Toolbox for LaTeX
- unite.vim : Unite and create user interfaces (for more WOW factor, check this blog or the homepage)
- Plugin from OSCON 2013: "More Instantly Better Vim" - Damian Conway:
- autoswap.mac (manage automatically swap file — Mac only)
- vmath.mac (compute sums, averages, min and max)
- vis.vim (let : commands to act on visual blocks, not the full lines)
- visualdrags (allows moving / duplicate visual)
- closeb : Close complex brackets or tags An extended version of closetag.vim. Press ^_ to automatically close current tag (HTML, XML, LaTeX...)
- Gist : vimscript for gist
- functionlist.vim : This plugin makes it easy to navigate between functions in a long file.
- ShowFunc.vim : Creates a list of all tags / functions from a window, all windows or buffers.
- taglist.vim : Source code browser (supports C/C++, java, perl, python, tcl, sql, php, etc)
- DeleteTrailingWhitespace : Delete unwanted whitespace at the end of lines Currently using custom mapping / autocmd (see below).
- vim-easy-align
Some videos that illustrates those plugins:
- Top Vim Plugins (YouTube), showing Surround, SnipMate, TComment, MRU, FuzzyFinder, NerdTree, MatchIt
- Another list of nice plugins (MRU, SnipMate, VimExplorer)
Uninstalled
- easytags.vim : Automated tag file generation and syntax highlighting of tags in Vim
- Replaced by AutoTag — syntax highlighting of tags is limited to c files; moreover AutoTag has a clever way to look for the tags file in project hierarchy.
- AutoClose : Inserts matching bracket, paren, brace or quote
Don't like it much because inserting the closing pair will jump out of nested pair, which is annoying when one try to simply insert a closing brace (but a solution is to use the Surrond plugin to surround some selected text with braces). - trinity.vim : Build the trinity of srcexpl, taglist, NERD_tree to be a good IDE
- Source Explorer (srcexpl.vim) : A Source code Explorer based on tags works like context window in Source Insight
- taglist.vim : Source code browser (supports C/C++, java, perl, python, tcl, sql, php, etc)
- The NERD tree : A tree explorer plugin for navigating the filesystem
- minibufexpl.vim : Elegant buffer explorer - takes very little screen space
- 1st attempt: No real tab support. Seems to interfere with trinity plugin. Interesting key bindings inside though. To reassess...
- 2nd attempt: Don't care about tabs, the idea is to fully replace tabs with buffers since most plugins interface better with buffers. Disabled
- Closing a buffer: Use d in minibufexpl window to close a buffer, or :bd[elete] seems to work most of the time.
- Uninstalled because found better alternative (bufstat)
- Someone is working on improving minibufexpl (v6.4)
- LustyJuggler: Switch very quickly among your active buffers
- Removed because I don't use it (LustyExplorer, NERDTree and FuzzyFinder are much better alternatives)
- Latex Text Formatter : This scripts makes it easier to format latex text
- Does not work well (line too short, line merge too aggressive)
- calendar.vim - A calendar application for Vim
:Calendar
to launch. < and > to cycle through views, E and T to view Event list or Task list.:Calendar 2001 1 1
to focus a specific date.
--- trinity.vim 2009-12-22 19:50:06.000000000 +0100
+++ trinity-new.vim 2010-07-06 01:24:28.000000000 +0200
@@ -90,7 +90,7 @@
" Set the window width
let g:Tlist_WinWidth = 24
" Sort by the order
- let g:Tlist_Sort_Type = "order"
+ let g:Tlist_Sort_Type = "name"
" Do not display the help info
let g:Tlist_Compact_Format = 1
" If you are the last, kill yourself
@@ -100,7 +100,7 @@
" Do not show folding tree
let g:Tlist_Enable_Fold_Column = 0
" Always display one file tags
- let g:Tlist_Show_One_File = 1
+ let g:Tlist_Show_One_File = 0
endfunction " }}}
--- taglist.vim 2007-09-21 18:11:20.000000000 +0200
+++ taglist-new.vim 2010-07-06 01:08:11.000000000 +0200
@@ -3207,14 +3207,7 @@
if a:win_ctrl == 'newwin'
split
endif
+ " 20100706 - MIP PATCH BEGIN - USE badd / buffer! so that we can
+ " switch files even when current buffer changes are not written ----
+ if !bufexists(a:filename)
+ exe "badd " . escape(a:filename, ' ')
+ endif
+ exe "buffer! " . escape(a:filename, ' ')
+ " exe "edit " . escape(a:filename, ' ')
+ " 20100706 - MIP PATCH END -----
- exe "edit " . escape(a:filename, ' ')
else
" Open a new window
if g:Tlist_Use_Horiz_Window
--- taglist.vim 2011-06-27 00:32:33.000000000 +0200
+++ taglist-new.vim 2011-06-27 00:37:23.000000000 +0200
@@ -1552,6 +1552,8 @@
\ :call <SID>Tlist_Window_Jump_To_Tag('useopen')<CR>
nnoremap <buffer> <silent> o
\ :call <SID>Tlist_Window_Jump_To_Tag('newwin')<CR>
+ nnoremap <buffer> <silent> O
+ \ :call <SID>Tlist_Window_Jump_To_Tag('newwinv')<CR>
nnoremap <buffer> <silent> p
\ :call <SID>Tlist_Window_Jump_To_Tag('preview')<CR>
nnoremap <buffer> <silent> P
@@ -1592,6 +1594,8 @@
\ <C-o>:call <SID>Tlist_Window_Jump_To_Tag('useopen')<CR>
inoremap <buffer> <silent> o
\ <C-o>:call <SID>Tlist_Window_Jump_To_Tag('newwin')<CR>
+ inoremap <buffer> <silent> O
+ \ <C-o>:call <SID>Tlist_Window_Jump_To_Tag('newwinv')<CR>
inoremap <buffer> <silent> p
\ <C-o>:call <SID>Tlist_Window_Jump_To_Tag('preview')<CR>
inoremap <buffer> <silent> P
@@ -3206,6 +3210,10 @@
" the existing window into two.
if a:win_ctrl == 'newwin'
split
+ " 20110627 - MIP PATCH BEGIN - Allow vertical split -----
+ elseif a:win_ctrl == 'newwinv'
+ vsplit
+ " 20110627 - MIP PATCH END -----
endif
" 20100706 - MIP PATCH BEGIN - USE badd / buffer! so that we can
" switch files even when current buffer changes are not written ----
@@ -3265,6 +3273,10 @@
" existing window into two.
if a:win_ctrl == 'newwin'
split
+ " 20110627 - MIP PATCH BEGIN - Allow vertical split -----
+ elseif a:win_ctrl == 'newwinv'
+ vsplit
+ " 20110627 - MIP PATCH END -----
endif
endif
endif
@@ -3313,6 +3325,7 @@
" Jump to the location of the current tag
" win_ctrl == useopen - Reuse the existing file window
" win_ctrl == newwin - Open a new window
+" win_ctrl == newwinv - Open a new window (vertical)
" win_ctrl == preview - Preview the tag
" win_ctrl == prevwin - Open in previous window
" win_ctrl == newtab - Open in new tab
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
- First, load the system config files:
- Second, load the user config files:
- 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)
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 |
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 |
Using vimdiff
Invoke vimdiff with
vimdiff file1.txt file2.txt
One can tell vim to ignore case or whitespaces with:
" Ignore case differences
:set diffopt+=icase
" Ignore white space differences
:set diffopt+=iwhite
" Tell vim to redo comparison (in case file was edited)
:diffupdate
Vim on 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 [12]):
- 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:
- Create the file C:\Program Files\Vim\vimrc (system vimrc file for windows)
- Enable Ruby support — there is a bug in Vim7.3_46 that crashes vim when ruby is loaded. This is fixed in a later release [1]. Install:
- Save .vimrc in utf-8 format *with* BOM — Vim Windows will default to Iso8859 encoding on windows. By adding the BOM to .vimrc file, it'll ensure that both Vim Linux and Vim Windows will use the same encoding to read the configuration file (relevant in case you have special character in .vimrc)
- Use Caps Lock as Escape key Import the following in the registry then logout/logon:
REGEDIT4
[HKEY_CLASSES_ROOT\*\Shell\Tab Edit with &Vim\command]
@="\"C:\\Program Files\\vim\\vim71\\gvim.exe\" -p --remote-tab-silent \"%1\" \"%*\""
Best solution however is to use the gvim explorer menu extension, which offers much more functionality (see NXL67170).
HOME=c:\cygwin\home\username
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.
REGEDIT4
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,3a,00,46,00,01,00,3a,00,00,00,00,00
To restore old behavior:
REGEDIT4
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,3a,00,46,00,01,00,3a,00,00,00,00,00
Check also potential goodies Vi#Vim_in_Windows.2FCygwin above.
Configuration
Tabs and indent
"#### Indentation ########################################
set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab
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
Or shorter:
set ts=4 sts=4 sw=4 noet
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
Variables vs Options
Some basic stuff on Vim scripts:
" Use LET for VARIABLES
let myvar="Hello"
echo myvar
let myvar=call func(param1,param2)
" Use SET for OPTIONS
set runtimepath=~/.vim " SET option"
set runtimepath? " GET option"
" We can use LET with &{OPTIONS}
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 within runtimepath (eg. replace ~ with /home/...)"
echo &runtimepath " Expansion occurs with runtimepath"
" Another example, use variable to prepend a path to runtimepath:
set runtimepath-=~/vimfiles
let tmp=&runtimepath
let &runtimepath="~/.vim,".tmp
Expressions
See :h expression-syntax
for more.
|expr1| expr2 ? expr1 : expr1 if-then-else
|expr2| expr3 || expr3 .. logical OR
|expr3| expr4 && expr4 .. logical AND
|expr4| expr5 == expr5 equal
expr5 != expr5 not equal
expr5 > expr5 greater than
expr5 >= expr5 greater than or equal
expr5 < expr5 smaller than
expr5 <= expr5 smaller than or equal
expr5 =~ expr5 regexp matches
expr5 !~ expr5 regexp doesn't match
expr5 ==? expr5 equal, ignoring case
expr5 ==# expr5 equal, match case
etc. As above, append ? for ignoring case, # for
matching case
expr5 is expr5 same |List| instance
expr5 isnot expr5 different |List| instance
|expr5| expr6 + expr6 .. number addition or list concatenation
expr6 - expr6 .. number subtraction
expr6 . expr6 .. string concatenation
|expr6| expr7 * expr7 .. number multiplication
expr7 / expr7 .. number division
expr7 % expr7 .. number modulo
|expr7| ! expr7 logical NOT
- expr7 unary minus
+ expr7 unary plus
|expr8| expr8[expr1] byte of a String or item of a |List|
expr8[expr1 : expr1] substring of a String or sublist of a |List|
expr8.name entry in a |Dictionary|
expr8(expr1, ...) function call with |Funcref| variable
|expr9| number number constant
"string" string constant, backslash is special
'string' string constant, ' is doubled
[expr1, ...] |List|
{expr1: expr1, ...} |Dictionary|
&option option value
(expr1) nested expression
variable internal variable
va{ria}ble internal variable with curly braces
$VAR environment variable
@r contents of register 'r'
function(expr1, ...) function call
func{ti}on(expr1, ...) function call with curly braces
Process control
if filereadable("foo.cfg") " Boolean"
" ...
endif
while lnum <= line("$") " Number - other tests are =, !=, <=, >="
call FixLine(lnum)
let lnum = lnum + 1
endwhile
if $ENV_VAR != "" " String"
echo $ENV_VAR
endif
" FOR with LISTS
for item in ["foo", "bar"]
echo item
unlet item " E706 without this"
endfor
for item in ["foo", ["bar"]]
echo item
unlet item " E706 without this because different item types
endfor
for [lnum, col] in [[1, 3], [2, 5], [3, 8]]
echo getline(lnum)[col]
endfor
Handy functions
(see :h functions
for complete list)
glob("/foo/bar") " String, extension file wildcard (here as file existence test)"
filereadable("foo.cfg") " Number, TRUE if file readable"
Testing version, options and features
Some examples for testing Vim version, and option/feature availability [13]:
if v:version < 703 " Exit if Vim version 7.3 or less
finish
endif
" Plugin goes here.
" Avoid installing twice or when in unsupported Vim version.
if exists('g:loaded_pluginname') || (v:version < 700)
finish
endif
let g:loaded_pluginname = 1
To test whether an option/feature is available
exists('+relativenumber') " Is option relativenumber available?
has('python') " Is feature python supported?
Frequent caveats
- Don't confuse variables and options. First are initialized with let, second with set. Options can be read/written like a variable with prefix &, like &filetype.
- Don't confuse functions (like max, strlen), with editor commands (like let, set, call). Functions can be called directly in expression, but must be called with command call where commands are expected.
add(list,10) " WRONG! add is not an editor command"
list2=add(list,10) " CORRECT"
call add(list,10) " CORRECT"
- Give regular expression patterns in single-quotes!
echo match("binary(blob)","b\|(",0) " WRONG!"
echo match("binary(blob)",'b\|(',0) " CORRECT"
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 [14]). 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!
Troubleshoot
- FuzzyFinder plugin complains about:
Error detected while processing function 148..229..fuf#openTag:
line 1:
E432: Tags file not sorted: tags
- Tags were generated with
ctags --sort=foldcase -R .
- Solution is to set option
:set ignorecase
Make with a custom target
See plugin mcf-make-target above.
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 [15] 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
:A |
Switch between header / source file |
{{ |
Insert & push closing brace down a new line |
Leadercs ^Spaces |
symbol: Find all references to token, split |
Leaderdcd |
DiffChangesDiffToggle |
:MRU |
Open MRU file list |
cs"' |
Change surrounding " to ' |
[[ or BS |
Previous file |
gcm |
Toggle comments motion |
^m |
Jump to next |
Cheatsheet
^W_ ^W| ^W= |
Max. h/w current window or tile |
MouseL |
Jump to tag |
^U |
Remove auto-range (:help omap-info )Insert register / object ( |
^Space |
Same as Esc, but easier (see [16]) |
n^O |
Go to n older pos. in jump list |
^^ or ^6 (azerty) |
Edit alternate buffer (#) |
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
|
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 |
inner word inner WORD |
aw aW |
a word a WORD |
i[ ib i( |
inner [] block inner () block |
a[ ab a( |
a [] block a () block |
i" i' |
inner "" string inner '' string |
a" a' |
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
- Dot command — See [17] for example.
- Use * to search word under cursor (and # backward) &mdash see also [18] for example.
- 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
- Rename a variable with ciw for instance, and type the new variable name (use ^R- to start from old name)
- Look for next occurent of the replaced variable with F3 (or ù)
- Repeat replacement with . (dot key)
- Repeat from step 2 (or use n), or N for previous occurrence.
- Quick buffer edit — Use
:b {bufname}
to quickly jumped to buffer referenced by its number (see:ls
) or by its name. Buffer name can be incomplete as long as it is unique (this is very handy!!!). - use ( and ) to move between sentences, ]p paste indented, `. move to last edit (source Efficient editing with vim)
- Smart ranges and clipboards — (source [19])
- Starting from cursor position above , type c% and equivESC, and then Obool equiv = C-R";ESC to get the result below.
- Syntax folding — Enable automatic folding for javascrip, PHP, XML, ... [20] . Add to .vimrc:
- Use Caps Lock as another Escape (ESC) key. On Linux, see X#Configuring_Layout_Options. Basically we update gnome settings for current user:
" 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:
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.
if (!entry.used && |equivalent(entry.key(), qk.key) && (curcontext & entry.contexts))
bool equiv = equivalent(entry.key(), qk.key);
if (!entry.used && equiv && (curcontext & entry.contexts))
set foldmethod=syntax
set foldlevelstart=1
let javaScript_fold=1 " JavaScript
let perl_fold=1 " Perl
let php_folding=1 " PHP
let r_syntax_folding=1 " R
let ruby_fold=1 " Ruby
let sh_fold_enabled=1 " sh
let vimsyn_folding='af' " Vim script
let xml_syntax_folding=1 " XML
gsettings get org.gnome.desktop.input-sources xkb-options
gsettings set org.gnome.desktop.input-sources xkb-options "@as ['caps:escape']"
On Windows, see Map caps lock to escape in Windows. Import the registry file (Win7/Win8):
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,3a,00,46,00,01,00,3a,00,00,00,00,00
Practical Vim
Part I — Modes
- Tip 4: Act, Repeat, Reverse —
- Tip 6: Meet the Dot Formula —One keystroke to move, one keystroke to execute (like
A.ESC
thenj.j.j.
,f+s_+_
then;.;.;.
,*cwcopyESC
thenn.n.n.
) - Tip 9: Compose Repeatable Changes — e.g. favor daw instead of dbx or bdw to delete a word, because the former is a single operation, and hence invests the most power into the dot command.
- Tip 13: Make Corrections Instantly from Insert Mode — e.g. use ^h for backspace, ^w for delete word, ^u for delete line.
- Tip 14: Go Back to Normal Mode — use ^[ for Switch to Normal mode, and ^o for Insert Normal mode (e.g. ^ozz).
- Tip 15: Paste from a Register Without Leaving Insert Mode
- use ^r for putting character-wise register, and ^r^o for inserting characters litteraly and without auto-indent
- remap CAPS LOCK (capslock) to something else at system level, either ESC (escape) or CTRL (see Tips section).
- Tip 16: Do Back-of-the-Envelope Calculations in Place — Example a ^r= 6*35 CR
- Tip 17: Insert Unusual Characters by Character Code — ^v123 inserts by decimal code, ^vu1234 inserts by hexadecimal code, ^v non-digit inserts literally, ga returns character code.
- Tip 18: Insert Unusual Characters by Digraph — ^k char1 char2 inserts digraph. See
:h digraphs-default
(convention),:h digraphs
(all digraphs),:h digraph-table
(more usable list). There's also plugin betterdigraphs.vim from Damian Conway. - Tip 19: Overwrite Existing Text with Replace Mode — r, R to replace, Insert to switch mode, gr,gR to replace visually.
- Tip 21: Define a Visual Selection — gv reselect the last visual selection, o go to other end of highlighted text.
- Tip 27: Command-Line Mode — ^v or ^k to insert special character, ^r{register} to insert content of any register.
- Tip 28: Execute a Command on One or More Consecutive Lines — Example of ranges
:1p
,:$p
,:2,5p
,:.,$p
,:%p
,:'<,'>p
,/<html>/,/<\/html>/p
,/<html>/+1,/<\/html>/-1p
,:.,.+3p
(there is also0
for virtual line before first line). - Tip 29: Duplicate or Move Lines Using ':t' and ':m' Commands —
:[range]copy {address}
or:[range]t {address}
for copy,:[range]move {address}
or:[range]m {address}
for move. - Tip 30: Run Normal Mode Commands Accross a Range — Use
:[range]normal {command}
or:[range]norm {command}
. Eg.:%norm .
,:%norm A;
,:%norm i//
. - Tip 31: Repeat the Last Ex Command — Use
@:
and@@
afterwards (last command always stored in:
register). - Tip 32: Tab-Complete Your Ex Commands — Use ^d to view auto-complete list, Tab and S-Tab to cycle through it.
- Tip 33: Insert the Current Word at the Command Prompt — Use ^r^w to insert current word, ^r^a for current WORD.
- Tip 34: Recall Commands from History — Use Up/Down or or ^p/^n to recall command with filtering. Use :q for Command-Line Window (Enter to execute, :q to leave). Use ^f to switch to Command-line window, even while typing a command. Use following mapping to have filtering enabled for ^p/^n:
- Tip 35: Run Commands in the Shell — Use
%
as shorthand for current file name, like:!ruby %
(see :h cmdline-special):shell
runs a shell (in a terminal better use ^z to suspend vim),:!{cmd}
executes a command with the shell,:read !{cmd}
inserts command output at the cursor,:[range]write !{cmd}
executes command with selected text as input,:[range]!{filter}
filters selected text through command.
Make a change | {edit} | . | u |
Scan line for next character | f{char} / t{char} | ; | , |
Scan line for previous character | F{char} / T{char} | ||
Scan document for next match | /patternCR | n | N |
Scan document for previous match | /patternCR | ||
Perform substitution | :s/target/replacement | & | u |
Execute a sequence of changes | qx {changes} q | @x éx |
u |
cnoremap <C-p> <Up>
cnoremap <C-n> <Down>
Part II — Files
- Tip 36: Track Open Files with the Buffer List —
:ls
to list buffers;:buffer N
or:buffer {bufname}
to jump directly;:bprevious
,:bnext
,:bfirst
and:blast
to navigate through buffers;:bdelete
to delete buffers; and:bufdo
to execute command on all buffers.:write
,:update
and:saveas
to write buffer to disk.
Tips and Tricks
Miscellaneous
- 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):
- Retab To convert tabs in current file to current tab settings, use command
- View differences in vim
- Interaction with X Clipboard vim can use the X clipboard if it has been compiled with the clipboard feature (run
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 ([22]) - 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 [23]). - (from [24]), The
:g
command is useful to apply a command to all lines matching a search. - vim-syntax-extra — C syntax extension, to also highlight C delimiters. Copy as file ~/.vim/after/syntax/c.vim (and ~/.vim/after/syntax/cpp.vim). The previous solution was to use the settings below, but these break code folding with
- Replace a word with yanked text (see tip [25])
- Quickly switch between opened buffers. The commands
:b
and:sb
accepts partial buffer name when specifying which buffer to edit: - Various command to interact with the shell:
- Save a file you edited without the needed permissions (source [26])
- Delete all trailing spaces ([27]):
- Promote window to tab (see [29])
- 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:
- Use
:E[xplore]
for explore directory of current file (from netrw) - Formatting code with column shell filter. Say you have a code like the one on the left. To align this text, select the 3 lines, then
!column -t
. This will give: - Use modeline to embed vim format settings in the document itself. This requires the following line in ~/.vimrc:
:nnoremap <space> :exec "normal i".nr2char(getchar())."\e"<CR>
$ vi -R sensitive_file
$ view sensitive_file
:retab
[21]. For instance to convert tabs into space
:set expandtab
:retab
vimdiff file1.txt file2.txt
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...) "
" delete all lines matching pattern
:g/pattern/d
" --> example: delete empty lines
:g/^$/d
" --> example: delete lines with only whitespaces
:g/^\s*$/d
" delete all lines *NOT* matching pattern (:v same as :g!)
:g!/pattern/d
:v/pattern/d
foldmethod=syntax
.
" OBSOLETE - THESE BREAK FOLDMETHOD=SYNTAX
syn match cDelimiter "[&\.!^,;:<>=|+%*-]"
syn match cParenDelimiter "[][(){}]"
hi def link cDelimiter Delimiter
hi def link cParenDelimiter Delimiter
yiw " Yank word under cursor "
... " Move to next word "
viwp " Replace current word with yanked text. "
...
viwp
...
: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"
:!{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. |
:w !sudo tee %
:%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
More elaborate version:
" Trim trailing spaces on save
if !exists("autocmd_BufWritePre_trimspace_tab")
let autocmd_BufWritePre_trimspace_tab = 1
autocmd FileType c,cpp,java,php,vim autocmd BufWritePre <buffer> :call setline(1,map(getline(1,"$"),'substitute(v:val,"\\s\\+$","","")'))
" autocmd BufWritePre * :set expandtab<CR>:retab
endif
Or better yet, create a custom mapping [28]:
" F10 - Remove all trailing whitespace
nnoremap <F10> :let _s=@/<Bar>:%s/\s\+$//e<Bar>:let @/=_s<Bar><CR>
:noremap <C-W>t :tabedit <C-R>%<CR>gT<C-W>qgt
" 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
int myint = 1;
double a = 2.0;
float verylongf = 1234.50;
|
→ |
int myint = 1;
double a = 2.0;
float verylongf = 1234.50;
|
set modeline
Now you can for instance for automatic word wrapping in a text document by adding the following line to the document (for instance as the last line or within a comment block):
/* vim: set ai fo=tcroq tw=105: */
Alternatively, use the following to disable automatic line break, but still wrap words at word boundaries:
/* vim: set ai fo=tcroq tw=0 wrap linebreak nolist: */
Add spell
to enable background spell checker (e.g. LaTeX document):
/* vim: set ai fo=tcroq tw=105 spell: */ /* vim: set ai fo=tcroq tw=0 wrap linebreak nolist spell: */
set spelllang=fr
set fileencoding=utf-8
to convert a file to unicode UTF-8 format. Vim can also add the BOM (Byte Order Mark) with setlocal bomb
" Convert into utf-8
set fileencoding=utf-8
" Add the BOM
setlocal bomb
" Remove the BOM
setlocal nobomb
!par -jw74
:let @+=expand("%")<CR> " Copy basename
:let @+=expand("%:p")<CR> " Copy fullpath
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>
): - Insert register / object <C-R> — these can be used to insert some register content or text from context (see also
:help c_CTRL-R
): - Insert an environment variable $VAR in command map — Use
<C-R>=$VAR<CR>
to have a command map use the value of an environment variable - New user-defined command in lowercase — One cannot define a user-defined in lower case with
<cword> replaced with word under cursor
<CWORD> replaced with WORD under cursor
<cfile> replaced with pathname under cursor
...
<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>
nmap <silent> <F12> :wa<CR>:b <C-R>=$VPMAINDOC<CR><CR>\ll
newcommand
because Vim forces the command to start with an uppercase. But we can circumvent this requirement with an abbreviation [30]:
cboreabbrev ag <c-r>=(getcmdtype()=':' && getcmdpos()==1 ? 'Ag' : 'ag')<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
, theni
, followed by S-Insert (under mintty) - gvim under Cygwin/X will have clipboard support.
- There is also some plugin (e.g. [31])
- vim will not have clipboard support. But similar result can be obtained by using
- Alternatively, install Win32 gvim (but this versions is not correctly integrated in cygwin, or requires use of wrapper)
Whitespaces
- A nice tip about highlighting unwanted spaces
- Use
set list
andset listchars
to show trailing space, tags, extending spaces as another character...
Some examples:
:set list listchars=tab:>-,trail:.,extends:>,precedes:<
" Enter the middle-dot by pressing Ctrl-k then .M
:set list listchars=tab:\|_,trail:·
" Enter the right-angle-quote by pressing Ctrl-k then >>
:set list listchars=tab:»·,trail:·
" Enter the Pilcrow mark by pressing Ctrl-k then PI
:set list listchars=tab:>-,eol:¶
" The command :dig displays other digraphs you can use.
- To hide tabs even when using listchars:
:set list listchars=tab:\ \ ,trail:·,extends:>,precedes:<
- This is also subject to highlighting ("NonText" is used for "eol", "extends" and "precedes" / "SpecialKey" for "nbsp", "tab" and "trail")
Advanced source highlighting
- See file $VIMRUNTIME/syntax/c.vim for more advanced highlighting options for C files, like
- Highlighting of space errors
- Highlighting of curly, parens errors...
- Highlight column:
:set colorcolumn=+1 " highlight column after 'textwidth'
:set colorcolumn=+1,+2,+3 " highlight three columns after 'textwidth'
:set colorcolumn=80 " highlight column 80
:highlight ColorColumn ctermbg=lightgrey guibg=lightgrey
Digraphs
Digraph is an easy way to enter special character in Vim, much easier than using ^V. See :help dig
for more details.
code | result |
---|---|
^k.M | · |
^k>> | » |
^kPI | ¶ (Pilcrow mark) |
viEmu
Tips for better use of viEmu.
- Applying format in Word via keyboard. To apply format like bold or italic, you can't use the regular key binding C-b or C-i since it is intercepted by viEmu. Some work-arounds:
- (best) Use the new Ribbon shortcuts. For instance, A-h 1 for bold, A-h 2 for italic, Alt 3 for redo...
- (heavy) Temporarily disable viEmu with C-S-A-v. Say C-S-A-v C-b C-S-A-v for bold.
- (worsts) In the viEmu menu, tell viEmu not to intercept C-b and to pass it to Word.
- In viEmu, don't let C-c to be passed to office app. This allows using C-C to leave insert mode.
- In viEmu, don't Emulate vi/vim block caret. This allows formatting a single word w/o selecting it first, as usual in office.
- In viEmu, don't Update display during long commands. Unchecking this box seems to fix the display glitch with viEmu status bar when scrolling down.
Editing HTML, XML, LaTeX
(for LaTeX, see also the vim-latex plugin)
- Use closetag
- Use C-X C-O (omnicomplete) or define macro:
imap ,/ </<C-X><C-O>
- Define abbreviations (see [32])
- Surround plugin in insert mode (use ^stli<enter> to insert a
<li>
tag for instance) - LaTex + surround plugin, use ysm\ to insert
\begin{...} \end{...}
environment. - To format LaTeX, see also plugin Latex Text Formatter.
Omni-Completion
Vim omni-completion C-X C-O is quite powerful. Example:
- To insert HTML closing tags, type
</C-X C-O
<sometag>
some text
some text
</C-X C-O
- To complete a LaTeX tag. For instance to close a
\begin
environment, type C-X C-O C-O C-X C-O:
\begin{theorem} some text some text C-X C-O C-O C-X C-O
Use Vim as Hexadecimal Editor
- From [33]
" To convert current buffer to hex
:%xxd
" To convert current buffer from hex
:%xxd -r
- Use plugin hexman.vim. This still require to open vim in binary mode to prevent insertion of newlines (see [34]):
vim -b file # Might still require ":set noeol"
vim ++bin file
vim file # Then ':set binary' and ':set noeol'
Plugins
Install a plugin
- Unzip the plugin in ~/.vim directory (or plugin, autoload...)
- Generate the help tags with:
helptags ~/.vim/tags
vim-latex
See page vim-latex.
Building from Sources
See
To Do
- Done Find a way to prevent vim to mess up with my register when deleting text.
- Auto-complete, use cursor keys instead of up/down to search in the drop down list.
- Look at Virtual-edit (see http://vim.wikia.com/wiki/Add_trailing_blanks_to_lines_for_easy_visual_blocks)
- Other links:
- 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
- NEW — Install neovim.
- NEW — Install universal ctags.
Troubleshoot
- Command
syn match
may create memory leaks which slowly impact performance. Do the following as temporary fix (from [35]):
autocmd BufWinLeave * call clearmatches()
- Slow cursor move (vertical / horizontal)
- references: [36],
- Can be due to cursorline. Try
set nocursorline
. Also remove any occurence ofhighlight CursorLine ...
- Can be due to cursorcolumn. Try
set nocursorcolumn
. Also remove any occurence ofhighlight CursorColumn ...
- Can be due to slow matchparen. Add to .vimrc:
let loaded_matchparen = 1
set noshowmatch
- Can be improved with
set lazyredraw
- Meta-characters <M-x> (aka <Alt-x>) not working in console
set <M-x>=^[x
- Note, this may also require the following in the shell console:
set convert-meta on
- Although my current settings below also work it seems:
set meta-flag on set convert-meta off set input-meta on set output-meta on
- Another solution is to set the terminal in 8-bit mode as suggested in the link. This is however not possible with Gnome-terminal. A more advanced solution use a timeout to allow distinguishing between M-x and EscTemplate:X [39]:
let c='a'
while c <= 'z'
exec "set <A-".c.">=\e".c
exec "imap \e".c." <A-".c.">"
let c = nr2char(1+char2nr(c))
endw
set timeout ttimeoutlen=50
Or use the following to have timeout only for keycodes and not other mappings:
set ttimeout ttimeoutlen=50
- Troubleshoot startup speed issue
- Vim can log the time to start each plugin [40]:
vim --startuptime timing.log SOMEFILE
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)
{{{content}}}