Search code examples
vimtabsvispace

Getting tabs in vim instead of spaces even with `set expandtab`


I create the file "tabtest" by vi tabtest and then typing:

a
tab
b
newline  
c
tab
d  
CTRL-C
:
wq  

I have set expandtab in my `.vimrc. But I'm still getting tabs in the file instead of spaces.

(base) balter@ab-rstudio:~$ cat -A tabtest
a^Ib$
^Ic^Id$
(base) balter@ab-rstudio:~$ cat .vimrc
let python_highlight_all = 1

set undodir=${HOME}/.vim/undo/
set backupdir=${HOME}/.vim/backup/
set directory=~/.vim/swp/
set writebackup
set noswapfile

set autoindent
set showmatch
set mouse=a
syntax on

set tabstop=4
set softtabstop=0
set shiftwidth=4
set textwidth=79
set expandtab
set smarttab
set autoindent
set fileformat=unix

set backspace=indent,eol,start
set whichwrap+=<,>,h,l,[,]

set laststatus=2
set statusline+=%F

colorscheme murphy

set paste

Solution

  • One of the golden rules of tool customization is to only put stuff you understand in your config. The first step toward that understanding is to read (not skim) the documentation for any option or command you plan to add and then, armed with that newfound knowledge, ponder the pros and cons of doing so.

    :help 'paste' is a very powerful option with many explicitly documented side effects, such as:

    When the 'paste' option is switched on (also when it was already on):
            [...]
            - 'expandtab' is reset
            [...]
    

    That option should never be set in one's vimrc because of all those nasty side effects that are desirable only when using the terminal/system's pasting facilities but very undesirable the rest of the time. Remove it.

    Here is how it generally goes:

    1. user tries to paste something with the terminal's "paste" thing and gets garbled text,
    2. user looks it up on the internet, learns about the paste option, and never bothers to read its documentation,
    3. user quickly gets tired of that :set paste<CR><whatever>:set nopaste<CR> dance and decides to add set paste to their vimrc so that it is always on,
    4. user notices issues with indenting or insert mode mappings but is unable to relate them to their nifty idea of adding set paste to their vimrc… because they have no idea of what the option actually does,
    5. user turns once again to the internet, which reminds them that reading the doc in due time would have made them aware of the pitfalls of set paste and led them to :help 'pastetoggle', which solves the repetitiveness issue nicely without adding set paste to their vimrc,
    6. user is, hopefully, enlightened.

    But, of course, the real solution is to get a proper Vim with clipboard support, but that's another, beaten to death, story.