Tricks with Vim

I initially felt that a few things in Vim are just…meh. I want my tabs to take up 4 spaces instead of 9. I want syntax highlighting for LESS or TypeScript. There are better ways, right?

You can customize Vim by creating a .vimrc file in your home directory.  vim ~/.vimrc

Tab Settings

Quick primer: A “hard tab” is inserting the TAB character (i.e., \t). A “soft tab” is inserting spaces in lieu of a TAB character. I personally prefer soft tabs, but don’t care enough to join the holy war.

Here’s a good article on tabs, spaces, and indents.

  • tabstop: Size (in spaces) of a hard tab. Still only inserts a single TAB character.
  • softtabstop: Number of spaces inserted when the TAB key is pressed.
  • expandtab: Set this option to always use soft tabs.
  • shiftwidth: Size (in spaces) of an indent.

So if you want your tabs to be 4 spaces, and to always be soft tabs:

set tabstop=4
set softtabsstop=4
set shiftwidth=4
set expandtab

Syntax Highlighting

If you open a TypeScript file in Vim, you won’t get the nice JavaScript syntax highlighting.  Want to configure this? Use BufReadPost.

au BufReadPost *.ts set syntax=js
au BufReadPostd *.less set syntax=css

I don’t like having to hit ESC to switch to Command Mode

Well, good sir or madam, you are in luck!  You can also use Ctrl+C.  (Better or worse depending on your keyboard. I find this fantastic for keyboards where you can set a thumb button to Ctrl.)

Multiple Files in Split Windows

Use the “Split” command, :sp filename, to open a new file in a split window (horizontally).  Use the command Ctrl+wv to split the windows vertically.

vim file1
:sp file2 (opens and splits horizontally)
ctrl+wv (splits vertically)

Or, when you are opening Vim, use the -o switch to open multiple files.  -o to split horizontally, -O to split vertically.

vim -o file1 file2   // split horizontally
vim -O file1 file2   // split vertically


Your Thoughts?

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s