Skip to main content

Changing Line Indentation in VI

· 2 min read

Further to my previous post on changing the tab size in the VI editor, I've found out how to change the indentation size in VI.

In most GUI editors, such as gedit, highlighting several lines of text and pressing the Tab key will indent the lines by one tab character. To get a similar behaviour in VIM, you have to first determine how many tab characters you want to add or remove. This number has to be assigned to shiftwidth. Next find out what is the indentation size (how many characters of space a tab character should represent). You can do this with the command :set tabstop

In openSUSE 10.3 with vim 7.1, if you want an indentation consisting of only tab characters, no space characters, the shiftwidth size should be a multiple of the tabstop size.

For example, I like my tabs to represent four spaces. So I set the tabstop size to be four: :set tabstop=4

I also want the lines of text to be indented with 4 tabs, so I set the shiftwidth size to be 16 (16/4 = 4): :set shiftwidth=16

To select lines of text in VIM, in command mode press the upper-case V character to change VIM to the visual mode. Upper-case V lets you select lines of text. After the lines of text are selected, press the right arrow key > to indent the lines. These lines will be indented with 4 tab characters.

To indent only a single line, enter the right arrow key twice (>>) when the cursor is on the line.

The left arrow key removes indentation.

Note that if the shiftwidth value is not a multiple of tabstop value, additional space characters will be inserted.

Disclaimer: Multiple online sources have described that using CTL-T and CTL-D actually adds and removes a tab character respectively. Using openSUSE 10.3 and vim 7.1-44.2, I don't have this behaviour. It might be due to the pre-configured settings made to VIM.