Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Search local vimrc files (".lvimrc") in the tree (root dir up to current dir) and load them.

License

NotificationsYou must be signed in to change notification settings

embear/vim-localvimrc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This plugin searches for local vimrc files in the file system tree of thecurrently opened file. It searches for all ".lvimrc" files from the directoryof the file up to the root directory. By default those files are loaded inorder from the root directory to the directory of the file. The filename andamount of loaded files are customizable through global variables.

For security reasons it the plugin asks for confirmation before loading a localvimrc file and loads it using |:sandbox| command. The plugin asks once persession and local vimrc before loading it, if the file didn't change sinceprevious loading.

It is possible to define a whitelist and a blacklist of local vimrc files thatare loaded or ignored unconditionally.

The plugin can be found onGitHub andVIM online.

Installation

Using a vimball archive

Download the archive atVIM online and run

vim -c'so %|q' localvimrc.vmb

Using Vim 8 package management

This is an elegant way to separate plugins from each other and be easily ableto completely remove a plugin later.

mkdir -p~/.vim/pack/localvimrc/start/git clone --depth 1 https://github.com/embear/vim-localvimrc.git~/.vim/pack/localvimrc/start/localvimrcvim -c'packloadall|helptags ALL'

Using a third party package manager

If the installed vim is older than version 8 it is possible to use a thirdparty package manager. For example installvim-plug and add the following toyour global vimrc:

callplug#begin()Plug'embear/vim-localvimrc'callplug#end()

Then actually install the plugin:

vim -c'PlugInstall'

Other options for a package manager arevundle,dein,neobundle,pathogen orpacker.

Commands

TheLocalVimRC command

Resource all local vimrc files for the current buffer.

TheLocalVimRCClear command

Clear all stored decisions made in the past, when the plugin asked aboutsourcing a local vimrc file.

TheLocalVimRCCleanup command

Remove all stored decisions for local vimrc files that no longer exist.

TheLocalVimRCForget command

Remove stored decisions for given local vimrc files.

TheLocalVimRCEdit command

Open the local vimrc file for the current buffer in an split window forediting. If more than one local vimrc file has been sourced, the user candecide which file to edit.

TheLocalVimRCEnable command

Globally enable the loading of local vimrc files if loading has been disabledby |LocalVimRCDisable| or by setting |g:localvimrc_enable| to0 duringstartup.

Enabling local vimrc using this command will trigger loading of local vimrcfiles for the currently active buffer. It willnot load the local vimrc filesfor any other buffer. This will be done by an autocommand later when anotherbuffer gets active and the configured |g:localvimrc_event| autocommand getsactive. This is the case for the default |BufWinEnter|.

TheLocalVimRCDisable command

Globally disable the loading of local vimrc files if loading has been disabledby |LocalVimRCEnable| or by setting |g:localvimrc_enable| to1 duringstartup.

TheLocalVimRCDebugShow command

Show all stored debugging messages. To see any message with this commanddebugging needs to be enabled with |g:localvimrc_debug|. The number of messagesstored and printed can be limited using the setting |g:localvimrc_debug_lines|.

TheLocalVimRCDebugDump command

Write all stored debugging messages to given file. To write any message withthis command debugging needs to be enabled with |g:localvimrc_debug|. Thenumber of messages stored and written can be limited using the setting|g:localvimrc_debug_lines|.

Functions

TheLocalVimRCFinish function

After a call to this function the sourcing of any remaining local vimrc fileswill be skipped. In combination with the |g:localvimrc_reverse| option it ispossible to end the processing of local vimrc files for example at the root ofthe project by adding the following command to the local vimrc file in the rootof the project:

callLocalVimRCFinish()

Variables

The plugin provides several convenience variables to make it easier to set uppath dependent setting like for example makeprg. These variables are onlyavailable inside your local vimrc file because they are only unambiguous there.

Adding the following lines to a local vimrc in the root directory of a projectmodify the behavior of |:make| to change into a build directory and call makethere:

let &l:makeprg="cd".g:localvimrc_script_dir_unresolved."/build && make"

NOTE:

This is only possible if you disable sandbox mode.


Other variables provide a way to prevent multiple execution of commands. Theycan be used to implement guards:

" do stuff you want to do on every buffer enter event" guard to end loading if it has been loaded for the currently edited fileifg:localvimrc_sourced_once_for_filefinishendif" do stuff you want to do only once for a edited file" guard to end loading if it has been loaded for the running vim instanceifg:localvimrc_sourced_oncefinishendif" do stuff you want to do only once for a running vim instance

Theg:localvimrc_file variable

Fully qualified file name of file that triggered loading the local vimrc file.

Theg:localvimrc_file_dir variable

Fully qualified directory of file that triggered loading the local vimrc file.

Theg:localvimrc_script variable

Fully qualified and resolved file name of the currently loaded local vimrcfile.

Theg:localvimrc_script_dir variable

Fully qualified and resolved directory of the currently loaded local vimrcfile.

Theg:localvimrc_script_unresolved variable

Fully qualified but unresolved file name of the currently loaded local vimrcfile.

Theg:localvimrc_script_dir_unresolved variable

Fully qualified but unresolved directory of the currently loaded local vimrcfile.

Theg:localvimrc_sourced_once variable

Set to1 if the currently loaded local vimrc file had already been loaded inthis session. Set to0 otherwise.

Theg:localvimrc_sourced_once_for_file setting

Set to1 if the currently loaded local vimrc file had already been loaded inthis session for the currently edited file. Set to0 otherwise.

Settings

To change settings from their default add similar line to your global |vimrc|file.

letg:option_name=option_value

Theg:localvimrc_enable setting

Globally enable/disable loading of local vimrc files globally. The behavior canbe changed during runtime using the commands |LocalVimRCEnable| and|LocalVimRCDisable|.

  • Value0: Disable loading of any local vimrc files.
  • Value1: Enable loading of local vimrc files.
  • Default:1

Theg:localvimrc_name setting

List of filenames of local vimrc files. The given name can include a directoryname such as ".config/localvimrc".

Previous versions of localvimrc only supported a single file as string. This isstill supported for backward compatibility.

By default the local vimrc files are expected to contain vim script. For Neovimthere is additional support for Lua. In order to use Lua code in the localvimrc, the file name must have the extension.lua.

  • Default:[ ".lvimrc" ]

Theg:localvimrc_event setting

List of autocommand events that trigger local vimrc file loading.

  • Default:[ "BufWinEnter" ]

For more information see |autocmd-events|.


NOTE:

BufWinEnter is the default to enable lines like

setlocalcolorcolumn=+1

in the local vimrc file. Settings "local to window" need to be set for everytime a buffer is displayed in a window.

Because the plugin searches local vimrc files in the path of the currentlyopened file not all autocommand events make sense. For example the event|VimEnter| might cause problems. When that event is emitted it is possible thatno file is opened but some temporary buffer is currently shown. This will leadto an unexpected behavior. If you would like to apply settings only once perrunning Vim instance please use |g:localvimrc_sourced_once_for_file| and|g:localvimrc_sourced_once|. An example how to use those variables in a localvimrc file is described in the introduction to |localvimrc-variables|.


Theg:localvimrc_event_pattern setting

String defining the pattern for which the autocommand events trigger localvimrc file loading.

  • Default:"*"

For more information see |autocmd-patterns|.

Theg:localvimrc_reverse setting

Reverse behavior of loading local vimrc files.

  • Value0: Load files in order from root directory to directory of the file.
  • Value1: Load files in order from directory of the file to root directory.
  • Default:0

Theg:localvimrc_count setting

On the way from root, the last localvimrc_count files are sourced.

NOTE:

This might load files not located in the edited files directory or even notlocated in the projects directory. If this is of concern use theg:localvimrc_file_directory_only setting.

  • Default:-1 (all)

Theg:localvimrc_file_directory_only setting

Just use local vimrc file located in the edited files directory.

NOTE:

This might end in not loading any local vimrc files at all. If limiting thenumber of loaded local vimrc files is of concern use theg:localvimrc_countsetting.

  • Value0: Load all local vimrc files in the tree from root to file.
  • Value1: Load only file in the same directory as edited file.
  • Default:0

Theg:localvimrc_sandbox setting

Source the found local vimrc files in a sandbox for security reasons.

  • Value0: Don't load vimrc file in a sandbox.
  • Value1: Load vimrc file in a sandbox.
  • Default:1

Theg:localvimrc_ask setting

Ask before sourcing any local vimrc file. In a vim session the question is onlyasked once as long as the local vimrc file has not been changed.

  • Value0: Don't ask before loading a vimrc file.
  • Value1: Ask before loading a vimrc file.
  • Default:1

Theg:localvimrc_persistent setting

Make the decisions given when asked before sourcing local vimrc filespersistent over multiple vim runs and instances. The decisions are written tothe file defined by and |g:localvimrc_persistence_file|.

  • Value0: Don't store and restore any decisions.
  • Value1: Store and restore decisions only if the answer was given in upper case (Y/N/A).
  • Value2: Store and restore all decisions.
  • Default:0

Theg:localvimrc_persistence_file setting

Filename used for storing persistent decisions made when asked before sourcinglocal vimrc files.

  • Default (Unix): "$HOME/.localvimrc_persistent"
  • Default (Windows): "$HOME/_localvimrc_persistent"

Theg:localvimrc_whitelist setting

If a local vimrc file matches the regular expression given by|g:localvimrc_whitelist| this file is loaded unconditionally.

Files matching |g:localvimrc_whitelist| are sourced even if they are matched by|g:localvimrc_blacklist|.

See |regular-expression| for patterns that are accepted.

Example:

" whitelist all local vimrc files in users project foo and barletg:localvimrc_whitelist='/home/user/projects/\(foo\|bar\)/.*'" whitelist can also use lists of patternsletg:localvimrc_whitelist=['/home/user/project1/','/opt/project2/','/usr/local/projects/vim-[^/]*/']
  • Default: No whitelist

Theg:localvimrc_blacklist setting

If a local vimrc file matches the regular expression given by|g:localvimrc_blacklist| this file is skipped unconditionally.

Files matching |g:localvimrc_whitelist| are sourced even if they are matched by|g:localvimrc_blacklist|.

See |regular-expression| for patterns that are accepted.

Example:

" blacklist all local vimrc files in shared project directoryletg:localvimrc_blacklist='/share/projects/.*'" blacklist can also use lists of patternsletg:localvimrc_blacklist=['/share/projects/.*','/usr/share/other-projects/.*']
  • Default: No blacklist

Theg:localvimrc_autocmd setting

Emit autocommands |LocalVimRCPre| before and |LocalVimRCPost| after sourcingevery local vimrc file.

  • Default:1

Theg:localvimrc_python2_enable setting

Enable probing whether python 2 is available and usable for calculating localvimrc file checksums, in case |sha256()| is not available.

  • Default:1

Theg:localvimrc_python3_enable setting

Enable probing whether python 3 is available and usable for calculating localvimrc file checksums, in case |sha256()| is not available.

  • Default:1

Theg:localvimrc_debug setting

Debug level for this script. The messages can be shown with|LocalVimRCDebugShow| or written to a file with |LocalVimRCDebugDump|.

  • Default:0

Theg:localvimrc_debug_lines setting

Limit for the number of debug messages stored. The messages can be shown with|LocalVimRCDebugShow| or written to a file with |LocalVimRCDebugDump|.

  • Default:100

Autocommands

If enabled localvimrc emits autocommands before and after sourcing a localvimrc file. The autocommands are emitted as |User| events. Because of thatcommands need to be registered in the following way:

autocmdUserLocalVimRCPreechom'before loading local vimrc'autocmdUserLocalVimRCPostechom'after loading local vimrc'

TheLocalVimRCPre autocommand

This autocommand is emitted right before sourcing each local vimrc file.

TheLocalVimRCPost autocommand

This autocommands is emitted right after sourcing each local vimrc file.

Frequently Asked Questions

modeline settings are overwritten by local vimrc

Depending on the |g:localvimrc_event| that is used to trigger loading localvimrc files it is possible that |modeline| already had been parsed. This mightbe cause problems. If for example there isset ts=8 sts=4 sw=4 et in thelocal vimrc and a Makefile contains# vim: ts=4 sts=0 sw=4 noet this modelinewill not get applied with default settings of localvimrc. There are twopossibilities to solve this.

The first solution is to use |BufRead| as value for |g:localvimrc_event|. Thisevent is emitted by Vim before modelines are processed.

The second solution is to move all those settings to the local vimrc file anduse different settings depending on the |filetype|:

if &ft=="make"setlts=4sts=0sw=4noetelsesetlts=8sts=4sw=4etendif

Project specific settings for other plugins are ignored

Most plugins require to have their configuration variables set when the pluginis loaded. If you want to have project specific settings for those plugins yourun into a chicken and egg problem. This is because localvimrc is only able toload the project specific configuration when a buffer in the project is loaded.By that time the other plugins are already loaded and the project specificconfigurations are most likely ignored. A solution to this is to make theplugin you want to configure an optional plugin (see |:packadd|). This way itis possible to do the settings in the local vimrc file and activate the pluginafterwards. To do so add something like this to your local vimrc file:

" NOTE: <PLUGIN> needs to be replaced with the directory name of the pluginif!g:localvimrc_sourced_once" add your <PLUGIN> settings hereletg:plugin_setting=1000" late loading of pluginpackadd<PLUGIN>endif

Contribute

To contact the author (Markus Braun), please send an email tomarkus.braun@krawel.de

If you think this plugin could be improved, fork onGitHub and send a pullrequest or just tell me your ideas.

If you encounter a bug please enable debugging, export debugging messages toa file and create a bug report onGitHub. Debug messages can be enabledtemporary and exported to a file calledlocalvimrc_debug.txt on command linewith the following command:

vim --cmd"let g:localvimrc_debug=99" -c"LocalVimRCDebugDump localvimrc_debug.txt" your_file

Credits

  • Simon Howard for his hint about "sandbox"
  • Mark Weber for the idea of using checksums
  • Daniel Hahler for various patches
  • Justin M. Keyes for ideas to improve this plugin
  • Lars Winderling for whitelist/blacklist patch
  • Michon van Dooren for autocommands patch
  • Benoit de Chezell for fix with nested execution
  • Huy Le for patch to support Lua scripts in Neovim

Changelog

v3.2.0 : 2024-10-25

  • add command |LocalVimRCDebugDump| to write debug messages to a file.
  • add support for Lua scripts when using Neovim.
  • for search path fallback to the current working directory, it checks for an existing directory of the current buffer rather than an empty directory name.
  • fix a bug with printing error messages

v3.1.0 : 2020-05-20

  • add option to disable probing of Python versions
  • prevent recursive sourcing of local vimrc files
  • better handling of syntax errors in sourced local vimrc files

v3.0.1 : 2018-08-21

  • fix a compatibility issue with unavailable |v:true| and |v:false| in Vim version 7.4

v3.0.0 : 2018-08-14

  • use SHA256 as for calculating checksums and use FNV-1 as fallback.
  • add command |LocalVimRCCleanup| to remove all unusable persistence data.
  • add command |LocalVimRCForget| to remove persistence data for given files.
  • add command |LocalVimRCDebugShow| to show debug messages.
  • add setting |g:localvimrc_debug_lines| to limit the number of stored debug messages.

v2.7.0 : 2018-03-19

  • add setting |g:localvimrc_enable| and commands |LocalVimRCEnable| and |LocalVimRCDisable| to globally disable processing of local vimrc files.
  • add setting |g:localvimrc_event_pattern| to change the pattern for which the autocommand is executed.

v2.6.1 : 2018-02-20

  • fix a bug with missing uniq() in Vim version 7.4

v2.6.0 : 2018-01-22

  • add command |LocalVimRCEdit| to edit sourced local vimrc files for the current buffer.

v2.5.0 : 2017-03-08

  • add unit tests.
  • settings |g:localvimrc_whitelist| and |g:localvimrc_blacklist| now takes optionally a list of regular expressions.
  • add convenience variables |g:localvimrc_script_unresolved| and |g:localvimrc_script_dir_unresolved|.
  • add ability to view local vimrc before sourcing when |g:localvimrc_ask| is enabled.
  • emit autocommands before and after sourcing files.
  • add |g:localvimrc_file_directory_only| to limit sourcing to local vimrc files in the same directory as the edited file.
  • add |LocalVimRCFinish| function to stop loading of remaining local vimrc files from within a local vimrc file.

v2.4.0 : 2016-02-05

  • add setting |g:localvimrc_event| which defines the autocommand events that trigger local vimrc file loading.
  • don't lose persistence file on full partitions.
  • make it possible to supply a list of local vimrc filenames in |g:localvimrc_name|.
  • ask user when sourcing local vimrc fails and |g:localvimrc_sandbox| and |g:localvimrc_ask| is set whether the file should be sourced without sandbox.
  • fix a bug where local vimrc files are sourced in wrong order when some of them are symlinks to a different directory.

v2.3.0 : 2014-02-06

  • private persistence file to enable persistence over concurrent vim instances.
  • add convenience variables |g:localvimrc_sourced_once| and |g:localvimrc_sourced_once_for_file|.

v2.2.0 : 2013-11-09

  • remove redraw calls for better performance and fixing a bug on windows.
  • load local vimrc on event BufWinEnter to fix a bug with window local settings.
  • add |g:localvimrc_reverse| to change order of sourcing local vimrc files.
  • add convenience variables |g:localvimrc_file|, |g:localvimrc_file_dir|, |g:localvimrc_script| and |g:localvimrc_script_dir|.

v2.1.0 : 2012-09-25

  • add possibility to make decisions persistent.
  • use full file path when storing decisions.

v2.0.0 : 2012-04-05

  • added |g:localvimrc_whitelist| and |g:localvimrc_blacklist| settings.
  • ask only once per session and local vimrc before loading it, if it didn't change.

v2758 : 2009-05-11

  • source .lvimrc in a sandbox to better maintain security, configurable using |g:localvimrc_sandbox|.
  • ask user before sourcing any local vimrc file, configurable using |g:localvimrc_ask|.

v1870 : 2007-09-28

  • new configuration variable |g:localvimrc_name| to change filename.
  • new configuration variable |g:localvimrc_count| to limit number of loaded files.

v1613 : 2007-04-05

  • switched to arrays in vim 7.
  • escape file/path names correctly.

v1.2 : 2002-10-09

  • initial version

About

Search local vimrc files (".lvimrc") in the tree (root dir up to current dir) and load them.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp