Docstoc

Linux vim tips and tricks

Document Sample
Linux vim tips and tricks Powered By Docstoc
					                                              Vim: Tips and tricks
1      Issues related to new Vim 7.0                                           2     Regarding GVim
1.1      Parenthesis matching                                                  2.1     A ‘clean’ GVim
When your cursor in on a parenthesis, the matching paren-                      One of the good things about GVim is that the most com-
thesis (“the other side”) will be highlighted. This may an-                    mon tasks are listed on the menu so you can learn quickly.
noy some of you. In order to disable this feature, open                        After that you may want to claim back the area, eliminate
your vimrc (located in your home directory, ~/.vimrc in                        the menu and toolbar. (Why not Vim? GVim has more
Unix and N:\ vimrc in Windows) and add this line:                              colours to choose from, that’s why — at least to me) Just
let loaded matchparen = 1                                                      add this line to your vimrc:
Restart your Vim (or GVim) to see the effect.                                   set guioptions=g

                                                                               2.2     Fonts, fonts,          fonts, ...
1.2      Keyword completion — disable the
         menu                                                                  Choose a font in the GVim using


                                                  
                                                                               :set guifont=*

candidates when you press ØÙ Ctrl  + ØÙ N  or ØÙ Ctrl  + ØÙ P  by
Starting from 7.0, you will see a menu showing a list of                       Once you’re satisfied with the font, show the font name
                                                                               with :set guifont, which will return something like
default. If you find this annoying then turn it off by adding                     guifont=Monospace 8
this line to the vimrc:                                                        You have to escape spaces and commas that might occur
set completeopt=                                                               in the string using backslash. In this case you should add
See more about completion by :help completion.                                 this line to your vimrc:
                                                                               set guifont=Monospace\ 8
1.3      Tab editing
                                                                               2.3     Clipboard

                                                                                                    
Instead of typing :edit new.file (:e in short) to edit

                                                                               ØÙ "  ØÙ +  ØÙ y  and ØÙ "  ØÙ +  ØÙ p  , respectively.
another file, a new option :tabedit new.file (:tabe) is                         In GVim, you can do copy and paste to the clipboard by

                                                               
Navigate though the tabs using ØÙ Ctrl  + ØÙ PgUp  and
available. This creates tabs, like those used in browser.

               
ØÙ Ctrl  + ØÙ PgDn  (If your terminal processed these key bind-
                                       
                                                                               3     Other bits
ings, you can still use ØÙ g  ØÙ t  and ØÙ g  ØÙ T  — though that’s
                                                                               3.1     Paste Mode
not as convenient). Advantage of using tabs over buffer
list is that you switch between files using shortcut keys                       Have you ever tried pasting something into Vim and end
(instead of :bn<ENTER> and :bN<ENTER>). Also, name of                          up with badly aligned result? Here’s the trick: type :set
the file is shown clearly on the tab, so you know what you                      paste to put Vim in paste mode before you paste, so Vim
are doing. (though you lose one line)                                          will not do anything fancy and paste all the stuff verbatim.
     Combining this command and window splitting (:new,                        After you have finished pasting, type :set nopaste to go
:vnew, :split, :vsplit, ...) gives you the most flexibility.                    back to normal, where indentation will take place again.
                                                                               (You only need this option when pasting in terminal, but
                                                                               not GUI)
1.4      Spell checking
To enable spell checking, type the following:                                  3.2     Copying from Terminal
  :setl spell spl=en                           All regions (default)

                                                            
                                                                               Use :set nonu to disable line numbering before you copy
  :setl spell spl=en gb,en us                              GB, USA
Move around misspelled words using ØÙ ]  ØÙ s  and ØÙ [  ØÙ s  .
                                                                               lines of code to the clipboard. Restore by :set nu.

To turn it off:
                                                                                                                        
                                                                               3.3     Searching
                                                                                   • You might know ØÙ /  and ØÙ ?  for searching. How
:setl spell spl=

                                                                                                     
                                                                                     about ØÙ *  and ØÙ #  ? They search for word nearest to
Of course, putting these as a keyboard macro will be more
convenient. Add this to your vimrc:
set spell spl=en us " Select language                                                the cursor forward and backward, respectively. They

                                                                                                                                        
set nospell " Turn it off at start                                                   save you time typing the word to search.
                                                                       • Use ØÙ n  instead of ØÙ /  ØÙ ENTER  to repeat search ( ØÙ N 
In insert mode, ØÙ Ctrl  + ØÙ X  ØÙ S  gives you spell sugges-
nmap <F7> :set spell! "toggle spell check

                                          
tion(s), and you can press ØÙ Ctrl  + ØÙ N  for next suggestion.
                                                                                     do it backwards).

For more information, :help spell.                                                 • Use :noh to cancel highlights after searching.
    • :set is turns on incremental search.                                3.11        Code folding
                                                                          Instead of splitting your window to view source code from

 
3.4     Man page                                                          different part of the program, you can fold up codes that

ØÙ K  shows you the man page for the keyword under cursor.
                                                                          you are not interested. To fold some part of your code,
                                                                                                                                
                                                                          :fo. Note that when you type ØÙ :  it will insert ’<,’>
                                                                          just create a selection using the visual mode, then type

                                                                          automatically and you don’t need to remove them. A fold
                                                                                                                                
3.5     Dictionary Lookup
                                                                          open and close a fold by ØÙ z  ØÙ o  and ØÙ z  ØÙ c  , respectively.
                                                                          will be represented by /*{{{*/ and /*}}}*/. You can
Add this line to your vimrc:

                                    
nmap <F2> :!webster <C-R><C-W><CR>                                            You can also ask Vimto fold the code based on the
Dictionary shows up when you press ØÙ F2  in normal mode.
                                                                          indentation of your code. To achieve this, type :set
                                                                          fdm=indent (or add it to vimrc). Here fdm means “fold
                                                                                                                  
                                                                          dent level using ØÙ z  ØÙ c  and ØÙ z  ØÙ o  . Moreover, you can
                                                                          method”. Then you can fold/unfold the code at that in-

                                                                                                                                                    
3.6     Joining line(s)
                                                              fold/unfold codes of the same indent level by ØÙ z  ØÙ m  (fold
ØÙ J  join the current line with the next, and ØÙ g  ØÙ J  do the                   
                                                                          more) and ØÙ z  ØÙ r  (fold less). In this case you can fold your

                                                                                     
same without inserting a space.

                                                                              Why ØÙ z  ? According to the help, z is like a folded piece
                                                                          code without adding the fold mark.

3.7     Executing commands                                                of paper.
:! command executes command as if it is typed under
the shell. Combining this with :make you don’t need to                    3.12        Indenting code
exit Vim every time you compile and debug your program.
                                                                          There are few options related to indentation, namely
                                                                          autoindent, cindent and smartindent. An example to
                                                                          this is located in $VIMRUNTIME/vimrc example.vim.
 
3.8     Matching parenthesis
                                                                                                                    
ØÙ %  is used to match parenthesis. It can match (), [], {},            use number of lines ØÙ =  ØÙ =  . To indent the whole file, type
                                                                                  To adjust indentation (e.g. in case of copy & paste),

                                                                                 
                                                                          ØÙ g  ØÙ g  ØÙ =  ØÙ G  (go to the top, then indent until the end of
comment (/* */) and #define.
                                                                          file.
3.9     Make C, C++ programs in Vim
                                                                          3.13        Using numbers
Add this line to vimrc
                                                                                                                             
                                                                          indicating the number of times to repeat. E.g. ØÙ 8  ØÙ p 
au FileType cpp,c setl mp=make\ %:t:r                                     You can precede most of the commands with a number
then you can make your C or C++ programs in Vim using
:make. Note that it uses environment variables CFLAGS                     paste 8 times. But for some commands, preceding with a

                                                                                                                            
and CXXFLAGS as default flags. So add                                      number changes its meaning:

                                                                              • ØÙ G  send you to the bottom of file, while n ØÙ G  send
set CFLAGS="-Wall -g"
set CXXFLAGS=($CFLAGS)

                                                                                                                
                                                                                you to the n ’th line.
to your .cshrc (for csh), or
export CFLAGS="-Wall -g"                                                      • ØÙ %  match parenthesis, while n ØÙ %  send you to the
export CXXFLAGS=${CFLAGS}                                                         n ’th percentile of the file.
to your .bashrc (for bash), to make sure you don’t miss
any warnings and have your program ready for debugging.
In case of any warnings or errors it will take you there.
                                                                          3.14        Macros

                                                                                                                
You can display subsequent warnings and errors after a
                                                                          gin recording a macro by pressing ØÙ q  c , where c can
                                                                          Macros are (one of the) most powerful feature in Vim. Be-
make by typing :cn (previous by :cN).
                                                                          be any alphabets. This will be the name of the macro.
                                                                                                       
                                                                          ing a macro by pressing ØÙ q  again. Execute the macro
                                                                          Any keys pressed after that will be recorded. End record-
                                      
3.10      Visual mode
Three visual modes are available. ØÙ v  is for normal visual                                
                                                                      you recorded by ØÙ @  c . Using macros can increase pro-
mode, you can invoke it using mouse drag. ØÙ V  is for line             ductivity a lot, saving your time in doing some repetitive
                                                        
using a mouse you do double click and drag. ØÙ Ctrl  + ØÙ V 
visual mode, the unit becomes line instead of character,                  work.



                                                                                                                      
is for block visual mode, one of the features that differenti-             3.15        Undo, Redo and Repeat
                                                                              • Undo by ØÙ u  , undo the whole line by ØÙ U  .
ate itself from other editors. A triple click (yes!) and drag
invoke this.
                       
     • Redo by ØÙ Ctrl  + ØÙ R  .                                                               – g Replace all occurrence in the line. Without

        
                                                                                                      this only the first matched string in each line
     • ØÙ .  repeats the last action. This can be a sequence
                                                                                                      will be replaced.
       of actions you do in insert mode.                                                            – c Confirm each substitution.
                                                                                                    – n Just report the number of matches, substitu-
3.16        Splitting Window                                                                          tion is not done. (version 7.0 or above)

Split a window horizontally using :sp (split). Split a win-
                                                                                          3.19       Constant manipulation
dow vertically using :vs (vertical split). Precede the com-
                                                                                                                                    
                                                                                          want to modify. The shortcut ØÙ Ctrl  + ØÙ A  add one to the
mand with a number (e.g. :10sp) to control the height (or                                 Sometimes you have constants in your program that you

                                                                                                               
width) of the first window.
                                                                                          number and ØÙ Ctrl  + ØÙ X  subtract one from the number.
    Combining split and edit command, we have :new and
                                                                   
    Move to the adjacent windows using ØÙ Ctrl  + ØÙ W  and
:vnew. Cycle through the list of files using :bn and :bN.
                                                                                          The advantage of using this is your cursor don’t have to be

                                         
                                                                                          right on top of the number, it just search first number after

size of the window using ØÙ Ctrl  + ØÙ W  { ØÙ +  , ØÙ -  , ØÙ >  , ØÙ <  }
then cursor key. Remove a window using :q. Adjust the                                     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.
3.17        Navigation and Marks
You jump to elsewhere in a file or another file for various
                                                                                          3.20       Retabbing for printing
reasons. Searching, or simply press the wrong key, brings                                 By default, tab is equal to 8 spaces when printed. This
                                                  
ØÙ Ctrl  + ØÙ O  (‘oh’). To go forward, press ØÙ Ctrl  + ØÙ I  .
you away from where you are working. To go back, press                                    causes printouts hard to read when you have more than a

                                        
                                                                                          few level of indentation. Retab is the solution. Perform
     To mark a position, press ØÙ m  c . This saves the loca-
                                                                   
                                                                                          the following:

tion in mark c . To go back to that location press ØÙ ‘  c .
                                                                                          ’ Specify how much spaces one tab is equivalent
                                                                                          ’ to (Not required if you are happy with the
                                                                                          ’ current settings)

                               
3.18        Search and Replace                                                            :set ts=4

     • Perform a search using ØÙ /  pattern .
                                                                                          ’ With expandtab, tabs are converted to spaces
                                                                                          :set expandtab
                                                                                          ’ Replace tabs with white spaces
     • Perform a replace using                                                            :ret
       : r s/ pattern / string / flags                                                     ’ Save and do your printing, e.g.
       where                                                                              ’ lpr -Popenlab xx.c
                                                                                          ’ With noexpandtab, spaces are converted to tabs
         r defines the range                                                               :set noexpandtab
                                                                                          ’ Undo changes to your code using ’u’, or
            – % : the whole file                                                           :ret
            – a,b : from line a to line b

                                                                                                           
            – ’<,’> : range equals to lines highlighted (in-                              3.21       Jump to declaration
                                                                                          ØÙ g  ØÙ d  and ØÙ g  ØÙ D  go to local and global declaration re-
              serted automatically in visual mode)
                                                                                          spectively. Place the cursor on a variable or a function and
                                                                                                                          
         pattern is the pattern to search for. Vim accepts

                                                                                          back using ØÙ Ctrl  + ØÙ O  )
        regular expression. Include \c in search pattern to                               these commands will bring you to the declaration. (Go
        ignore case in search.
                                                                                          Note that these function is not guaranteed to work as it
            – gr[ae]y matches both gray and grey.                                         may bring you to the wrong place (e.g. when you define
                                                                                                                                                              
                                                                                          Display the definition of the current variable by ØÙ [  ØÙ i  .
            – red\|blue matches both red and blue.                                        the function multiple times).
            – ^ matches beginning of line, $ matches end of
              line.
                                                                                          3.22       Using ctags
         string is the string to replace with. You can use
        backreference (\1, \2, . . . ) to refer back to the string                        ctags is a powerful utility help to analyze the code and
        matched (enclosed in \( . . . \)).                                                help the programming process. Create a tag file using
                                                                                          ctags:
            – :%s/ \([4-9]\)/\1th/g replaces 4 with 4th, 5                                ctags *.c *.h
                                                                                                           
              with 5th, etc.
                                                                                          use ØÙ Ctrl  + ØÙ ]  to ask for declaration. Go back by
                                                                                          The tag file defaults to tags.             After that you can

                                                                                                                                        
                                                                                          ØÙ Ctrl  + ØÙ T  . ctags is more powerful than ØÙ g  ØÙ d  and
        flags modifies the behavior of a search. You can
        mix some of them.
   
ØÙ g  ØÙ D  , but requires update if code has changed. You can                Contents
incorporate the update in your Makefile to simplify the
               
                                                                                  1 Issues related to new Vim 7.0                                          1
ØÙ g  ØÙ Ctrl  + ØÙ ]  shows a list of matching tags. This is use-
work. A good tool for analysing code.
                                                                                    1.1 Parenthesis matching . . . . . . . .           . . . .         .   1

              
                                                                                    1.2 Keyword completion — disable the               menu            .   1

ØÙ Ctrl  + ØÙ ]  to this in your vimrc:
ful to identify multiple definition. If you like you can map
                                                                                    1.3 Tab editing . . . . . . . . . . . . .          . . . .         .   1
                                                                                    1.4 Spell checking . . . . . . . . . . . .         . . . .         .   1
nmap ^] g^]
A nice tutorial on ctags can be found at:                                         2 Regarding GVim                                                         1
http://www.linuxjournal.com/article/8289                                            2.1 A ‘clean’ GVim . . . . . . . . . . . . . . . .                     1
                                                                                    2.2 Fonts, fonts, fonts, ... . . . . . . . . . . . . .                 1
                                                                                    2.3 Clipboard . . . . . . . . . . . . . . . . . . .                    1
4     Finally...
                                                                                  3 Other bits                                                             1
4.1     Help! How do I...                                                           3.1 Paste Mode . . . . . . .       . . . . .   .   .   .   .   .   .   1
                                                                                    3.2 Copying from Terminal          . . . . .   .   .   .   .   .   .   1
What have been covered is just a very small portion of tips                         3.3 Searching . . . . . . . .      . . . . .   .   .   .   .   .   .   1
and tricks. If you have other problems concerning Vim, try                          3.4 Man page . . . . . . . .       . . . . .   .   .   .   .   .   .   2
the following:                                                                      3.5 Dictionary Lookup . . .        . . . . .   .   .   .   .   .   .   2
                                                                                    3.6 Joining line(s) . . . . . .    . . . . .   .   .   .   .   .   .   2
    • Ask vimtutor if you are new to Vim.
                                                                                    3.7 Executing commands . .         . . . . .   .   .   .   .   .   .   2
    • A quick reference card might help get you started.                            3.8 Matching parenthesis . .       . . . . .   .   .   .   .   .   .   2
      Search, on the internet, using the term "vim                                  3.9 Make C, C++ programs          in Vim .     .   .   .   .   .   .   2
      quick reference card". This should return you                                 3.10 Visual mode . . . . . . .     . . . . .   .   .   .   .   .   .   2
      some html formatted reference as well as prettily-                            3.11 Code folding . . . . . .      . . . . .   .   .   .   .   .   .   2
      formatted pdf which is nice for printouts. A graph-                           3.12 Indenting code . . . . .      . . . . .   .   .   .   .   .   .   2
      ical cheat sheet is also available ("vim graphical                            3.13 Using numbers . . . . .       . . . . .   .   .   .   .   .   .   2
      cheat sheet").                                                                3.14 Macros . . . . . . . . . .    . . . . .   .   .   .   .   .   .   2
                                                                                    3.15 Undo, Redo and Repeat         . . . . .   .   .   .   .   .   .   2
    • Use :help. For example, :help ts should tell you                              3.16 Splitting Window . . . .      . . . . .   .   .   .   .   .   .   3
                                                    
      self by using the cursor keys ( ØÙ h  ØÙ j  ØÙ k  ØÙ l  ), follow
      what is mean by ts (in your vimrc). Navigate your-                            3.17 Navigation and Marks .        . . . . .   .   .   .   .   .   .   3
                                                                                    3.18 Search and Replace . . .      . . . . .   .   .   .   .   .   .   3

                                              
                                                                                    3.19 Constant manipulation .       . . . . .   .   .   .   .   .   .   3
      ing ØÙ Ctrl  + ØÙ ]  , go back by ØÙ Ctrl  + ØÙ T  . Wander
      a link (enclosed in pipes, |link| for example) us-
                                                                                    3.20 Retabbing for printing .      . . . . .   .   .   .   .   .   .   3
                                                                                    3.21 Jump to declaration . .       . . . . .   .   .   .   .   .   .   3
      around the help should increase your skill in using
                                                                                    3.22 Using ctags . . . . . . .     . . . . .   .   .   .   .   .   .   3
      Vim. (But remember, learn through practice)

    • As mentioned, internet is a good place to look for                          4 Finally...                                                             4
      help. In particular, on www.vim.org there are lots                            4.1 Help! How do I... . . . . . . . . . . . . . . .                    4
      of tips and scripts. Scripts for eye-catching colour
      schemes, syntax highlighting for other languages are
      few examples.

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

				
DOCUMENT INFO
Description: Tips and Tricks for Linux Text editor "vim"