1

I am working on a machine where I do not have root access, and I do not like some of the global vim settings (in /etc/vimrc.local).

How can I disable these in my personal .vimrc?

Specifically, the global file has many

autocmd FileType XXX setlocal ...

which differ from my preferences. Besides redefining every filetype in my local file, is there any other way to fix this?

EDIT more information:

The relevant portion of /etc/vimrc.local is

""" indentation schema
set autoindent              " enable automatic indentation
" set default indentation scheme
filetype plugin indent on   " indent based on filetype
set tabstop=2
set shiftwidth=2
set expandtab               " insert spaces instead of tab
" set language-specific indentation schema
if has("autocmd")
  " use real tabs in Makefiles
  autocmd FileType make setlocal tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab
  " indentation is tab/width=8,4 in C's KNF style
  autocmd FileType c setlocal tabstop=8 shiftwidth=8 softtabstop=4 noexpandtab
  autocmd FileType cpp setlocal tabstop=8 shiftwidth=8 softtabstop=4 noexpandtab
  " indentation is two spaces in Ruby
  autocmd FileType ruby setlocal tabstop=2 shiftwidth=2 expandtab
  " indentation for shell scripts
  autocmd FileType sh setlocal tabstop=2 shiftwidth=2 expandtab
  " indentation is four spaces in python - also set by python.vim
  autocmd FileType python setlocal tabstop=4 shiftwidth=4 expandtab
endif

In my case, I don't like the c/cpp rules defined here; I'm wondering what I can put in my .vimrc to override. Currently, my .vimrc has

set tabstop=2
set shiftwidth=2
set autoindent
set expandtab " spaces instead of tabs

but the autocmd options are taking precedence.

  • 1
    Can you edit in the file content? It may help to identify other solutions. In general, I’m not aware of a way to skip the system vimrc, only override it. – D. Ben Knoble Mar 4 at 4:38
  • Well, you can always do vim -u NONE and then :source ~/.vimrc. – klaus Mar 4 at 6:53
  • But I don't want to ignore all the settings in the global file. Then I need to pick individually turn all of them back on in my local file. – baum Mar 4 at 20:50
  • As a temporary fix, I have redefined the autocmd FileType ... for those filetypes where I want different settings. It's annoying, manual, and not robust, but it'll have to do. – baum Mar 5 at 3:53
0

The only "clean" way I see, is to replicate the autocmds in your own setup and correct the option values with them (you already mentioned that in the comments). Autocmds for the same event (here FileType) are executed in the order they are defined.

So you have to add the following to your vimrc:

autocmd FileType c setlocal ....

Do you have contact to the person(s) responsible for /etc/vimrc.local? If yes, you could ask them to wrap the autocmds in a augroup like:

if has("autocmd")
  augroup vimrc_local
    " use real tabs in Makefiles
    autocmd FileType make setlocal tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab
    ...
  augroup END
endif

Now all the autocmds from vimrc.local are grouped under vimrc_local. With that you could add the following to your vimrc:

autocmd! vimrc_local

This will delete all autocmds from the group vimrc_local.


Could I use autocmd! without a augroup?

Yes, but this will delete all autocommands in the "default" group. The docs (:help autocmd-remove) contains warnings like:

Warning: You should normally not do this without a group, it breaks plugins, syntax highlighting, etc.

You might get away with it, if you use it as the first command in your vimrc and no plugins or syntax settings are in the global vimrc.

You could test yourself with the following.

Open Vim and execute

:put =execute('autocmd')

This will put all defined autocmds into the current buffer. Save it as a1 and exit.

Add autocmd! as the first line in your vimrc.

Again open Vim and execute:

:put =execute('autocmd')

Save the buffer as a2.

Now you can diff a1 and a2 and see which autocmds were removed. Then you decide for yourself, whether this might work for you.

If you see some strange behavior later on, disable the autocmd! to check if that might be the reason.

Anyway I would recommend to talk to the vimrc.local guys first.

Your Answer

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

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