Tag Archives: vim

Yet another Vim tips post

I’ve been using vim as my texteditor/codeeditor/ide/etc for about 2 years now. Since then, I tried a lot of different plugins, configurations, and end up with a good set for my workflow. I decided to share here some of those, maybe someone will find them useful 😛

First, here’s a list of my most useful plugins and some configurations I’ve done to them:

CtrlP is by far my most useful plugin. I used to use Command-T, but it’s dependency on ruby was giving me lots of headaches. It searches for files, recursively from the work directory (the place where you opened (g)vim) in a fuzzy way. You call it’s search function and start typing the file pattern.

ctrlp.vim plugin

You can also use it to search for opened buffers in the same fuzzy way.  And here is some options I’ve put on my ~/.vimrc:

" This will flush CtrlP and remake it's cache
nmap <C-F7> :CtrlPClearCache<CR>
" This will open the CtrlP fuzzy search window for files
nmap <silent> <Leader><Space>f :CtrlP<CR>
" This will open the CtrlP fuzzy search window for buffers
nmap <silent> <Leader><Space>b :CtrlPBuffer<CR>
" This will open the CrtrlP fuzzy search window for tags
nmap <silent> <Leader><Space>t :CtrlPTag<CR>

Indexer makes it very easy to automatically generate (and maintain) tags for your projects, so you can jump to them using CTRL+], g+] or even CTRL+Click. All you have to do is install it and it’s dependencies (see the homepage) and do a simple configuration.

There is tree ways of configuring it. One is by using it as an extension to project.tar.gz plugin. Another way is by creating a .vimprj folder inside your project’s folder, so as soon as it finds that folder, it’ll understand that that’s a projects’ folder and create tags inside that .vimprj. The last one, the one I use, is to create a ~/.indexer_files file and specify a projects parent folder. That way is perfect for people who keeps all of their projects inside the same directory, and it supports nested directories. Here is my configuration as an example:

[PROJECTS_PARENT]
~/dev/
~/dev/external/
~/dev/stoq/
~/dev/icmc/
~/dev/proj/

By doing that, the tags will be generated/updated at a folder ~/.vim_indexer_tags.  It’s so simple that I never have to remember to generate/update tags for none of my projects, even one that I just created (since they will be on one of PROJECTS_PARENT directories).

That’s another very useful plugin for people who uses grep a lot. It grep for a pattern, and populate vim’s quickfix window with all the found patterns, so it’s easy to access them, so you can navigate through them using ]q (the same as :cnext)  and [q (the same as :cprev).

And obviously, you can setup some maps to make it easier to grep your pattern. Here is a piece of my ~/.vimrc:

" Files to skip
let Grep_Skip_Files='*.bak *~ *.pyc *.o *.obj'
" Directories to skip
let Grep_Skip_Dirs='.bzr .git .hg'
" The grep itself
nnoremap <silent> <Leader>gg :Grep<CR>
" Grep recursively
nnoremap <silent> <Leader>gr :Rgrep<CR>
" Grep buffer
nnoremap <silent> <Leader>gb :Bgrep<CR>

Those are just my must-have list. There are some other that I won’t detail here but I find useful too, like:

To finish, here is some general-use pieces of my ~/.vimrc:

" Dictionary and Spell Options
set spelllang=en,pt
set dictionary+=/usr/share/dict/words
 
" Jump to the last position when the file was last opened..
autocmd BufReadPost * if line("'"") > 1 && line("'"") <= line("$") | exe "normal! g'"" | endif
 
" Visual help to keep text on 80 columns
set textwidth=79
autocmd BufEnter * hi ColorColumn ctermbg=lightred ctermfg=white guibg=#592929
if version >= 703
  autocmd BufEnter * set cc=+1
else
  autocmd BufEnter * match ColorColumn /%80v.+/
endif
 
" Clear highlight when refreshing.
nnoremap <C-L> :nohls<CR><C-L>
inoremap <C-L> <C-O>:nohls<CR>
 
" Make Y compatible with D
nnoremap Y y$
 
" Make Shift+Insert paste from global clipboard
nnoremap <S-Insert> "+p
inoremap <S-Insert> <C-O>"+p
noremap! <S-Insert> <MiddleMouse>
 
" Continous visual indenting
vnoremap < <gv
vnoremap > >gv

Most of those tips/plugins were found googling, on vim tips wiki and vim scripts itself.

Suggestions for improving this post are very welcome! 🙂