4

Many Vim's plugin's settings are specified by let g:something = value. I have a need for setting buffer-local values of some variables (actually with vim-localvimrc)

I know there's a way to outwit by using a condition (Setting buffer specific variables in Vim). But not all these autocmds are under my control, since some are set by other plugins. Is there anything like setlocal for global variable?

2
  • What are you trying to do? (XY Problem) Commented Aug 11, 2014 at 2:31
  • There's a setting in spf13-vim for deleting trailing whitspace when saving by detecting g:spf13_keep_trailing_whitespace. Generally I don't need to keep trailing whitespace, but that's not for some projects. Commented Aug 11, 2014 at 2:48

2 Answers 2

3

There are many ways to do to what you actually want (strip trailing whitespace for some projects) but I don't think there is anyway to shadow a global variable with a buffer local one (which is what you asked).

One way to do this is to add

autocmd BufWritePre /path/to/project/* call StripTrailingWhitespace()

the following to your vimrc. Which will call the function StripTrailingWhitespace for any file under /path/to/project (including subdirectories). StripTailingWhitespace is defined in spf13.

You can probably generate the path to the project dynamically by looking at the path to the local vimrc file.


Another way would be to change the condition spf13 uses to call StripTrailingWhitespace

The StripTrailingWhitespace autocmd in spf13 is defined as the following. (I added newlines so I'm not sure if this will work.)

autocmd FileType c,cpp,java,go,php,javascript,python,twig,xml,yml,perl
\   autocmd BufWritePre <buffer>
\       if !exists('g:spf13_keep_trailing_whitespace') |
\           call StripTrailingWhitespace() |
\       endif

So you would change it to !exists('g:spf13_keep_trailing_whitespace') to some other condition like checking if in whitelisted projects or check a buffer local variable.


General recommendation would be to get rid of any vim distribution. It is very hard to understand what is in them when you are first starting out. It then becomes very hard to modify them. It is generally better to start from scratch and add plugins and mappings when you find something lacking. It will take longer but you will understand everything in your vim configuration.

Sign up to request clarification or add additional context in comments.

1 Comment

+1: "General recommendation would be to get rid of any vim distribution"
1

In your case, the plugin you are using shall accept overriding global variables with buffer local variables. Indeed it can be done by changing the condition -- in lh-vim-lib there is a lh#option#get() function that'll be easy to adapt to spf13. If you think it makes sense, contact spf13 maintainers or open an issue in the project bug tracker.

Another solution would rely on defining a local_vimrc script for each project you are working on. In some projects you'll have a :let g:spf13_keep_trailing_whitespace = 1, in other you'll have a :silent! unlet g:spf13_keep_trailing_whitespace -- usually it is accomplished with a :let g:var=0, but the current condition ignores the variable value.

2 Comments

local_vimrc hack isn't that solid. I often have multiple tabs for different projects, and I sometimes need to switch back between them. Modifying global state in one tab will also effect other tabs. Unfortunately, most plugins provide options based on global variable only. I believe using different Vim instances for different projects may solve the problem. However, that enforce me to change my habit.
The only limitation of local_vimrc solutions is that we need to define a project-script per project in order to re-set global variables every time we enter a buffer -- I expect your local_vimrc plugin to also reload the project-script every time a buffer is entered, as my plugin does.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.