Saturday, August 7, 2010

Vim Tips

SkyHi @ Saturday, August 07, 2010
Tab operations

Vim tips: Using tabs
1.Open several files in tabs

vim -p [filename] [filename] ...

2.Open a new tab in vim

:tabnew [filename]
:tabe [filename]

3.Moving among different tabs

:tabn //previous tab
:tabp //next tab
:tabfirst //first tab
:tabfir //first tab
:tablast //last tab
gt //next tab
Ctrl+Page Dn/Up //works when you have only one "terminal tab" open
//It works too in gvim

4.Reposition tab

:tabm //move this tab to the last position
:tabm n //move this tab to the n-th position

File Explorer or File Manager

1. Vim built-in file explorer

2. VimExplorer (highly recommended!)
:VE //open VimExplorer
//to the parent in the file tree
//open the file using its associated default application
e //open selected file in a new tab
;c //start a shell from current path.
;e //start another file manager(nautilus,konquer)
dd //delete file under cursor
+f //create a new file.
+d //create a new directory.
m{a-z} //put current path to register(a-z).
'{a-z} //jump to path in register(a-z).
J //view path in register.

3. The NERD tree (highly recommended!)
:NERDTree //open the NERD tree
o.......Open selected file, or expand selected dir
go......Open selected file, but leave cursor in the NERDTree
t.......Open selected node in a new tab
T.......Same as 't' but keep the focus on the current tab
...Open selected file in a split window
g..Same as , but leave the cursor on the NERDTree
!.......Execute the current file
O.......Recursively open the selected directory
x.......Close the current nodes parent
X.......Recursively close all children of the current node
e.......Open a netrw for the current dir

4. vtreexplorer

Latex Editing

A (very) quick introduction to Latex-Suite
1. Debian has the package "vim-latexsuite", and the package installs files in system directory
/usr/share/vim/addons. I choose not to install the package but to download vim-latex and
put it to my ~/.vim folder.

2. Compile latex

3. View generated dvi file

4. Select dvi file viewer: add the following to .vimrc
let g:Tex_ViewRule_dvi = 'xdvi'

Python IDE

1. Python with a modular IDE (Vim)

Java IDE

1. Vim JDE
2. Vim JDE Manual

C/C++ Coding IDE

1. c.vim : C/C++-IDE

2. vim and linux CodingStyle
set noexpandtab                " use tabse, not spaces
set tabstop=8 " tabstops of 8
set shiftwidth=8 " indents of 8
set textwidth=78 " screen in 80 columns wide, wrap at 78
set autoindent smartindent " turn on auto/smart indenting
set smarttab " make and smarter
set backspace=eol,start,indent
" allow backspacing over indent, eol, & start

3.Fast Code Block Formatting
format the whole file

format code block: visual select, then "=="

4. Exuberant Ctags
ctag *.c *.h  // generate tag file
ctags -R // recursively
:set tags=/path/tags
ctrl + ] // jump into the tag
ctrl + t // jump back

5. Cscope
The Vim/Cscope tutorial
Using Cscope on large projects
Vim documentation: if_cscop
cscope -Rbq
:cs add /path/cscope.out

cs find c|d|e|f|g|i|s|t name
0 or s
1 or g find definition
2 or d find callee
3 or c find caller
4 or t find string
6 or e egrep mode
7 or f find this file
8 or i find files that contain this file

6. Vim Taglist plugin (highly recommended!)
//show the complete info for the item under cursor
//move to code

By default, the tag list will be sorted by the order in which the tags
appear in the file. You can sort the tags either by name or by order by
pressing the "s" key in the tag explorer window.

7. CRefVim: a C-reference manual especially designed for Vim
  cr normal mode:  get help for word under cursor
Memory aid cr: (c)-(r)eference
cr visual mode: get help for visually selected text
Memory aid cr: (c)-(r)eference
cw: prompt for word CRefVim should search for
Memory aid cw: (c)-reference (w)hat
cc: jump to table of contents of the C-reference manual
Memory aid cc: (c)-reference (c)ontents

8. OmniCppComplete: C/C++ omni-completion with ctags database

9. SuperTab continued: Do all your insert-mode completion with Tab.

10. Use sessions to save state of work
vim -S session.vim'
:mks[ession][!] '

11. minibufexpl.vim: Elegant buffer explorer - takes very little screen space
bf //go to the first buffer
bp //go to the previous buffer
bn //go to the next buffer
b[num] //go to the num-th buffer
bd[num] //remove the num-th buffer

12. a.vim: Alternate Files quickly (.c --> .h etc)
:A switches to the header file corresponding to
the current file being edited (or vise versa)
:AS splits and switches
:AV vertical splits and switches
:AT new tab and switches
:AN cycles through matches

13. Automatic folding (add the following in .vimrc)
set foldmethod=syntax

command in vim
zr //unfold one level
zR //unfold all levels
zm //fold one level
zM //fold all levels

14. QuickFix Window
:cw //open
:cn //next item
:cp //previous item

15. Visual Mark: Visual mark, similar to UltraEdit's bookmark

(1) For gvim, use "Ctrl + F2" to toggle a visual mark.
For both vim and gvim, use "mm" to toggle a visual mark.
(2) Use "F2" to navigate through the visual marks forward in the file.
(3) Use "Shift + F2" to navigate backwards.

16. Organize/Navigate projects of files

17. Manage Sessions in Vim

Debugging in Vim

1. Clewn and vimGdb (highly recommended!)
I prefer using vimGdb although it requires you to patch vim source
and compile. In order to use the key mappings provided by vimGdb, in your .vimrc
file, add
source /home/username/.vim/macros/gdb_mappings.vim

or in vim session
run /home/username/.vim/macros/gdb_mappings.vim

List of key mappings in 'gdb_mappings.vim':
 launch the interactive gdb input-line window
CTRL-Z send an interrupt to GDB and the program it is running
B info breakpoints
L info locals
A info args
S step
I stepi
CTRL-N next: next source line, skipping all function calls
X nexti
F finish
R run
Q quit
C continue
W where
CTRL-U up: go up one frame
CTRL-D down: go down one frame
CTRL-B set a breakpoint on the line where the cursor is located
CTRL-E clear all breakpoints on the line where the cursor is located
CTRL-P Normal mode: print value of word under cursor
Visual mode: GDB command "createvar" selected expression
CTRL-X print value of data referenced by word under cursor

In gdb window, you need to first "file [name of the executable]" to enable
the debugging.

Other tips

1. Your personal vim setup is in ~/.vimrc and ~/.vim/...
System vim files are in /usr/share/vim

2. Copy & Paste between different sessions
"*yy //copy the current line to the clipboard
//you can use visual mode (ESC,v) to choose several lines

"*p //put whatever is on the clipboard in the document.

3. Select All
ggVG  //select all

4. Convert code to HTML