Wednesday, June 13, 2012

How to insert spaces with Tab key with Vim

Some projects require for one reason or another to use spaces instead of a Tab character to indent the C/C++ code. Most of the time it is related to the diffs that are generated by the CM systems. If you are a Vim user, you can configure your editor to insert spaces with Tab keystroke by adding these commands to your ~/.vimrc:

 set ts=4  
 set shiftwidth=4  
 set expandtab  
 set nowrap  

This assumes that the width of desired tab is 4 characters. gVim is a popular GUI font-end for Vim editor. However, I found the way gVim stores its configuration into ~/.vimrc non-intuitive:
  • Go to your $HOME directory.
  • Start the shell terminal.
  • Start gvim from command-line.
  • Configure your settings the way you like them to be.
  • Save the setting to .vimrc with command
     :mkvimrc  
    
The trick here is to run gvim from your home directory. Otherwise, gVim writes .vimrc file in the current directory where it was started from.

No comments:

Post a Comment