Useful (To me :) Vim commands.

It’s been a while since I have changed my vimrc file.  I recently had to do it to activate “textwidth” in my home box.  I still do not understand why it was not set to the usual 80 characters that I like.  It probably has been like that for some years but I never notice because I don’t do a lot of coding in that machine.  In any case, that pushed me to create a post about the commands that I have used over the years that I think are worth noting :).  Instead of listing the vim commands and what they do, I’m going to list what I needed to do and how I did it.  The meaning of the commands you can always find with vim help.

Different tab schemes for different filetypes

I try to follow the GNU coding style when I code in C or in C++.  This means, among other things. 2 space tabs.  On the other hand, I like to write my python code with 4 space tabs.  To accomplish this I use “autocmd” along with “execute” and “set”.  autocmd adds a predefined command that gets executed along with other autocmd commands.  In short, I have a line that detects the type of file I am editing and sets the vim tab parameters; all this in one line :).  Here is an example:

autocmd FileType c execute "set tabstop=2 shiftwidth=2 softtabstop=2"

The third word in the command is the type of file.  Vim understands strings like “python”, “javascript”, “html” and so on.  Notice that I tell vim to execute a multiple set.  It is necessary to enclose the command in quotation marks.

My lines should be of certain length

I have always been comfortable with a line length of 80 chars.  I’ve heard people argue that 72 is better.  I used to use 72 when I used mutt.  But I use 80 because it allows me to place two windows with code, side-by-side, and completely visualize the code (There is no code lost to the end of the window and/or there is no code that is wrapping around where it’s not supposed to).  Also, visualizing and comparing two diffs is much easier (I don’t do this much).  In any case the vim command to do this is “textwidth”.  Here is an example:

set textwidth=80

Painless spell checking.

This bit of code is not of my doing.  I downloaded my initial vimrc from… I don’t remember where and it already had the spell checking function in there.  I have looked at it a couple of times and I think its worth mentioning.  If you add this to your vimrc you will be able to toggle spell check with the F11 key.  It comes in handy when writing documents.

function s:spell()
if !exists("s:spell_check") || s:spell_check == 0
echo "Spell check on"
let s:spell_check = 1
setlocal spell spelllang=en_us
echo "Spell check off"
let s:spell_check = 0
setlocal spell spelllang=
map <F11> :call <SID>spell()<CR>
imap :call spell()

I’ll leave this post with those 3 little tips. :)


About joelgranados

I'm fascinated with how technology and science impact our reality and am drawn to leverage them in order to increase the potential of human activity.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s