0

I thought this was straightforward i.e. put the command that works in the buffer viz "set ts=4" in vimrc but this does not seem to work if I also want the functionality provided by vimrc_example, which I do.

I started with a fresh install of Vim 8.1 and added "set ts=4" to the end. When I fired up Vim, tabstop was set to 8. I looked more carefully at what vimrc does and found that there are all sorts of complicated things going on, including instructions that prevent subsequent instructions having any effect - aha!

I made a new vimrc that contains the one instruction "set ts=4" and found that works in the sense that tabstop is 4 when I run Vim. But of course there is none of the functionality that I want and expect from Vim e.g. syntax highlighting, so I tried what I thought was a minimal way of getting from something that worked to what I want. I made a vimrc that reads, in its entirety:

source $VIMRUNTIME/vimrc_example.vim
set ts=4

This provides syntax highlighting etc but tabs are back at 8.

Digging a bit deeper, vimrc_example.vim calls defaults.vim and if I take that call out then tabs are set at 4 so the problem seems to be with defaults.vim. I had a look at defaults.vim and there are no occurrences of "set tabstop" or "set ts" or any mention of tabs so I am mystified. In desperation I stuck "set ts=4" on the end of defaults.vim and re-enabled the call to defaults.vim and that didn't work either.

migrated from superuser.com May 29 at 19:36

This question came from our site for computer enthusiasts and power users.

  • Update. One solution is to use Vim 7.4 instead. – user1476044 May 29 at 17:14
  • Check out this post on debugging your vimrc: vi.stackexchange.com/q/2003/5298 – Herb Wolfe May 29 at 20:59
  • You can execute :verbose set ts? to see where it was last set. – Ralf May 30 at 19:25
0

There are a couple of things running amok here: when no vimrc exists for the user, vim sources the defaults.vim file, as you know. However, once that vimrc is created, it no longer does so.

Vim also comes with an example.

The standard way to create your own vimrc is to do the following:

  1. Create your vimrc (vim ~/.vim/vimrc, or vim ~/.vimrc for older versions)
  2. Copy from the defaults and example scripts the lines you want, and embark on the long quest to continually customize your vim.

So, in your case, the vimrc could look like

syntax on
filetype plugin indent on
set tabstop=4

Finally, a quick recommendation from someone who lives in Vim: leave tabs at width 8, unless you’re going to use exclusively tabs. I use spaces, and adjust the shiftwidth and related settings per-filetype. So tabs always display as 8. See my Dotfiles for how I do it.

  • How does your advice (1) translate for Windows users? – user22480 May 30 at 17:40
  • Windows users may need to adjust the path. See :help vimrc I believe – D. Ben Knoble May 30 at 19:24
  • @user22480 see my comment; i believe your answer was retroactively converted to a comment. – D. Ben Knoble May 31 at 16:00
  • Windows uses the file $HOME\_vimrc (instead of .vimrc). Create a new file C:\Users\<USERNAME>\_vimrc and then you can edit your configuration there. – otter.pro Jul 1 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy