post: Code: Setting Tabs & Indent in Vim


Setting Tabs & Indent in Vim



Code Editor Development Unix Vim

Vim is a highly configurable editor built to enable efficient text editing. It is an improved version of the vi editor distributed with most UNIX systems. It also comes as a windows program gVim:


  • persistent, multi-level undo tree
  • extensive plugin system
  • support for hundreds of programming languages and file formats
  • powerful search and replace
  • integrates with many tools

One of the frequently asked questions by vim users is how to replace the 4 space tab indent with 4 space characters. Below is a block of settings which will perform this function. They can be added to your .vimrc file.

filetype indent on  " load filetype-specific indent files
set tabstop=4       " number of visual spaces per TAB
set shiftwidth=4    " On pressing tab, insert 4 spaces
set expandtab       " tabs are spaces

An additional feature that can be added is the to map the :retab command. Retab essentially triggers expand tab for any current conventional tab characters, and replaces them with spaces dependent on the tabstop command. To manually do this type :retab on the vim command line. The code below added to your vimrc would map this to the F2 function command. The :wq! saves the file on remap. Remove this to keep the file open, or :w! to just save.

map <F2> :retab <CR> :wq! <CR>

comments powered by Disqus