Reputation: 20580
In a vim plugin, how can I tell if a user has already set a variable in their .vimrc file?
For instance, I have in .vimrc
:
set shiftwidth=2
Then I load a plugin that has
set shiftwidth=3
So I think to modify the plugin like so:
if !exists("shiftwidth")
set shiftwidth=3
endif
But when I load up a new vim window with the revised plugin loaded, my tabs are still set to 3
and not 2
.
How can I make it so that it only sets shiftwidth=3
unless otherwise specified in .vimrc
?
Upvotes: 1
Views: 759
Reputation: 22266
These commands should show where shiftwidth
was set:
:set verbose=15
:set shiftwidth
:set verbose=0
If you want to programmatically do something with that info you'd need to redirect
the verbose output and parse it for what you want:
:redir => myvariable
:set verbose=15
:set shiftwidth
:set verbose=0
:redir END
myvariable
will now have text that would otherwise have been printed to screen.
REVISED ANSWER Here is a way I think you could do what you clarify in your comments.
Add a last line to user's vimrc to save the current value of shiftwidth to a global variable. The value will be saved before any plugins are loaded, unless plugins are explicitly sourced in the vimrc before the last line. You can then reset shiftwidth to this value in your own plugin.
[everything in vimrc comes before this line]
:let g:vimrc_shiftwidth = &shiftwidth
You can programmatically add this line with something like the write >> [file]
command. Presumably you would include a comment indicating what plugin had added this command to the vimrc. Also, I don't think this would capture the correct value, e.g., in case where user uses exrc option and sets shiftwidth in a different vimrc. All in all, still not recommended.
Upvotes: 2