As I am using vim frequently, gradually I find it necessary to grasp some advanced vim topics. Sometimes, search engines don't return the useful posts/resources in the first page.
Vim built-in functions, internal variables, data structures, etc. http://vimdoc.sourceforge.net/htmldoc/eval.html
Filetype http://www.vim.org/htmldoc/filetype.html
Command line mode http://vimdoc.sourceforge.net/htmldoc/cmdline.html
Options and commands:
http://vimdoc.sourceforge.net/htmldoc/options.html
http://vimdoc.sourceforge.net/htmldoc/various.html
Configuration: http://peox.net/articles/vimconfig.html
Other topics (FAQs): http://www.cs.utah.edu/~kad/vim/vimnotes.shtml#17
Vim Buffer Resources
http://www.vim.org/htmldoc/windows.html
http://vim.wikia.com/wiki/Vim_buffer_FAQ
Information getting | |
Ctrl+g, :f, :file | print some info of current file in command line |
:ls, :buffers, :files | list |
{count}Ctrl+g | 1Ctrl+g: print current file name with full path 2Ctrl+g: currentbuffer number is printed also |
g Ctrl+g | print cursor position in 5 ways: Column, Line, Word, Character and Byte. |
{Visual} g Ctrl+g | print Word, Character, Line, and Byte counts of selected text |
Editing switch | |
:view {file} | same as :edit, except the file is opened in readonly mode. |
:file name | set current file name to name. The old name becomes alternate file name. |
:0file | Remove the name of the current buffer. |
:edit[!] | Re-load current file. Useful when the file is modified outside of vim. |
:edit[!] {file} | Edit a new file. |
Ctrl + ^ | Edit alternate file(equivalent to ":e #").By default, the alternate file is the previously edited file |
:{count} Ctrl + ^ | go to a specific buffer |
:edit #[count] | Edit the [count]th buffer |
:b[uffer] [count] | Edit the [count]th buffer |
:enew[!] | edit a new unnamed buffer |
Find and Edit | |
:find[!] {file} | Find {file} in path and then edit |
:{count}find[!] {file} | Find {count}-th {file} in path and then edit |
[count] gf | Edit the [count]-th file in path whose name is under the cursor. If [count] is omitted, just get the first match. The file in opened in current window. |
[count] Ctrl-w gf | Edit the [count]-th file in path whose name is under the cursor. It is opened in a new tab. |
[count] Ctrl-w f | Edit the [count]-th file in path whose name is under the cursor. It is opened in a newly split window in current tab. |
{Visual}[count]gf | selected text is used as filename to search |
[count]gF | Similar to the commands above. If a number follows the filename, after opening the file, cursor would be moved to the specified position. |
{Visual}[count]gF | |
Writing | |
:w[rite][!] | write current buffer to current file. |
:{range}w[rite][!] | write specified lines to current file |
:{range}w[rite][!] {file} | write specified lines (or whole buffer) to {file} |
:{range}w[rite][!] >> | append specified lines to current file |
:{range}w[rite][!] >> {file} | append specified lines to {file} |
:[range]w[rite] !{cmd} | feed specified lines into {cmd} as standard input |
Insert a file | |
:r[ead] [file] | insert file {file} after the cursor |
:{range}r[ead] {name} | insert file {file} below the specified line |
:[range]r[ead] !{cmd} | Execute {cmd} and insert its standard output after the cursor or below the specified line. E.g. :read !ls |
Window manipulation
Command | Explanation |
Ctrl-w s, Ctrl-w Ctrl-s :[N]sp[lit] [file] | Split current window into two horizontally.Option splitbelow controls where to put newly created window. |
Ctrl-w v, Ctrl-w Ctrl-v :[N]vsp[lit] [file] | Split current window into two vertically. Option splitright controls where to put newly created window. |
Ctrl-w n, Ctrl-w Ctrl-n :[N]new [file] | Create a new window with an empty file in it or with the specified file. split horizontally |
:[N]vnew [file] | Create a new window with an empty file in it or with the specified file. split vertically. |
:[N]sv[iew] {file} | same as :split, but set 'readonly' option |
:[N]sf[find] {file} | same as :split, search for {file} in path. |
Ctrl-w Ctrl-^, Ctrl-w ^ | Does ":split #". Split window into two and edit alternate file. When a count is given, it is equivalent to ":split #N". Split and edit buffer N |
Explictly set position of window | |
:vertical {cmd} | Explicitly set position of a newly created window which is generated by invoking {cmd} |
:leftabove {cmd} :aboveleft {cmd} | |
:rightbelow {cmd} :belowright {cmd} | |
:topleft | |
:botright | |
Cursor movement and window movement | |
Ctrl-w k Ctrl-w j Ctrl-w h Ctrl-w l | Move cursor to Nth window above current one. Move cursor to Nth window below current one Move cursor to Nth window left of current one Move cursor to Nth window right of current one |
Ctrl-w w Ctrl-w W Ctrl-w t Ctrl-w b Ctrl-w p | move cursor to window below/right of current one. move cursor to window above/left of current one. move cursor to top-left window move cursor to bottom-right window move cursor to previous window |
Ctrl-w K Ctrl-w J Ctrl-w H Ctrl-w L | move window to be at the very top, with full width of screen at the very bottom, with full width of screen at the far left, with full height of screen at the far right, with full height of screen |
Ctrl-w r Ctrl-w R | rotate windows downwards/rightwards upwards, leftwards |
Ctrl-w x | exchange current window with next one or with Nth window if count is given. The two exchanged windows must be located in the same row or column. |
Ctrl-w T | Move current window to a new tab |
Buffer
A buffer is a file loaded into memory for editing.
Unlisted buffer: It means it exists, but it is not listed in buffer list.
active buffer: (1) displayed in a window (2) loaded
hidden buffer: (1) is not displayed in a window, (2) loaded into memory
inactive buffer:(1) is not displayed (2) not loaded (3) Options are remembered.
unlisted buffer: some buffers are not listed in buffer list. when you apply :bdelete or :bwipeout to a buffer, it would be deleted from buffer list.
hidden buffer -> unhidden buffer: start to edit it with any command or delete it with :bdelete command.
:q[uit] [!] quit current window. The buffer becomes inactive.
For following three commands, modified buffers are never abandoned (never becomes inactive).
:close [!] close current window. (1) if 'hidden' is set, the buffer becomes hidden (2) else abandon it (becomes inactive).
:hid[e] Quite current window and state of the buffer depends on option bufhidden.
:only[!] Make the current window the only one in current tab. All other windows are closed.
Direct buffer editing command
# Remove association between a buffer and a file. The buffer is now just a buffer without any content.
:bd[elete][!] [N] Unload buffer [N] (default: current buffer) and delete it from buffer list. :bdelete[!] {bufname} Unload and delete buffer corresponding to {bufname} :bdelete[!] N1 N2 ... Unload and delete buffer N1, N2, ... :N,Mbdelete[!] Unload and delete all buffers in range N to M
# The buffer is removed thoroughly which means the buffer does not exist any more.
:[N]bwipout[!] Like :bdelete, but really delete the buffer. :bw[ipeout][!] {bufname} Everything releated to the buffer is lost. :N,Mbw[ipeout][!] :bw[ipeout][!] N1 N2 ...
# memory is freed. When you switch to that buffer later, the content would be read from the file.
:[N]bun[load][!] The memory allocated for those specified :bun[load][!] [N] buffers is freed. But the buffers remain in the :bunload[!]{bufname} buffer list. :N,Mbunload[!] :bunload[!] N1 N2 ...
Important options:
hidden: off - a buffer is unloaded when it is abandoned.
When (1) the buffer is modified and
(2) 'autowrite' is off or writing is not possible
(3) the '!' flag was used.
a buffer becomes hidden even if hidden option is off.
on - a buffer becomes hidden when it is abandoned.
bufhidden: what happens when a buffer becomes hidden.
<empty> - follow hidden option
hide - the buffer becomes hidden (not unloaded)
unload - unload the buffer
delete - delete the buffer from buffer list (like using :bdelete)
wipe - wipe out buffer from buffer list (like using :bwipeout)
When "unload", "delete" or "wipe" is used, changes in a buffer are load without any warning.
Usually, if option hidden is on, the operations of buffer manipulation commands depend on option bufhidden.
If option hidden is off, buffer is unloaded
A list of buffer/arg switch command from vim help doc:
args list buffer list meaning ~ 1. :[N]argument [N] 11. :[N]buffer [N] to arg/buf N 2. :[N]next [file ..] 12. :[N]bnext [N] to Nth next arg/buf 3. :[N]Next [N] 13. :[N]bNext [N] to Nth previous arg/buf 4. :[N]previous [N] 14. :[N]bprevious [N] to Nth previous arg/buf 5. :rewind / :first 15. :brewind / :bfirst to first arg/buf 6. :last 16. :blast to last arg/buf 7. :all 17. :ball edit all args/buffers 18. :unhide edit all loaded buffers 19. :[N]bmod [N] to Nth modified buf split & args list split & buffer list meaning ~ 21. :[N]sargument [N] 31. :[N]sbuffer [N] split + to arg/buf N 22. :[N]snext [file ..] 32. :[N]sbnext [N] split + to Nth next arg/buf 23. :[N]sNext [N] 33. :[N]sbNext [N] split + to Nth previous arg/buf 24. :[N]sprevious [N] 34. :[N]sbprevious [N] split + to Nth previous arg/buf 25. :srewind / :sfirst 35. :sbrewind / :sbfirst split + to first arg/buf 26. :slast 36. :sblast split + to last arg/buf 27. :sall 37. :sball edit all args/buffers 38. :sunhide edit all loaded buffers 39. :[N]sbmod [N] split + to Nth modified buf 40. :args list of arguments 41. :buffers list of buffers
More Misc. commands
:scriptnames a list of sourced script files in the order they were first sourced.
:verbose set [option]
:verbose map
:echo g:colors_name
:Ctrl-w T
:read !{cmd}
:tab {cmd} execute the external command and open the result file in a new tab. E.g. :tab split
:tab split filename
:tabedit filename
:tab ball open all buffers, each in a separate tab page.
:tabnew |r !dir execute command dir and redirect its output to a file opened in a new tab page.
:tabnew |:E
:echo functionname(argument) call an internal function and returns the output
:call functionname(argument) call an internal function and returned value is discarded.
:exe string_expr execute the commands contained in the string in Ex mode.
Windows file association
For example, if you want to associate .txt files with vim, use following commands:
ftype VIM="e:\program files\vim\vim72\gvim.exe" --remote-tab-silent "%1" assoc .txt=VIMReplace the path with your own vim installation directory..
If you want to maximize the window, use
ftype VIM="e:\program files\vim\vim72\gvim.exe" -c "simalt ~x" --remote-tab-silent "%1"