0% found this document useful (0 votes)
55 views6 pages

Vim - Redefine Tab As 4 Spaces - Stack Overflow

Uploaded by

rahul bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views6 pages

Vim - Redefine Tab As 4 Spaces - Stack Overflow

Uploaded by

rahul bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

17/01/2020 vim - Redefine tab as 4 spaces - Stack Overflow

Redefine tab as 4 spaces


Asked 10 years, 1 month ago Active 4 months ago Viewed 786k times

My current setting assumes 8 spaces; how could I redefine it?

1054 vim

edited Aug 20 '14 at 15:00 asked Dec 10 '09 at 6:18


Paul Bellora Ricky
438 49k 17 119 171 27.7k 35 80 126

4 Better yet, set it to four spaces and enable auto-indent at the same time: Auto-indent with tabwidth set to
4 spaces – solid_liq Sep 11 '13 at 1:55

7 @heinrich5991 I don't get it. I don't need 8 spaces to realize that a line is indented, and more than
necessary means limiting the characters you can view on a long line of text (at least before wrapping).
But to each his own. :) – weberc2 Apr 20 '14 at 11:25

38 @heinrich5991 My argument is "use as little as possible to readily identify an indented line". By my


estimation, one's ability to easily identify an indentation drops off rapidly at < 3 spaces, and it stops
increasing at 4 spaces. Therefore, indentation that exceeds 4 spaces is a waste, in my experience. –
 weberc2 Apr 21 '14 at 13:40

7 @weberc2 Couldn't possibly agree more, mate. Which is why I feel so damn claustrophobic reading
Ruby or "modern-day JavaScript", each of which use 2-spaces for indentation. It legitimately gives me
eye strain when trying to follow heavily-nested structures. Hard tabs for the goddamn win. – user458541
Nov 13 '15 at 6:57

Possible duplicate of Replace Tab with Spaces in VIM – rofrol Mar 28 '16 at 20:13

10 Answers

It depends on what you mean. Do you want actual tab characters in your file to appear 4
spaces wide, or by "tab" do you actually mean an indent, generated by pressing the tab key,
1553 which would result in the file literally containing (up to) 4 space characters for each "tab" you
type?

Depending on your answer, one of the following sets of settings should work for you:

For tab characters that appear 4-spaces-wide:

set tabstop=4

If you're using actual tab character in your source code you probably also want these
settings (these are actually the defaults, but you may want to set them defensively):

set softtabstop=0 noexpandtab

Finally, if you want an indent to correspond to a single tab, you should also use:
https://stackoverflow.com/questions/1878974/redefine-tab-as-4-spaces 1/6
17/01/2020 vim - Redefine tab as 4 spaces - Stack Overflow

set shiftwidth=4

For indents that consist of 4 space characters but are entered with the tab key:

set tabstop=8 softtabstop=0 expandtab shiftwidth=4 smarttab

To make the above settings permanent add these lines to your vimrc.

In case you need to make adjustments, or would simply like to understand what these options
all mean, here's a breakdown of what each option means:

tabstop

The width of a hard tabstop measured in "spaces" -- effectively the (maximum) width of an
actual tab character.

shiftwidth

The size of an "indent". It's also measured in spaces, so if your code base indents with tab
characters then you want shiftwidth to equal the number of tab characters times
tabstop . This is also used by things like the = , > and < commands.

softtabstop

Setting this to a non-zero value other than tabstop will make the tab key (in insert mode)
insert a combination of spaces (and possibly tabs) to simulate tab stops at this width.

expandtab

Enabling this will make the tab key (in insert mode) insert spaces instead of tab characters.
This also affects the behavior of the retab command.

smarttab

Enabling this will make the tab key (in insert mode) insert spaces or tabs to go to the next
indent of the next tabstop when the cursor is at the beginning of a line (i.e. the only
preceding characters are whitespace).

For more details on any of these see :help 'optionname' in vim (e.g. :help 'tabstop' )

edited Apr 4 '18 at 22:36 answered Dec 10 '09 at 6:19


Laurence Gonsalves
114k 28 203 251

16 It is also important to ensure that Makefiles always use hard tab characters, otherwise builds will fail! I
have shown how to, at stackoverflow.com/questions/234564/… – Shervin Emami Jan 24 '14 at 2:48

29 @Undefined That's the system vim settings. ~/.vimrc is the user's vim settings. Most people leave
the system settings alone and just edit the user settings. Note that ~/.vimrc will not exist until you
create it on most systems. – Laurence Gonsalves May 23 '14 at 16:38

4 Vim users take note of the set softtabstop=4 feature! I am tired of trying to read your code with
less , or any other editor except vim , only to see wacky indenting because you redefined tab to be

https://stackoverflow.com/questions/1878974/redefine-tab-as-4-spaces 2/6
17/01/2020 vim - Redefine tab as 4 spaces - Stack Overflow
some arbitrary number of spaces (even though the rest of the system thinks otherwise)! :-) –
 Ogre Psalm33 Sep 17 '14 at 12:51

3 @OgrePsalm33 Personally, I always have tabstop=8 and expandtab enabled when editing code.
Not everyone feels that way, however. There are some who feel an indent should be a single tab
character, so the width is adjustable by the reader. I don't think these people are exclusively (or even
mostly) vim users... – Laurence Gonsalves Oct 7 '14 at 0:42

4 Can someone help me understand why this answer says to use tabstop=8 for indents that consist of
4 spaces? I couldn't get the reason after reading the descriptions of the various settings. Another answer
says to use tabstop=4 which makes a little bit more sense to me. – Kodos Johnson Jan 16 '19 at 1:09

To define this on a permanent basis for the current user, create (or edit) the .vimrc file:

727 $ vim ~/.vimrc

Then, paste the configuration below into the file. Once vim is restarted, the tab settings will
apply.

set tabstop=4 " The width of a TAB is set to 4.


" Still it is a \t. It is just that
" Vim will interpret it to be having
" a width of 4.

set shiftwidth=4 " Indents will have a width of 4

set softtabstop=4 " Sets the number of columns for a TAB

set expandtab " Expand TABs to spaces

edited Aug 20 '14 at 19:28 answered Dec 10 '09 at 6:20


CJBS Alan Haggai Alavi
11.3k 5 64 113 62.5k 15 91 121

If you do not have ~/.vimrc, try with /etc/vim/vimrc. In my case on Ubuntu 16.04 the config file is there. But
keep in mind that changing this file will cause the configuration to be used globaly. – Todor Todorov Oct 11
'16 at 7:43

3 One more thing, use :retab to convert existing tab to spaces .


vim.wikia.com/wiki/Converting_tabs_to_spaces – DawnSong Nov 10 '16 at 14:39

or shorthand for vim modeline:

54 vim :set ts=4 sw=4 sts=4 et :

answered Dec 10 '09 at 6:22


zen
10.1k 4 21 16

I copied and pasted this into my .vimrc file:

https://stackoverflow.com/questions/1878974/redefine-tab-as-4-spaces 3/6
17/01/2020 vim - Redefine tab as 4 spaces - Stack Overflow

25 " size of a hard tabstop


set tabstop=4

" always uses spaces instead of tab characters


set expandtab

" size of an "indent"


set shiftwidth=4

The first 2 settings mean that when I press Tab I get 4 spaces. The third setting means that
when I do V> (i.e. visual and indent) I also get 4 spaces.

Not as comprehensive as the accepted answer but it might help people who just want something
to copy and paste.

answered Jun 28 '13 at 10:49


Snowcrash
56.2k 47 166 267

There are few settings which define whether to use spaces or tabs.

So here are handy functions which can be defined in your ~/.vimrc file:
21
function! UseTabs()
set tabstop=4 " Size of a hard tabstop (ts).
set shiftwidth=4 " Size of an indentation (sw).
set noexpandtab " Always uses tabs instead of space characters (noet).
set autoindent " Copy indent from current line when starting a new line
(ai).
endfunction

function! UseSpaces()
set tabstop=2 " Size of a hard tabstop (ts).
set shiftwidth=2 " Size of an indentation (sw).
set expandtab " Always uses spaces instead of tab characters (et).
set softtabstop=0 " Number of spaces a <Tab> counts for. When 0, featuer is off
(sts).
set autoindent " Copy indent from current line when starting a new line.
set smarttab " Inserts blanks on a <Tab> key (as per sw, ts and sts).
endfunction

Usage:

:call UseTabs()
:call UseSpaces()

To use it per file extensions, the following syntax can be used (added to .vimrc ):

au! BufWrite,FileWritePre *.module,*.install call UseSpaces()

See also: Converting tabs to spaces.

Here is another snippet from Wikia which can be used to toggle between tabs and spaces:

https://stackoverflow.com/questions/1878974/redefine-tab-as-4-spaces 4/6
17/01/2020 vim - Redefine tab as 4 spaces - Stack Overflow

" virtual tabstops using spaces


set shiftwidth=4
set softtabstop=4
set expandtab
" allow toggling between local and default mode
function TabToggle()
if &expandtab
set shiftwidth=8
set softtabstop=0
set noexpandtab
else
set shiftwidth=4
set softtabstop=4
set expandtab
endif
endfunction
nmap <F9> mz:execute TabToggle()<CR>'z

It enables using 4 spaces for every tab and a mapping to F9 to toggle the settings.

edited Sep 11 '19 at 9:50 answered Jan 22 '18 at 21:37


kenorb
89.9k 40 481 511

Put your desired settings in the ~/.vimrc file -- See below for some guidelines and best
practices.
14 There are four main ways to use tabs in Vim:

1. Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4 (or 3 or whatever you prefer)
and use 'noexpandtab'. Then Vim will use a mix of tabs and spaces, but typing and will
behave like a tab appears every 4 (or 3) characters.

Note: Setting 'tabstop' to any other value than 8 can make your file appear wrong in many
places (e.g., when printing it).
2. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use 'expandtab'. This way you will
always insert spaces. The formatting will never be messed up when 'tabstop' is changed.
3. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a |modeline| to set these values
when editing the file again. Only works when using Vim to edit the file.
4. Always set 'tabstop' and 'shiftwidth' to the same value, and 'noexpandtab'. This should then
work (for initial indents only) for any tabstop setting that people use. It might be nice to have
tabs after the first non-blank inserted as spaces if you do this though. Otherwise aligned
comments will be wrong when 'tabstop' ischanged.

Source:

vimdoc.sourceforge.net/htmldoc/options.html#'tabstop'
:help tabstop

edited Jan 24 '16 at 23:42 answered May 7 '14 at 1:45


Mateusz Piotrowski ElasticThoughts
4,990 6 36 58 3,026 5 34 52

+1 for explaining why setting tabstop != 8 can be bad! – Ogre Psalm33 Oct 2 '14 at 13:09

https://stackoverflow.com/questions/1878974/redefine-tab-as-4-spaces 5/6
17/01/2020 vim - Redefine tab as 4 spaces - Stack Overflow
3 This is great, but you should note that this is verbatim from
vimdoc.sourceforge.net/htmldoc/options.html#'tabstop' or :help tabstop in vim. – belacqua Jan 13 '16
at 2:47

One more thing, use


:retab

9 to convert existing tab to spaces http://vim.wikia.com/wiki/Converting_tabs_to_spaces

edited Jan 4 '17 at 14:58 answered Nov 10 '16 at 14:40


DawnSong
2,068 1 22 27

Add line
set ts=4

6 in
~/.vimrc file for per user
or
/etc/vimrc file for system wide

edited Jun 7 '17 at 12:12 answered Jul 19 '16 at 14:12


Alok Singh Mahor
3,714 3 27 42

:set sw=4

5 See Mastering the VI editor

edited Sep 1 '15 at 18:10 answered Dec 10 '09 at 6:21


izstas Amarghosh
4,186 3 34 50 51.9k 10 83 113

My basic ~/.vimrc with comment:

1 set number " show line number


set tabstop=2 " set display width of tab; 1 tab = x space with
set expandtab " transform tab to x space (x is tabstop)
set autoindent " auto indent; new line with number of space at the beginning same
as previous
set shiftwidth=2 " number of space append to lines when type >>

answered Aug 25 '19 at 14:07


o0omycomputero0o
2,078 1 21 33

https://stackoverflow.com/questions/1878974/redefine-tab-as-4-spaces 6/6

You might also like