sourcevimrc.vim is a lightweight and useful plugin for both Vim and NeoVim
that automatically sources your .vimrc (Vim) or init.vim (NeoVim) files on
write. This means that whenever you make changes to your Vim configuration
file, the plugin reloads it without the need to restart Vim or NeoVim, allowing
for a more streamlined workflow when tweaking your configuration.
- Faster configuration tweaking: When adjusting settings, key mappings, or plugins in your configuration file, you no longer need to restart your editor or manually source the file to see the effects of your changes.
- Streamlined plugin management: If you install, update, or remove a plugin, the SourceVimRC plugin will ensure that your configuration file is updated accordingly, without the need for manual intervention.
- Error reporting: The plugin is designed to catch errors when sourcing the configuration file, providing you with useful error messages to help you identify and fix issues quickly.
- No configuration required.
Add to $XDG_CONFIG_HOME/nvim/init.vim:
call plug#begin()
Plug 'ctrlcctrlv/sourcevimrc.vim', {'branch': 'master'}
call plug#end()This plugin is compatible with both Vim and NeoVim. It automatically detects
which editor you're using and sources the appropriate configuration file
($HOME/.vimrc for Vim or $XDG_CONFIG_HOME/nvim/init.vim for NeoVim).
The code's really short. Audit it then. Or ask the bot to.
This Neovim script is designed to automatically source either the
$HOME/.vimrcor$XDG_CONFIG_HOME/nvim/init.vimfile when it is written to. It does this by using an autocmd group calledSourceGrp.The
augroupcommand is used to create a new group of autocmds, calledSourceGrp. This group will contain all of the autocmds that are associated with automatically sourcing the vimrc or init.vim file.The
autocmd!command is used to clear any existing autocmds in theSourceGrpgroup. This ensures that no conflicting autocmds will be run when the script is executed.The
autocmd BufWritePostcommand is used to create an autocmd that will be triggered when the vimrc or init.vim file is written to. The$MYVIMRCvariable is used to specify the path to the vimrc or init.vim file. Thelet s:source_output = []command is used to create an empty list that will later be used to store any output from the sourcing process.The
|character is used to separate commands within the autocmd.The
trycommand is used to begin a block of code that will be executed. If an error occurs within this block, it will be caught and handled by thecatchblock.The
set shortmess+=ccommand is used to add thecflag to theshortmessoption. This flag tells Vim to suppress any error messages that may be generated during the sourcing process.The
source %command is used to source the current file (%), which in this case is the vimrc or init.vim file.The
set shortmess-=ccommand is used to remove thecflag from theshortmessoption. This restores the default behavior of displaying error messages.The
redrawcommand is used to refresh the Vim window after the sourcing process is complete.The
catchcommand is used to catch any errors that may occur during the sourcing process. The regex pattern/^Vim%((\a\+)\)\=:E\(.\+\):/is used to match error messages that are generated by thesourcecommand. These error messages typically begin withVim: Error, followed by a specific error message.The
let s:source_output = []command is used to reset thes:source_outputvariable to an empty list. This ensures that any previous output is cleared before new output is generated.The
let s:save_efm = &efmcommand is used to save the current value of theefmoption to thes:save_efmvariable. This allows the script to restore the original value of theefmoption after it has been temporarily changed.The
set efm=%fcommand is used to temporarily set theefmoption to%f. This tells Vim to use the filename as the error format for any error messages that are generated during the sourcing process.The
forloop is used to iterate over each line in the vimrc or init.vim file. Thereadfile($MYVIMRC)command is used to read the contents of the vimrc or init.vim file into a list of lines. Thecall execute(line)command is used to execute each line of the file.The
let &efm = s:save_efmcommand is used to restore the original value of theefmoption after it has been temporarily changed.The
echomcommand is used to display a message in the Vim command line. The messageError:is concatenated with the output of thesubstitutefunction.The
substitutefunction is used to extract the error message from the error message generated by thesourcecommand. Thesubmatch(1)function is used to extract the first submatch of the regex pattern, which is the error message itself. The^.*:s*pattern is used to match the beginning of the error message, up to the first colon and any whitespace characters that may follow it.The
''argument is used to replace the matched pattern with an empty string, effectively removing it from the error message.call input('')`
The
echomcommand is used to display a message in the Vim command line. The messagePress ENTER or type command to continue (SourceGrp)is displayed to prompt the user to continue the sourcing process.The
|character is used to separate commands within the autocmd.The
silent!command is used to suppress any error messages that may be generated by theinputcommand.The
call input('')command is used to wait for the user to press Enter or enter a command before continuing the sourcing process.The
endtrycommand is used to end thetryblock.The
echomcommand is used to display a message in the Vim command line. The messageWarning: Reloadedis concatenated with the path to the vimrc or init.vim file ($MYVIMRC) and the string!!.The
ifstatement is used to check if thes:source_outputvariable is not empty. If it is not empty, theechomcommand is used to display the contents of thes:source_outputlist, separated by newlines ("n").The
|character is used to separate commands within theifstatement.The
augroup ENDcommand is used to end theSourceGrpautocmd group.
Copyright 2023 Fredrick R. Brennan
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this software or any of the provided source code files except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
By contributing you release your contribution under the terms of the license.