embear / vim-localvimrc

Search local vimrc files (".lvimrc") in the tree (root dir up to current dir) and load them.
http://www.vim.org/scripts/script.php?script_id=441
GNU General Public License v3.0
484 stars 28 forks source link

Add setting to disable auto-loading on start #62

Closed bew closed 3 years ago

bew commented 6 years ago

Hello,

I don't want to save my loading decision in a persistent file, but I don't want either to choose what to do at every vim startup (for example when editing an unrelated file, or writing a commit message).

Can you add a setting like g:localvimrc_load_on_startup that would be defaulted to 1.

Thank you

embear commented 6 years ago

Sorry, but I don't really understand the request. If you don't want to use localvimrc on startup you already have different options:

What would the setting g:localvimrc_load_on_startup really do? Disable localvimrc completely? Do you plan to use this setting on vim startup as commandline option? Could you please describe the use case in more detail?

bew commented 6 years ago

Sure! I agree it wasn't very clear..

Basically I have a directory where I need some specific compilation flags for the project inside, so I've put them in a .lvimrc file in this directory. So far so good, when I open vim (or neovim in this case) to work on this project, it asks me if I want to load the localvimrc file.

Now, when I'm doing something else in this directory with vim (editing the README, a TODO file or writing a commit message), it will also ask me if I want to load the localvimrc file which I never want, since it only contains stuff related to the project itself.

This is why I'd want a flag to disable the autoloading of localvimrc file, and a command to enable it for this session (like :LocalVimRCEnable).

Is it more clear?

embear commented 6 years ago

OK, now I understand.

Current HEAD now contains LocalVimRCEnable and LocalVimRCDisable commands as well as g:localvimrc_enable option to change the default.

Additionally I added the possibility to change the pattern for the autocommand that sources the localvimrc files. This way you can change the behavior so the plugin only sources the localvimrc files for example C++ sources by setting let g:localvimrc_event_pattern = "*.cpp,*.hpp"

bew commented 6 years ago

Nice! So I've set g:localvimrc_enable to 0, and it indeed correctly disables the loading on startup. Then on :LocalVimRCEnable it seems to not do anything, I thought it would ask me to accept the localvimrc file, no?

embear commented 6 years ago

The command :LocalVimRCEnable does not trigger the loading of the local vimrc file. It just enables the processing according to the configured g:localvimrc_event again. The default is BufWinEnter. So if you change buffer and/or window it will trigger loading. I'm not sure what would be the right behavior.

Especially the last option may be very annoying if the user has loaded a lot of buffers and is asked for every buffer if he wants to load the file.

None of the above options will do everything correctly in all possible use cases. For each of them there are cases where the local vimrc is erroneously loaded/not loaded.

bew commented 6 years ago

I don't think there is a right behavior here.

I would say: keep the current behavior, and add 2 commands:

or something like that.

And add a note in the documentation that LocalVimRC won't auto load things ? Also maybe the 2 commands I suggested above could call LocalVimRCEnable if it's not enabled? But then I'm not sure how LocalVimRCEnable would be useful anymore if it can be bypassed..

embear commented 6 years ago

I made up my mind during my run today :-)

I don't want to add two more commands for this "corner use case" (my very personal opinion). Instead I decided to trigger the loading of local vimrc files for the current buffer if the call to LocalVimRCEnable really enables the plugin. After that the installed autocommand will kick in if you change to another buffer. I think this is the most natural behavior.

I added some more information to the help file to document this behavior.

embear commented 6 years ago

No comments in a week. Seems to be fixed/accepted. Closing

bew commented 6 years ago

Yes it's good, sorry for not getting back to you Thank you for your time on this

MuawizChaudhary commented 3 years ago

Hi @embear,

I am trying to use this plugin and have also encountered a similar error upon loading. Mind if I can ask for some help? I am using vim 8.

localvimrc: >>>>>>>> start content of /home/muawiz/.lvimrc
localvimrc: let g:localvimrc_enable=0
localvimrc: let g:localvimrc_persistence_file="/home/muawiz"
localvimrc: let g:localvimrc_ask=0
localvimrc: let g:localvimrc_sandbox=0
localvimrc: let g:localvimrc_persistent=2
localvimrc: <<<<<<<< end  content of /home//muawiz/.lvimrc
localvimrc: source /home/muawiz/.lvimrc? ([y]es/[n]o/[a]ll/[s]how/[q]uit)

and then it will keep on asking me if I want to source everytime I use vim.

embear commented 3 years ago

Hi @MuawizChaudhary,

you are facing a chicken and egg issue here. You are expecting localvimrc to respect settings in advance of loading them. There is nothing (secure) that localvimrc can do about that. You have two options:

  1. Add all the settings (or at least the g:localvimrc_enable) to your normal vimrc file.
  2. Put the localvimrc plugin into the directory pack/localvimrc/opt/localvimrc in your runtimepath and enable localvimrc only if you need it using :packadd localvimrc. Please see :he packadd and :he runtimepath
taf2 commented 3 years ago

Anyway to just disable autoload? Using vim in a log file is nearly impossible :(. Gotta be away :)

embear commented 3 years ago

Anyway to just disable autoload? Using vim in a log file is nearly impossible

@taf2,

To be honest: I don't understand neither the use case nor the problem here. Maybe you can elaborate a little more.

The purpose of localvimrc is to have a per project configuration and not a per machine configuration. As already mentioned, there are several options to disable localvimrc on startup. Disabling localvimrc will not solve the problem of @MuawizChaudhary that settings for localvimrc should be read by localvimrc and at the same time shall apply during the reading process.

If you would like to add a per machine configuration I would recommend to put for example a file hostspecific.vim into your runtime path and put the following to your VIMRC:

runtime! hostspecific.vim
taf2 commented 3 years ago

@embear sorry to be clear I am mistaken to respond here - the issue is vim specific I do not think it's a localvimrc issue

The issue I am trying to solve is when splitting a screen with code in one view and a log file in the other if you ever switch to another window and come back that view that has the log screen open locks up with a prompt that is nearly impossible to respond to when the log file is huge... i.e. i'm looking for an option to prevent vim from auto trying to load a file that has changed... I can explicitly :e if i wanted to reload the file...

Again sorry this is not a localvimrc issue but this thread does seem to rank very high for google searches related to the issue... which may be more descibed as a question regarding what the option would be to disable this setting in vim from autoloading a local file that has changed... iirc vim 7 didn't do this and sometime in the vim 8 series is when this new autoloading behavior started...

embear commented 3 years ago

Is it possible that you switched on autoread? At least this option describes exactly your "problem"

When a file has been detected to have been changed outside of Vim and
it has not been changed inside of Vim, automatically read it again.

Just try :set noautoread and see if the behavior changes.

taf2 commented 3 years ago

@embear thank you so much! That goes above what I was hoping for but hopefully helps others who might google and find this - thank you!