You are on page 1of 4

Vim: Tips and tricks

1
1.1

Issues related to new Vim 7.0
Parenthesis matching

2
2.1

Regarding GVim
A ‘clean’ GVim

When your cursor in on a parenthesis, the matching parenthesis (“the other side”) will be highlighted. This may annoy some of you. In order to disable this feature, open your vimrc (located in your home directory, ~/.vimrc in Unix and N:\ vimrc in Windows) and add this line: let loaded matchparen = 1 Restart your Vim (or GVim) to see the effect.

One of the good things about GVim is that the most common tasks are listed on the menu so you can learn quickly. After that you may want to claim back the area, eliminate the menu and toolbar. (Why not Vim? GVim has more colours to choose from, that’s why — at least to me) Just add this line to your vimrc: set guioptions=g

1.2

Starting from 7.0, you will see a menu showing a list of         candidates when you press ØÙ Ctrl  + ØÙ N  or ØÙ Ctrl  + ØÙ P  by default. If you find this annoying then turn it off by adding this line to the vimrc: set completeopt= See more about completion by :help completion.

Keyword completion — disable the menu

2.2

Fonts, fonts,

fonts, ...

Choose a font in the GVim using :set guifont=* Once you’re satisfied with the font, show the font name with :set guifont, which will return something like guifont=Monospace 8 You have to escape spaces and commas that might occur in the string using backslash. In this case you should add this line to your vimrc: set guifont=Monospace\ 8

1.3

Tab editing

2.3

Instead of typing :edit new.file (:e in short) to edit another file, a new option :tabedit new.file (:tabe) is available. This creates tabs, like those used in browser.     Navigate though the tabs using ØÙ Ctrl  + ØÙ PgUp  and     ØÙ Ctrl  + ØÙ PgDn  (If your terminal processed these key bind        ings, you can still use ØÙ g  ØÙ t  and ØÙ g  ØÙ T  — though that’s not as convenient). Advantage of using tabs over buffer list is that you switch between files using shortcut keys (instead of :bn<ENTER> and :bN<ENTER>). Also, name of the file is shown clearly on the tab, so you know what you are doing. (though you lose one line) Combining this command and window splitting (:new, :vnew, :split, :vsplit, ...) gives you the most flexibility.

3

In GVim, you can do copy and paste to the clipboard by             ØÙ "  ØÙ +  ØÙ y  and ØÙ "  ØÙ +  ØÙ p  , respectively.

Clipboard

Other bits

3.1

Paste Mode

1.4

Spell checking

Have you ever tried pasting something into Vim and end up with badly aligned result? Here’s the trick: type :set paste to put Vim in paste mode before you paste, so Vim will not do anything fancy and paste all the stuff verbatim. After you have finished pasting, type :set nopaste to go back to normal, where indentation will take place again. (You only need this option when pasting in terminal, but not GUI)

To enable spell checking, type the following: :setl spell spl=en All regions (default) :setl spell spl=en gb,en us GB, USA         Move around misspelled words using ØÙ ]  ØÙ s  and ØÙ [  ØÙ s  . To turn it off: :setl spell spl= Of course, putting these as a keyboard macro will be more convenient. Add this to your vimrc: set spell spl=en us " Select language set nospell " Turn it off at start nmap <F7> :set spell! "toggle spell check       In insert mode, ØÙ Ctrl  + ØÙ X  ØÙ S  gives you spell sugges    tion(s), and you can press ØÙ Ctrl  + ØÙ N  for next suggestion. For more information, :help spell.

3.2

Copying from Terminal

Use :set nonu to disable line numbering before you copy lines of code to the clipboard. Restore by :set nu.

3.3 

   • You might know ØÙ /  and ØÙ ?  for searching. How     about ØÙ *  and ØÙ #  ? They search for word nearest to the cursor forward and backward, respectively. They save you time typing the word to search.         • Use ØÙ n  instead of ØÙ /  ØÙ ENTER  to repeat search ( ØÙ N  do it backwards).

Searching

• Use :noh to cancel highlights after searching.

Note that it uses environment variables CFLAGS and CXXFLAGS as default flags. you can fold up codes that you are not interested.15     • Undo by ØÙ u  .10 Visual mode Macros are (one of the) most powerful feature in Vim. type :set fdm=indent (or add it to vimrc). Redo and Repeat . Add this line to vimrc au FileType cpp. type         ØÙ g  ØÙ g  ØÙ =  ØÙ G  (go to the top. you can invoke it using mouse drag. Combining this with :make you don’t need to exit Vim every time you compile and debug your program. Moreover. Then you can fold/unfold the code at that in        dent level using ØÙ z  ØÙ c  and ØÙ z  ØÙ o  . respectively. cindent and smartindent. ØÙ Ctrl  + ØÙ V  is for block visual mode. Here fdm means “fold method”.g. This will be the name of the macro. It can match ().   Three visual modes are available.’> automatically and you don’t need to remove them.9 Make C. namely autoindent. to make sure you don’t miss any warnings and have your program ready for debugging. Macros 3.5   ØÙ K  shows you the man page for the keyword under cursor.12 Indenting code 3. End record  ing a macro by pressing ØÙ q  again. ØÙ V  is for line visual mode. where c can be any alphabets. E.4 3.cshrc (for csh).     • ØÙ %  match parenthesis.     using a mouse you do double click and drag. and ØÙ g  ØÙ J  do the same without inserting a space. 3.   Why ØÙ z  ? According to the help. To indent the whole file.vim.14 3. preceding with a number changes its meaning:     • ØÙ G  send you to the bottom of file.bashrc (for bash). In case of any warnings or errors it will take you there. then indent until the end of file. []. You can         open and close a fold by ØÙ z  ØÙ o  and ØÙ z  ØÙ c  . Any keys pressed after that will be recorded. Instead of splitting your window to view source code from different part of the program.6       ØÙ J  join the current line with the next. ØÙ 8  ØÙ p  paste 8 times. z is like a folded piece of paper. Man page Dictionary Lookup Add this line to your vimrc: nmap <F2> :!webster <C-R><C-W><CR>   Dictionary shows up when you press ØÙ F2  in normal mode. Using numbers 3. To fold some part of your code. You can display subsequent warnings and errors after a make by typing :cn (previous by :cN). You can also ask Vimto fold the code based on the indentation of your code. Using macros can increase productivity a lot. saving your time in doing some repetitive work.c setl mp=make\ %:t:r then you can make your C or C++ programs in Vim using :make.g. 3. Note that when you type ØÙ :  it will insert ’<. the unit becomes line instead of character. undo the whole line by ØÙ U  . {}. or export CFLAGS="-Wall -g" export CXXFLAGS=${CFLAGS} to your . 3. A fold will be represented by /*{{{*/ and /*}}}*/.11 Code folding 3. 3.   ØÙ %  is used to match parenthesis. So add set CFLAGS="-Wall -g" set CXXFLAGS=($CFLAGS) to your .13 You can precede most of the commands with a number     indicating the number of times to repeat. To adjust indentation (e. ØÙ v  is for normal visual   mode.8 Matching parenthesis 3. then type   :fo. you can     fold/unfold codes of the same indent level by ØÙ z  ØÙ m  (fold     more) and ØÙ z  ØÙ r  (fold less). while n ØÙ %  send you to the n ’th percentile of the file. A triple click (yes!) and drag invoke this.• :set is turns on incremental search. Execute the macro   you recorded by ØÙ @  c .7 Executing commands :! command executes command as if it is typed under the shell. An example to this is located in $VIMRUNTIME/vimrc example. Joining line(s) 3. in case of copy & paste). just create a selection using the visual mode. Be  gin recording a macro by pressing ØÙ q  c . while n ØÙ G  send you to the n ’th line. Undo. In this case you can fold your code without adding the fold mark. comment (/* */) and #define. To achieve this.     use number of lines ØÙ =  ØÙ =  . one of the features that differentiate itself from other editors. C++ programs in Vim There are few options related to indentation. But for some commands.

ØÙ <  } Sometimes you have constants in your program that you     want to modify. tabs are converted to spaces :set expandtab ’ Replace tabs with white spaces :ret ’ Save and do your printing. – g Replace all occurrence in the line. Precede the command with a number (e. This causes printouts hard to read when you have more than a few level of indentation. brings you away from where you are working. Create a tag file using ctags: ctags *. when you define the function multiple times).21 Jump to declaration 3. This saves the loca  tion in mark c .c ’ With noexpandtab.g. tab is equal to 8 spaces when printed. . – ^ matches beginning of line. – red\|blue matches both red and blue.    • Redo by ØÙ Ctrl  + ØÙ R  . This can be a sequence of actions you do in insert mode.   • Perform a search using ØÙ /  pattern . ) to refer back to the string matched (enclosed in \( . Vim accepts regular expression. or simply press the wrong key.     Display the definition of the current variable by ØÙ [  ØÙ i  . . After that you can     use ØÙ Ctrl  + ØÙ ]  to ask for declaration.g.16 Splitting Window 3. The advantage of using this is your cursor don’t have to be right on top of the number. Without this only the first matched string in each line will be replaced. ’ lpr -Popenlab xx. To go back.18 You jump to elsewhere in a file or another file for various reasons. or :ret         ØÙ g  ØÙ d  and ØÙ g  ØÙ D  go to local and global declaration respectively. ØÙ >  . To go forward.0 or above) 3. Searching. 3.’> : range equals to lines highlighted (inserted automatically in visual mode) pattern is the pattern to search for. press ØÙ Ctrl  + ØÙ I  . press ØÙ m  c . Cycle through the list of files using :bn and :bN. Retab is the solution. – c Confirm each substitution. Combining split and edit command.     Move to the adjacent windows using ØÙ Ctrl  + ØÙ W  and then cursor key. string is the string to replace with. Constant manipulation 3. substitution is not done. etc. . (Go     back using ØÙ Ctrl  + ØÙ O  ) Note that these function is not guaranteed to work as it may bring you to the wrong place (e. recognize decimal numbers. \2. You can use backreference (\1. Split a window vertically using :vs (vertical split). spaces are converted to tabs :set noexpandtab ’ Undo changes to your code using ’u’. Place the cursor on a variable or a function and these commands will bring you to the declaration. we have :new and :vnew.h The tag file defaults to tags. it just search first number after the cursor on the same line and do the job. by default. press         ØÙ Ctrl  + ØÙ O  (‘oh’). – gr[ae]y matches both gray and grey.c *. . $ matches end of line. (version 7.19 Split a window horizontally using :sp (split).  repeats the last action.   • ØÙ . To go back to that location press ØÙ ‘  c . The shortcut ØÙ Ctrl  + ØÙ A  add one to the     number and ØÙ Ctrl  + ØÙ X  subtract one from the number. You can mix some of them. – :%s/ \([4-9]\)/\1th/g replaces 4 with 4th. – n Just report the number of matches. \)). Go back by         ØÙ Ctrl  + ØÙ T  . 5 with 5th. – a. ctags is more powerful than ØÙ g  ØÙ d  and .20 Retabbing for printing Search and Replace • Perform a replace using : r s/ pattern / string / flags where r defines the range – % : the whole file By default. 3. ØÙ .b : from line a to line b – ’<. Perform the following: ’ Specify how much spaces one tab is equivalent ’ to (Not required if you are happy with the ’ current settings) :set ts=4 ’ With expandtab. e. :10sp) to control the height (or width) of the first window. 0 for octal numbers and 0x for hexadecimal numbers. . Include \c in search pattern to ignore case in search. Remove a window using :q.17 Navigation and Marks 3.g. Adjust the             size of the window using ØÙ Ctrl  + ØÙ W  { ØÙ +  . This function.22 Using ctags ctags is a powerful utility help to analyze the code and help the programming process. . flags modifies the behavior of a search.   To mark a position.

. . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 2 Regarding GVim 2. 3. . • Use :help. . . .19 Constant manipulation . . . :help ts should tell you what is mean by ts (in your vimrc). .4 Spell checking . (But remember. . . . . . . . . 2006. . . . . . . . 3. . . . . . . . . . . . on the internet. . . . . . 3. . . You can incorporate the update in your Makefile to simplify the work. . . 3. . . . . .14 Macros . . . . . . . . This is useful to identify multiple definition. . . . . . . . . . 3. . . .7 Executing commands . . 2009. In particular. . .9 Make C. 3. For example. . . . .16 Splitting Window . .0 1. . .    ØÙ g  ØÙ D  . . . . . . . .18 Search and Replace . . . . . .. . . . . . .2 Copying from Terminal 3.. . Last modified: February 11. . . .       ØÙ g  ØÙ Ctrl  + ØÙ ]  shows a list of matching tags. . . . 3.. . . Scripts for eye-catching colour schemes. 3. . . . . fonts. .4 Man page . . . . . . . . try the following: • Ask vimtutor if you are new to Vim. . . . . . . . . .. Search. . but requires update if code has changed. .vim. .. . . . 3. . . . . . . . . .2 Keyword completion — disable the 1. Navigate your        self by using the cursor keys ( ØÙ h  ØÙ j  ØÙ k  ØÙ l  ). . . . . . . .21 Jump to declaration . . . . . . 3. . . . . . .1 A ‘clean’ GVim .1 Parenthesis matching .. . . . .3 Tab editing . What have been covered is just a very small portion of tips and tricks. 1. syntax highlighting for other languages are few examples. . . 3 Other bits 3. . . .13 Using numbers . . . . . . . . fonts. . . . . . Help! How do I. follow a link (enclosed in pipes. menu . . |link| for example) us        ing ØÙ Ctrl  + ØÙ ]  . . . .3 Clipboard . . . . . . . . . . . . . . . C++ programs 3. . . . . .17 Navigation and Marks . . . . . . . . . . . . .. . If you like you can map     ØÙ Ctrl  + ØÙ ]  to this in your vimrc: nmap ^] g^] A nice tutorial on ctags can be found at: http://www. .20 Retabbing for printing .6 Joining line(s) ..1 Paste Mode . 3. . . . . . 2.15 Undo. . . . .. . . on www. . . First edited: October 6. . . . learn through practice) • As mentioned.linuxjournal. 3. . . . . . . . . 3. . . . . . 3. in Vim . . This should return you some html formatted reference as well as prettilyformatted pdf which is nice for printouts. . . . . . . . . . . . . . . . . . . . .. . . . . 1. . .1 Finally. . . . . 3. . . .11 Code folding . . . . . . . 4 Finally. using the term "vim quick reference card". . . . . . . . . . . . . . Wander around the help should increase your skill in using Vim. .8 Matching parenthesis . . 3. . . 4 4. . . . . 3. . . . . . . . . . . . . . . . . . . . . . . . If you have other problems concerning Vim. . . 2. . . . . . . . A graphical cheat sheet is also available ("vim graphical cheat sheet"). 4. . . . A good tool for analysing code. . Redo and Repeat 3. . . . . .12 Indenting code . . . . . .10 Visual mode . . . . • A quick reference card might help get you started. . . . . . . . . . .org there are lots of tips and scripts. . . .com/article/8289 Contents 1 Issues related to new Vim 7. . . . . .1 Help! How do I. . . . .3 Searching . . . . . . . . . . . .5 Dictionary Lookup . . . . . . . . .2 Fonts. . . . go back by ØÙ Ctrl  + ØÙ T  . internet is a good place to look for help. . . 3. . .22 Using ctags . .