Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using gg=G in Vim to automatically tab a document when tabstop is modified

Tags:

vim

Usually, one uses gg=G in vim to automatically tab C/C++/Java code or in fact any sort of code.

However, I changed my tabstop variable to be tabstop=2 in my default settings for vim, so that whenever I tab I get the equivalent of 2 spaces instead of the default 8.

Now whenever I use gg=G, I get 4 tabs instead of 1 for indentation so that the spacing looks equivalent to before when I had tabstop=8. I just want 1 tab though. Is there a way to do this?

(I am using hard tabs only for indentation, no spaces).

like image 266
Ð.. Avatar asked Jul 15 '17 15:07

Ð..


1 Answers

What you are looking is shiftwidth. set sw=2 for proper indentation for your case. :help sw

At the same time, instead of changing tabstop=2, it is better to set softtabstop=2. Details at :help ts.

and for source code, usually it is good to expand tab as it is not preferred for source code.

You can set following settings in your vimrc

set softtabstop=2
set shiftwidth=2
set expandtab

I personally prefer indentation of 4. With 2, it will be hard to notice the indent sometimes :)

like image 107
dlmeetei Avatar answered Oct 09 '22 03:10

dlmeetei