0

I would like vim to only ever insert a space character when I touch the space bar. Instead vim decides that sometimes tabs should be 4 spaces. How do I force vim to always and forever use tabs regardless of what any other vim config in my system says to do? No matter what I place in .vimrc it still gives me spaces.

2
  • 2
    Uh, vim should by default insert a space when you hit the space bar? I’m confused by your first sentence. That said, you can control the tab settings with expandtab, which may be set by ftplugin files.
    – D. Ben Knoble
    Mar 22, 2020 at 13:19
  • @D.BenKnoble I tripped over that sentence too. The OP means that they never want a space entered if they haven't pressed the space bar. (Not that they don’t want the space bar to insert anything other than a space.)
    – Rich
    Mar 22, 2020 at 19:47

1 Answer 1

2

If I’ve understood you correctly, you want to do something like this in your vimrc:

augroup
  au!
  au FileType * setlocal noexpandtab
augroup

But I cannot guarantee this works (i.e., something might get triggered after this autocommand that sets expandtab). I think you could establish a buffer-local BufEnter autocommand inside the FileType autocommand, but that will be needless complexity if you never run into a situation where this doesn’t work.

I also cannot really recommend this... for one, I prefer spaces :) but additionally, this is very difficult to override when you want spaces!

A far better approach is to put setlocal noexpandtab in ~/.vim/after/ftplugin/{filetype}.vim anytime you run into a filetype that is using spaces where you want tabs.

3
  • 2
    There's also other settings that can cause Vim to insert spaces even under noet, for instance if softtabstop is set and &sts < &ts or if shiftwidth is set, &sw < &ts and you use the > or < commands, or indent/dedent with CTRL-T or CTRL-D or some automatic indentation with autoindent... In short, it's not that easy to force tab indents everywhere...
    – filbranden
    Mar 22, 2020 at 14:31
  • 1
    Too true! @filbranden
    – D. Ben Knoble
    Mar 22, 2020 at 14:40
  • noexpandtab is set in both ~/.vimrc and ~/.vim/after/ftplugin/python.vim. When I hit tab on a blank line, I get a tab. When I hit tab to indent existing text, I get spaces. Mar 22, 2020 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.