Ricky
Ricky

Reputation: 35843

Redefine tab as 4 spaces

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

Upvotes: 1373

Views: 1158916

Answers (15)

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143144

It depends on what you mean.

  • Do you want tab characters in your file to appear 4 character cells wide?
  • Or do you want the Tab key to generate an indent consisting of 4 space characters?

Depending on which behavior you need, one of the following sets of settings should work:

If you want tab characters in your file to appear 4 character cells wide:

set tabstop=4

If your code requires use of actual tab characters these settings prevent unintentional insertion of spaces (these are the defaults, but you may want to set them defensively):

set softtabstop=0 noexpandtab

If you also want to use tab characters for indentation, you should also set shiftwidth to be the same as tabstop:

set shiftwidth=4

To make any of these settings permanent add them to your vimrc.

If you want pressing the Tab key to indent with 4 space characters:

First, tell vim to use 4-space indents, and to intelligently use the Tab key for indentation instead of for inserting tab characters (when at the beginning of a line):

set shiftwidth=4 smarttab

If you'd also like vim to only use space caharacters, never tab characters:

set expandtab

Finally, I also recommend setting tab stops to be different from the indentation width, in order to reduce the chance of tab characters masquerading as proper indents:

set tabstop=8 softtabstop=0

To make any of these settings permanent add them to your vimrc.

More Details

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, along with links their the full documentation.


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 measured in spaces, so if your codebase indents with tab characters then you want shiftwidth to equal tabstop. This is also used by commands like =, > and <.

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.

If you have this enabled you can enter a literal tab character by typing ctrlV followed by Tab. (mnemonic "verbatim tab")

smarttab

Enabling this will cause the Tab key (in insert mode) go to the next indent (as set by 'shiftwidth') instead of the next tab stop (or soft tabstop), but only when the cursor is at the beginning of a line (i.e. the only preceding characters are whitespace). Spaces and/or tabs will be used depending on the values of other options.

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. Codebases where setting this to something other than 0 is desired are extremely rare.


For further details on any of these you can also use :help 'optionname' in vim (e.g. :help 'tabstop').

Upvotes: 2000

bcassell
bcassell

Reputation: 46

If you set your tabstop to be a different value than your indent width, as suggested in Laurence's answer, you will be unable to interoperate in a company codebase that lacks a tabs/notabs coding-style mandate. Thus, I always set them to be the same value, as has been done in most of the other answers.

Upvotes: 0

for any one new to neovim, if you are wondering how to replicate this configurations in lua, you do it this way:

set tabstop = 4 -> vim.opt.tabstop = 4

set expandtab -> vim.opt.expandtab = true

set shiftwidth = 4 smarttab ->

   vim.opt.shiftwidth = 4 
   vim.opt.smarttab = true

Upvotes: 1

weshouman
weshouman

Reputation: 933

For permanent change, create the file ~/.vim/plugin/tab_expander.vim with the content

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

To prevent touching the ~/.vimrc, thus keeping other default settings untouched.

Upvotes: 1

DawnSong
DawnSong

Reputation: 5162

One more thing, use
:retab
to convert existing tab to spaces http://vim.wikia.com/wiki/Converting_tabs_to_spaces

Upvotes: 20

Vivian De Smedt
Vivian De Smedt

Reputation: 1029

Make sure vartabstop is unset

set vartabstop=

Set tabstop to 4

set tabstop=4

Upvotes: 2

Vasilii Suricov
Vasilii Suricov

Reputation: 954

Permanent for all users (when you alone on server):

# echo "set tabstop=4" >> /etc/vim/vimrc

Appends the setting in the config file. Normally on new server apt-get purge nano mc and all other to save your time. Otherwise, you will redefine editor in git, crontab etc.

Upvotes: 4

kenorb
kenorb

Reputation: 166379

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:

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:

" 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.

Upvotes: 68

o0omycomputero0o
o0omycomputero0o

Reputation: 3534

My basic ~/.vimrc with comment:

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 >> 

Upvotes: 7

Alok
Alok

Reputation: 10544

Add line
set ts=4
in
~/.vimrc file for per user
or
/etc/vimrc file for system wide

Upvotes: 11

ElasticThoughts
ElasticThoughts

Reputation: 3477

Put your desired settings in the ~/.vimrc file -- See below for some guidelines and best practices.

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:

Upvotes: 17

Amarghosh
Amarghosh

Reputation: 59451

:set sw=4

See Mastering the VI editor

Upvotes: 7

Alan Haggai Alavi
Alan Haggai Alavi

Reputation: 74222

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

$ 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

Upvotes: 832

Snowcrash
Snowcrash

Reputation: 86097

I copied and pasted this into my .vimrc file:

" 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.

Upvotes: 31

zen
zen

Reputation: 13083

or shorthand for vim modeline:

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

Upvotes: 76

Related Questions