0% found this document useful (0 votes)
282 views4 pages

Vim Tips and Trick

Vim provides many tips and tricks for customizing and enhancing your editing experience. Some key tips include disabling parenthesis highlighting, using tabs for easier file navigation, enabling spell checking, using macros for repetitive tasks, and splitting windows for viewing multiple parts of code simultaneously. Vim's extensive set of commands and options allow you to customize your workflow.

Uploaded by

m_goku
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
282 views4 pages

Vim Tips and Trick

Vim provides many tips and tricks for customizing and enhancing your editing experience. Some key tips include disabling parenthesis highlighting, using tabs for easier file navigation, enabling spell checking, using macros for repetitive tasks, and splitting windows for viewing multiple parts of code simultaneously. Vim's extensive set of commands and options allow you to customize your workflow.

Uploaded by

m_goku
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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 eect.

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, thats 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 nd this annoying then turn it o 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 youre satised 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 le, 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 thats not as convenient). Advantage of using tabs over buer list is that you switch between les using shortcut keys (instead of :bn<ENTER> and :bN<ENTER>). Also, name of the le 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 exibility.

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? Heres 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 stu verbatim. After you have nished 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 o: :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.

:set is turns on incremental search.

3.11

Code folding

3.4

3.5

  K  shows you the man page for the keyword under cursor.

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.

3.6

      J  join the current line with the next, and g  J  do the same without inserting a space.

Joining line(s)

3.7

Executing commands

:! command executes command as if it is typed under the shell. Combining this with :make you dont need to exit Vim every time you compile and debug your program.   %  is used to match parenthesis. It can match (), [], {}, comment (/* */) and #define.

Instead of splitting your window to view source code from dierent part of the program, you can fold up codes that you are not interested. To fold some part of your code, just create a selection using the visual mode, then type   :fo. Note that when you type :  it will insert <,> automatically and you dont need to remove them. A fold will be represented by /*{{{*/ and /*}}}*/. You can         open and close a fold by z  o  and z  c  , respectively. You can also ask Vimto fold the code based on the indentation of your code. To achieve this, type :set fdm=indent (or add it to vimrc). Here fdm means fold method. Then you can fold/unfold the code at that in        dent level using z  c  and z  o  . Moreover, you can     fold/unfold codes of the same indent level by z  m  (fold     more) and z  r  (fold less). In this case you can fold your code without adding the fold mark.   Why z  ? According to the help, z is like a folded piece of paper.

3.12

Indenting code

3.8

Matching parenthesis

3.9

Make C, C++ programs in Vim

There are few options related to indentation, namely autoindent, cindent and smartindent. An example to this is located in $VIMRUNTIME/vimrc example.vim. To adjust indentation (e.g. in case of copy & paste),     use number of lines =  =  . To indent the whole le, type         g  g  =  G  (go to the top, then indent until the end of le.

Add this line to vimrc au FileType cpp,c setl mp=make\ %:t:r then you can make your C or C++ programs in Vim using :make. Note that it uses environment variables CFLAGS and CXXFLAGS as default ags. So add set CFLAGS="-Wall -g" set CXXFLAGS=($CFLAGS) to your .cshrc (for csh), or export CFLAGS="-Wall -g" export CXXFLAGS=${CFLAGS} to your .bashrc (for bash), to make sure you dont miss any warnings and have your program ready for debugging. In case of any warnings or errors it will take you there. You can display subsequent warnings and errors after a make by typing :cn (previous by :cN).   Three visual modes are available. v  is for normal visual   mode, you can invoke it using mouse drag. V  is for line visual mode, the unit becomes line instead of character,     using a mouse you do double click and drag. Ctrl  + V  is for block visual mode, one of the features that dierentiate itself from other editors. A triple click (yes!) and drag invoke this.

3.13

You can precede most of the commands with a number     indicating the number of times to repeat. E.g. 8  p  paste 8 times. But for some commands, preceding with a number changes its meaning:     G  send you to the bottom of le, while n G  send you to the n th line.     %  match parenthesis, while n %  send you to the n th percentile of the le.

Using numbers

3.14

3.10

Visual mode

Macros are (one of the) most powerful feature in Vim. Be  gin recording a macro by pressing q  c , where c can be any alphabets. This will be the name of the macro. Any keys pressed after that will be recorded. End record  ing a macro by pressing q  again. Execute the macro   you recorded by @  c . Using macros can increase productivity a lot, saving your time in doing some repetitive work.

Macros

3.15

    Undo by u  , undo the whole line by U  .

Undo, Redo and Repeat

    Redo by Ctrl  + R  .

  .  repeats the last action. This can be a sequence of actions you do in insert mode.

g Replace all occurrence in the line. Without this only the rst matched string in each line will be replaced. c Conrm each substitution. n Just report the number of matches, substitution is not done. (version 7.0 or above)

3.16

Splitting Window 3.19

Split a window horizontally using :sp (split). Split a window vertically using :vs (vertical split). Precede the command with a number (e.g. :10sp) to control the height (or width) of the rst window. Combining split and edit command, we have :new and :vnew. Cycle through the list of les using :bn and :bN.     Move to the adjacent windows using Ctrl  + W  and then cursor key. Remove a window using :q. Adjust the             size of the window using Ctrl  + W  { +  , -  , >  , <  }

Sometimes you have constants in your program that you     want to modify. The shortcut Ctrl  + A  add one to the     number and Ctrl  + X  subtract one from the number. The advantage of using this is your cursor dont have to be right on top of the number, it just search rst number after the cursor on the same line and do the job. This function, by default, recognize decimal numbers, 0 for octal numbers and 0x for hexadecimal numbers.

Constant manipulation

3.17

Navigation and Marks

3.18

You jump to elsewhere in a le or another le for various reasons. Searching, or simply press the wrong key, brings you away from where you are working. To go back, press         Ctrl  + O  (oh). To go forward, press Ctrl  + I  .   To mark a position, press m  c . This saves the loca  tion in mark c . To go back to that location press  c .   Perform a search using /  pattern .

3.20

Retabbing for printing

Search and Replace

Perform a replace using : r s/ pattern / string / ags where r denes the range % : the whole le

By default, tab is equal to 8 spaces when printed. This causes printouts hard to read when you have more than a few level of indentation. Retab is the solution. 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, tabs are converted to spaces :set expandtab Replace tabs with white spaces :ret Save and do your printing, e.g. lpr -Popenlab xx.c With noexpandtab, spaces are converted to tabs :set noexpandtab Undo changes to your code using u, or :ret         g  d  and g  D  go to local and global declaration respectively. Place the cursor on a variable or a function and these commands will bring you to the declaration. (Go     back using Ctrl  + O  ) Note that these function is not guaranteed to work as it may bring you to the wrong place (e.g. when you dene the function multiple times).     Display the denition of the current variable by [  i  .

a,b : from line a to line b <,> : range equals to lines highlighted (inserted automatically in visual mode) pattern is the pattern to search for. Vim accepts regular expression. Include \c in search pattern to ignore case in search. gr[ae]y matches both gray and grey. red\|blue matches both red and blue. ^ matches beginning of line, $ matches end of line. string is the string to replace with. You can use backreference (\1, \2, . . . ) to refer back to the string matched (enclosed in \( . . . \)). :%s/ \([4-9]\)/\1th/g replaces 4 with 4th, 5 with 5th, etc. ags modies the behavior of a search. You can mix some of them.

3.21

Jump to declaration

3.22

Using ctags

ctags is a powerful utility help to analyze the code and help the programming process. Create a tag le using ctags: ctags *.c *.h The tag le defaults to tags. After that you can     use Ctrl  + ]  to ask for declaration. Go back by         Ctrl  + T  . ctags is more powerful than g  d  and

    g  D  , but requires update if code has changed. You can incorporate the update in your Makefile to simplify the work. A good tool for analysing code.       g  Ctrl  + ]  shows a list of matching tags. This is useful to identify multiple denition. 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.linuxjournal.com/article/8289

Contents
1 Issues related to new Vim 7.0 1.1 Parenthesis matching . . . . . . . . 1.2 Keyword completion disable the 1.3 Tab editing . . . . . . . . . . . . . 1.4 Spell checking . . . . . . . . . . . . . . . . menu . . . . . . . . . . . . 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.1 A clean GVim . . . . . . . . . . . . . . . . 2.2 Fonts, fonts, fonts, ... . . . . . . . . . . . . . 2.3 Clipboard . . . . . . . . . . . . . . . . . . . 3 Other bits 3.1 Paste Mode . . . . . . . 3.2 Copying from Terminal 3.3 Searching . . . . . . . . 3.4 Man page . . . . . . . . 3.5 Dictionary Lookup . . . 3.6 Joining line(s) . . . . . . 3.7 Executing commands . . 3.8 Matching parenthesis . . 3.9 Make C, C++ programs 3.10 Visual mode . . . . . . . 3.11 Code folding . . . . . . 3.12 Indenting code . . . . . 3.13 Using numbers . . . . . 3.14 Macros . . . . . . . . . . 3.15 Undo, Redo and Repeat 3.16 Splitting Window . . . . 3.17 Navigation and Marks . 3.18 Search and Replace . . . 3.19 Constant manipulation . 3.20 Retabbing for printing . 3.21 Jump to declaration . . 3.22 Using ctags . . . . . . .

4
4.1

Finally...
Help! How do I...

What have been covered is just a very small portion of tips and tricks. If you have other problems concerning Vim, try the following: Ask vimtutor if you are new to Vim. A quick reference card might help get you started. Search, on the internet, using the term "vim quick reference card". This should return you some html formatted reference as well as prettilyformatted pdf which is nice for printouts. A graphical cheat sheet is also available ("vim graphical cheat sheet"). Use :help. For example, :help ts should tell you what is mean by ts (in your vimrc). Navigate your        self by using the cursor keys ( h  j  k  l  ), follow a link (enclosed in pipes, |link| for example) us        ing Ctrl  + ]  , go back by Ctrl  + T  . Wander around the help should increase your skill in using Vim. (But remember, learn through practice) As mentioned, internet is a good place to look for help. In particular, on www.vim.org there are lots of tips and scripts. Scripts for eye-catching colour schemes, syntax highlighting for other languages are few examples.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . in Vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . .

4 Finally... 4.1 Help! How do I... . . . . . . . . . . . . . . .

First edited: October 6, 2006. Last modied: February 11, 2009.

You might also like