Found a great reference card at http://michaelgoerz.net/refcards/vimlatexqrc.pdf. I made a picture out of it so that you can direct read it.
Thursday, May 03, 2012
Sunday, April 29, 2012
Wrap acronyms with \ac in Vim
:s/\(\W\)\(\u\{2,10\}\)\(\W\)/\1\\ac{\2}\3/g :s/\(\W\)\(\u\{2,10\}\)$/\1\\ac{\2}/g :s/^\(\u\{2,10\}\)\(\W\)/\\ac{\1}\2/g :s/^\(\u\{2,10\}\)$/\\ac{\1}/g
Thursday, October 27, 2011
Add "Edit with Vim" item to context-menu in Windows for Vim
This post shows how to add "Edit with Vim" and "Tab Edit with Vim" items to context menu (pop out when you right click a file) in Windows.
Run regedit.exe, go to HKEY_LOCAL_MACHINE/SOFTWARE/Classes/*/shell/
- Create new key "Tab Edit with &Vim" (Right click parent entry -> New -> Key)
- Create new key "Edit with Vim"
- Create new key "command" under "Tab Edit with &Vim"
- Edit the entry with name "(Default)", change its data to
<vim_dir>\gvim.exe" -p --remote-tab-silent "%1" "%*" - Create new key "command" under "Edit with Vim".
- Edit the entry with name "(Default)", change its data to
<vim_dir>\gvim.exe "%1"
It seems that latest versions of vim automatically create the registry entry:
- HKEY_LOCAL_MACHINE\SOFTWARE\Vim\Gvim and the data of entry path points to the vim executable.
- HKEY_CLASSES_ROOT\*\shellex\ContextmenuHandlers\gvim. Data of the entry "(Default)" is the CLSID (51EEE242-AD87-11d3-9C1E-0090278BBD99).
You can delete this entry because we have already added menu item "Edit with Vim". - HKEY_CLASSES_ROOT\Applications\gvim.exe\shell\edit\command. Data of the entry "(Default)" is "<vim_dir>\gvim.exe" "%1"
- HKEY_CLASSES_ROOT\CLSID\{51EEE242-AD87-11d3-9C1E-0090278BBD99}
It has a key named InProcServer32. The data of "(Default)" is <vim_dir>\gvimext.dll - Some other keys
Saturday, October 08, 2011
Make vim-latex to generate output in a specified directory
Recently I started to use latex to write papers. I want to edit latex in my favorite editor - vim. I found the project vim-latex: http://vim-latex.sourceforge.net/. It is powerful and convenient to use.
However, one feature I want is to generate output files (.div, .ps, .log, etc) into a separate directory rather than the same directory as tex files. It turns out that vim-latex does not support it natively. So I hacked into the source code to make it work on Windows.
-
Edit file ~/vimfiles/ftplugin/tex.vim
Add following config:
set iskeyword+=: let g:Tex_Outdir='out' let g:Tex_ViewRule_pdf='"Foxit Reader.exe" ' let g:Tex_CompileRule_dvi='mkdir '.g:Tex_Outdir.' & latex -output-directory='.g:Tex_Outdir.' -src-specials --interaction=nonstopmode $*' let g:Tex_CompileRule_pdf=g:Tex_CompileRule_dvi.' & cd '.g:Tex_Outdir.' & dvipdfm $*.dvi'
Basically above config specifies
- output directory of latex compilation
- How to view PDF files (if you don't give full path, the command needs to be in env variable PATH)
- How to compile dvi: create the output directory and put output there
- How to compile pdf: first compile tex to dvi, and then call dvipdfm to generate pdf
-
Change ~/vimfiles/ftplugin/latex-suite/compiler.vim
Change line 252 to (this line adds the full path of output directory):
let execString = 'start '.s:viewer.' "'.expand('%:p:h').'/'.g:Tex_Outdir.'/$*.'.s:target.'"'Change line 405 to (this line adds the full path of output directory):
Note: the text in blue is what I added (diff against the original code)let execString = 'silent! !'.viewer.' "'.expand('%:p:h').'/'.g:Tex_Outdir.'/'.mainfnameRoot.'.'.s:target.'" '.line('.').' "'.expand('%').'"'
Friday, December 17, 2010
vim quickfix and location list
| Commands | Description |
| copen | open quickfix window |
| cclose | close quickfix window |
| cwindow | open quickfix window if its content is not empty. |
| cc [nr] | display error [nr] |
| cr | display the first error. |
| cfirst | display the first error. |
| clast | display the last error. |
| [count]cn | display [count] next error |
| [count]cp | display [count] previous error |
| [count]cnf | display first error in the [count] next file |
| [count]cpf | display first error in the [count] previous file |
For commands related to location list, just replace first 'c' with 'l' in above commands.
Thursday, December 16, 2010
Compile and Install vim on Linux
Commands
wget ftp://ftp.vim.org/pub/vim/unix/vim-7.3.tar.bz2
tar jvxf vim-7.3.tar.bz2
cd vim73
./configure --enable-gui=no \
--enable-multibyte \
--enable-cscope \
--disable-netbeans \
--prefix=<your_desired_vim_home>
make
make test
make install
Change you environment variable PATH to add vim bin path. Following commands are for bash.
Temporary change
export PATH="<your_vim_home>/bin;$PATH"
Permanent change
echo -e '\nexport PATH="<your_vim_home>/bin;$PATH" ' >> ~/.bash_profile
Friday, January 30, 2009
Vim advanced topics
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"
Monday, January 26, 2009
More VIM text editing command
The more I use VIM, the more it surprises me.
Here, I list some useful commands in everyday text editing. I have known some of them for long time. But others were learnt recently.
| Command | Explanation | Note |
| Delete | ||
| cc | delete current lines and change to insert mode | |
| dd | delete current lines | |
| c{motion} | delete until the character {motion} moves to and change to insert mode. | |
| d{motion} | delete until the character {motion} moves to | |
| C | delete characters until the end of the line. Then change to insert mode | |
| D | delete characters until the end of the line. | |
| s | delete [count] characters and change to insert mode | |
| x | delete [count] characters under and after the cursor | |
| :[range]d | delete those lines in the range | |
| S | delete [count] lines and change to insert mode | |
| Replace | ||
| R | Enter Replace mode | |
| gR | ||
| Line joining | ||
| [range]J | join [count] lines | Steps of execution of these commands |
| {Visual}J | join the selected lines | |
| :[range]j[!] | join range lines. With !, the join doesn't insert/remove any spaces. | |
| [range]gJ | join [count] lines | Don't insert or delete any spaces. |
| {VIsual}gJ | join the selected lines | |
Some commands useful in source code editing
| Commands | Explanation |
| <{motion} | shift covered lines leftwards by 1 shiftwidth |
| >{motion} | shift covered lines rightwards by 1 shiftwidth |
| {Visual}[count]< | shift selected lines leftwards by 1 shiftwidth |
| {Visual}[count]> | shift selected lines rightwards by 1 shiftwidth |
| [range]< | shift selected lines leftwards by 1 shiftwidth |
| [range]> | shift selected lines rightwards by 1 shiftwidth |
| << | shit current line leftwards by 1 shiftwidth |
| >> | shit current line rightwards by 1 shiftwidth |
Text formatting in VIM
How to set options for VIM?
Examples:
| Setting | Explanation |
| set paste | set options for commands that don't need parameters. |
| set filetype=java | Directly set options for commands that accept parameters. |
| set guioptions-=option | Remove a specific option from the configuration of a command. |
| set guioptions+=option | Add a specific option to the configuration of a command. |
Basic concept
Sentence/Paragraph/Section: http://www.vim.org/htmldoc/motion.html#sentence
Text format
http://www.vim.org/htmldoc/change.html#formatting
Alignment
| Setting | Explanation | Examples |
| :[range]ce[nter] [width] | center lines | :.,+3 center 80 |
| :[range]ri[ght] [width] | right alignment | :% right |
| :[range]le[ft] [indent] | left alignment.(Unit of identation is space) | :left 4 |
Format
| Command | Explanation | Examples | Note |
| gq{motion} | Format lines that {motion} moves over. Cursor is put where {motion} moves to | gqap format a paragraph | Motion: http://www.vim.org/ htmldoc/motion.html |
| gw{motion} | Format lines that {motion} moves over. Put cursor back to the original position. | gwj | |
| gqgq gqq | Format current line | ||
| {Visual}gq | Format highlighted text |
How to control formatting setting of gq command?
Formatting does not change empty lines, but it changes lines only containing white spaces.
(1) textwidth
This option controls length of every formatted line.
If option textwidth is 0, the formatted line length is the screen width (max value is 79).
(2) autoindent
If this option is on, Vim uses indent of the first line for the following lines.
(3) joinspaces (boolean)
Used when lines are joined together. By default, it is on. Use set nojoinspaces to turn it off.
(4) formatprg
Set an external program to format text. In this case, the textwidth and other options have no effect on text formatting.
(5) formatoptions
Most useful options:
t: Auto-wrap text using textwidth. (does not apply to comments)
c: Auto-wrap commens using textwidth.
q: Allow formatting of comments with gq command. When using gq command in comments, blank lines and lines only with comment leaders and white spaces are considered as paragraph delimiters.
r: auto insert comment leader after hitting 'Enter' in Insert mode.
o: auto insert comment leader after hitting 'o' or 'O'.
a: auto format paragraphs when text is inserted or deleted.
w: A trailing non white space ends a paragraph.
Note: Formatting would be applied anyway no matter whether the paragraphs are delimited correctly.
You can use set formatoptions-=a to disable automatic formatting.
More options: http://www.vim.org/htmldoc/change.html#fo-table
(6) comments
This option controls how to format comments (usually in source code).
Default value is
"s1:/*,mb:*,ex:*/,://,b:#,:%,:XCOMM,n:>,fb:-"s: start of a three-piece comment
s{digit}: add extra indent to middle part. Unit is space (not tab).
s-{digit}: remove extra indent to middle part. Unit is space (not tab).
m: middle part of a three-piece comment. Middle part is left adjusted with start part of the comment by default. This is controlled by option l.
b: blanks required after the specified string.
e: end of a three-piece comment
x: In C++ comment, just type / to end a comment when middle-comment string is inserted. The space between middle-part and / would be removed automatically.
n: recognize numbered list(see below for details)
More options: http://www.vim.org/htmldoc/change.html#format-comments
My setting is
set textwidth=79 set formatoptions=tcqron
How to format numbered lists?
This sometimes is really tricky.
See this post: http://objectmix.com/editors/332035-vim-how-create-bullet-list-numbered-list.html
Usually, textwidth must be set and formatoptions must include nwt.
Also see option formatlistpat(flp) for how to set list header. That is used by vim to recognize numbered lists.
Saturday, March 15, 2008
Edit binary file in hex mode in vim
It will be nice if we can edit binary file directly in vim. VIM provides basic support which has a few restrictions.
(1) Open a binary file
vim -b datafile
or
:set binary
(2) Many characters are unprintable. You can see the Hex format by using:
:set display=uhex
Or you can use ga command to see the value of current character.
(3) To see current position, use
g CTRL-G
The output is verbose:
Col 6 of 38; Line 31 of 31; Word 94 of 96; Byte 747 of 780
(4) Move to a specific byte offset:
234go
(5) xxd can be used to convert the file into hex dump format
%!xxd
Result should look like this:
0000000: 6262 630a 6465 660a 6768 696b 0aab de0a bbc.def.ghik....
There are two parts: hex part and printable character part.
Go back:
%!xxd -r
Note: only changes in hex part have effect. Changes in printable text part are ignored.
Of course, tool xxd can be used independently in command line.
