Sunday, February 5, 2012

A couple of nice VIM tricks

0 comments
I really like VIM. I'm used to it and it's fast, flexible, and powerful. Once you learn how to extended it VIM opens up many new avenues to productivity.

This is a really basic trick but it's powerful. It's one of those that I spent a long time wishing vim would do only to learn that "IT COULD" do exactly what I wanted it to do!

One of the complaints about console text editors is that they don't scroll using the mouse wheel. VIM is not one of those. If you add this to your .vimrc file:

if has('mouse')
set mouse=a
endif

Vim will use the mouse wheel to let you scroll through the file you are working on!

If you code in python these next two tricks will be a big help. I work on all sorts of projects and once in awhile I'll get a file that uses spaces for tabs. I'm currently using tabs for everything because that's the standard where I work. But if I pick up a PEP8 compliant file then I'll get spaces. So to do a quick conversion while I'm in the file I put the following settings in my .vimrc:

map :%s/ \{4}/ /g

That will convert all spaces to tabs in command mode. Just reverse it or use another f key or key combination if you want to go from tabs to spaces.

This last trick is especially for python folks. Sometimes extra spaces will creep in between some text and your tabs. The interpreter doesn't like this at all and will make your life difficult by not compiling your file. If you have a space before a tab you can use this vim trick to highlight the spaces so that you can easily find them.

"Mark trailing whitespace and spaces before tabs
highlight RedundantSpaces term=standout ctermbg=red guibg=red
match RedundantSpaces /\s\+$\| \+\ze\t/ "\ze sets end of match so only spaces highlighted
set listchars=tab:>-,trail:.,extends:>

Be a little careful with this one because some themes you choose or syntax files will make this silently fail.

All the best.

Comments

0 comments to "A couple of nice VIM tricks"