2015年7月12日 星期日

Serveral Ways to Improve your VIM!

   Since I reinstall Ubuntu several times, it would be better that I write a memo to record some process to set up VIM in Ubuntu. This article reminds me some Plugins of VIM that I usually used.


NERD_Tree

This is a Plugin that can show you the File Architecture.

Set Up

1. Download it from here.
2. In your home directory, create a new ".vim" directory (if you have no VIM plugin yet).
3. Move everything in NERD_Tree you just download to ".vim" directory. And your ".vim"
   directory  would look like:
    doc  nerdtree_plugin  plugin  syntax
4. Add the following line to ".vimrc" file in your $HOME directory, so that when you press F2 in
    your keyboard, the NEARD_Tree will comes out.

nnoremap <silent> <F2> :NERDTree<CR>

Usage

1. When you want to see the file architecture, press F2, and if you want to open a file in current
    tab, just press Enter when your cursor is on that file name.
2. If you want to open a file in a new tab, just hit "Shift" + "t" when your cursor is on that file name.
3. You can use "Ctrl" + "w" + "w" to switch between windows in VIM.
4. Here is some commands (which already shiped with VIM) that can  control the window or tabs:
     (a) ":sp" + "FileName": Open a new file in the horizontal split window (so that you have two
           window (the top and the bottom window). If you don't specified FileName, VIM would open
           the same file.
           ":vs" + "FileName": Likewise, open a new file in the vertical split windows.
     (b) "Ctrl" + "w" + "T": If you have splitted window, you can use this command to move the split
          window to a new tab.
     (c) "Ctrl" + "w" + "+": If you have more than one horizontal split window, it would expand your
          activate window (in which your cursor in) expanded by one more line (and shrink the others
          by one line in the same time).
           "Ctrl" + "w" + "-": The opposite of above.
     (d) "Ctrl" + "w" + "r": If you have more than one split windows, this will change the two
          windows upside down.
     (e) ":tabm +1": If you have more than one tabs, you can move the current tab to right by this
           command.
          ":tabm -1": Likewise, you can use this command to move current tab to left.

TagList

You can use this plugin to see the class, functions, enums in the file.

Set Up

1. Download TagList from here.
2. Like NERD_Tree, Unzip the file you just download, and move them to the ".vim" directory. But
    be careful not to replace the file that belong to NERD_Tree. For example, after you set up the
   NEARD_Tree or other plugins, you would have a "doc" and "plugin" directory in your ".vim"
   directory. Now move files inside "plugin" (which is in the TagList directory you just Unzip) to
   the ".vim/plugin". Other plugins of VIM can be installed in the similar way.


Share Clipboard between VIM and Linux

   Have you ever want to search all files to find a keyword (e.g. a function) been referenced?
One way to do this is to copy that keyword in VIM and move to the other terminal and use the "grep" command. This can be done by use the mouse right-click and select "copy" in the VIM and move to the other terminal and type "grep '" and use the mouse right-click and select "paste" to paste the keyword. And finally you search that keyword by "grep" command.
    Isn't that kind of silly? And the reason you use VIM is to avoid your hand switching between keyboard and the mouse...
   Here is the solution: Make VIM share clipboard with the system! Do do so, you must install
the vim-gnome. The normal VIM fail to share its clipboard with system (if you want to see the detail, please see this article).
Fortunately, change from normal VIM to VIM-Gnome is easy. You just need to download it by:
sudo apt-get install vim-gnome
And it will read your original .vimrc setting without any further adjustment and must of the VIM plugins can be used directly. Now, add the following line in your .vimrc:
vnoremap <C-c> "+y
And when you in the command mode of VIM, select keyword by "v" and copy it by "Ctrl" + "c".  Now move to the other terminal in the other tab by "Ctrl" + "Page Down" (or, use "Ctrl" + "Shift" + "t" to create a new terminal in the new tab), and now you can type "Ctrl" + "Shift" + "v" to paste the keyword!

Gtags

    Do you want to find a plugin that can lead you to the definition of a function or has the ability to "find all reference of a variable or function"? Although the Ctags and Cscope support part of this, they don't support some C++ features (such as function overloading, class... etc). Therefore, that's why I recommend using Gtags.

Set Up

1. Download it from here.
2. You should also download the ncurses that will be used by Gtags.
3. Build ncurses in the command line:
        ./configure
        make
        sudo make install
4. Build Gtags:
        ./configure
        make
        sudo make install
5. Copy Plugin to Vim:
        cp /usr/local/share/gtags/gtags.vim $HOME/.vim/plugin
6. Add following line to your .vimrc:
        nnoremap gd :GtagsCursor<CR>
        map <C-n> :cn<CR>
        map <C-p> :cp<CR>
        nnoremap <C-f> :Gtags -r<CR><CR>
Thus, you can type "g" + "d" on a function, the VIM will go to its definition, and "Ctrl" + "f" to find all references (in this case, you can type "Ctrl" + "p" to go to the previous reference, and type "Ctrl" + "n" to go to the next reference).
 

QuickFix

    QuickFix is built in VIM already, you can type ":copen" in the VIM command mode to open it (the QuickFix will reside in the bottom of the window). And when you want to compile your code, you can type ":make" in the VIM command mode. Then the code will start compiling, and show its compile info in the QuickFix window (as the first figure in this article).

Finally, here is my final .vimrc:
" Record the last preview line
au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif

" highlight search
set hlsearch

" Quick leave by F12
nnoremap <silent> <F12> :q<CR>

" Refresh by F5
nnoremap <silent> <F5> :edit<CR>

" Plugins
nnoremap <silent> <F2> :NERDTree<CR>
nnoremap <silent> <F3> : Tlist<CR>
nnoremap <silent> <F4> :copen<CR>

" Copy
vnoremap <C-c> "+y

" Gtags
nnoremap gd :GtagsCursor<CR>
map <C-n> :cn<CR>
map <C-p> :cp<CR>
nnoremap <C-f> :Gtags -r<CR><CR>

2015.07.12
Joshua P. R. Pan

沒有留言:

張貼留言