For some reason the set expandtab
command in my .vimrc
file is not having any effect.
Here is my .vimrc
:
" tab settings
set expandtab
set smarttab
set softtabstop=2
set tabstop=2
set shiftwidth=2
set paste
However, when I run vi
(no file name) the :set
command emits:
:set
--- Options ---
helplang=en shiftwidth=2 ttyfast
paste tabstop=2 ttymouse=xterm2
fileencodings=ucs-bom,utf-8,default,latin1
which indicates that the expandtab
option is not set. This is further confirmed by executing :set expandtab?
which returns with noexpandtab
.
I'm on OSX 10.10, and vi --help
returns:
$ vi --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Jun 20 2016 11:11:25)
MacOS X (unix) version
Included patches: 1-1847
Compiled by Homebrew
How come some settings in my .vimrc are being honored, but not set expandtab
?
To convert tabs to spaces in the currently opened file in Vim, enter the Normal mode by pressing Esc key. Now use the retab command by pressing the ':' (colon) character and Vim will convert the existing tabs to spaces.
Use expand tab to convert new tabs to spaces The expandtab property will ensure that when you hit tab it will actually use spaces. So first set the number of spaces a tab should be, then set expandtab. set tabstop=2 shiftwidth=2 expandtab. Tabstop determines how many columns a tab counts for.
tabstop is effectively how many columns of whitespace a \t is worth. shiftwidth is how many columns of whitespace a “level of indentation” is worth. Setting expandtab means that you never wanna see a \t again in your file.
From the documentation on expandtab
:
This option is reset when the
paste
option is set and restored when thepaste
option is reset.
BTW, you probably don't want paste
to be set all the time. It's commonly used for pasting into a terminal Vim to avoid messing up indentation, etc.
To conveniently turn paste
on and off with one keypress you can add this shortcut (change <F2>
to whatever key you want):
set pastetoggle=<F2>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With