sourcevimrc.vim
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.
Features
- 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.
Install
Add to $XDG_CONFIG_HOME/nvim/init.vim
:
call plug#begin()
Plug 'ctrlcctrlv/sourcevimrc.vim', {'branch': 'master'}
call plug#end()
Compatibility
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).
That sounds dangerous
The code's really short. Audit it then. Or ask the bot to.
GPT-4's audit
This Neovim script is designed to automatically source either the
$HOME/.vimrc
or$XDG_CONFIG_HOME/nvim/init.vim
file when it is written to. It does this by using an autocmd group calledSourceGrp
.
augroup SourceGrp
The
augroup
command 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.
autocmd!
The
autocmd!
command is used to clear any existing autocmds in theSourceGrp
group. This ensures that no conflicting autocmds will be run when the script is executed.
autocmd BufWritePost $MYVIMRC let s:source_output = [] |
The
autocmd BufWritePost
command is used to create an autocmd that will be triggered when the vimrc or init.vim file is written to. The$MYVIMRC
variable 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.
try
The
try
command 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 thecatch
block.
set shortmess+=c
The
set shortmess+=c
command is used to add thec
flag to theshortmess
option. This flag tells Vim to suppress any error messages that may be generated during the sourcing process.
source %
The
source %
command is used to source the current file (%
), which in this case is the vimrc or init.vim file.
set shortmess-=c
The
set shortmess-=c
command is used to remove thec
flag from theshortmess
option. This restores the default behavior of displaying error messages.
redraw
The
redraw
command is used to refresh the Vim window after the sourcing process is complete.
catch /^Vim%((\a\+)\)\=:E\(.\+\):/
The
catch
command 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 thesource
command. These error messages typically begin withVim: Error
, followed by a specific error message.
let s:source_output = []
The
let s:source_output = []
command is used to reset thes:source_output
variable to an empty list. This ensures that any previous output is cleared before new output is generated.
let s:save_efm = &efm
The
let s:save_efm = &efm
command is used to save the current value of theefm
option to thes:save_efm
variable. This allows the script to restore the original value of theefm
option after it has been temporarily changed.
set efm=%f
The
set efm=%f
command is used to temporarily set theefm
option to%f
. This tells Vim to use the filename as the error format for any error messages that are generated during the sourcing process.
for line in readfile($MYVIMRC) | call execute(line) | endfor
The
for
loop 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.
let &efm = s:save_efm
The
let &efm = s:save_efm
command is used to restore the original value of theefm
option after it has been temporarily changed.
echom 'Error: ' . substitute(submatch(1), '^.*:s*', '', '')
The
echom
command is used to display a message in the Vim command line. The messageError:
is concatenated with the output of thesubstitute
function.The
substitute
function is used to extract the error message from the error message generated by thesource
command. 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.`echom 'Press ENTER or type command to continue (SourceGrp)' | silent!
call input('')`
The
echom
command 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 theinput
command.The
call input('')
command is used to wait for the user to press Enter or enter a command before continuing the sourcing process.
endtry
The
endtry
command is used to end thetry
block.
echom 'Warning: Reloaded ' . $MYVIMRC . '!!'
The
echom
command is used to display a message in the Vim command line. The messageWarning: Reloaded
is concatenated with the path to the vimrc or init.vim file ($MYVIMRC
) and the string!!
.
if !empty(s:source_output) | echom join(s:source_output, "n") | endif
The
if
statement is used to check if thes:source_output
variable is not empty. If it is not empty, theechom
command is used to display the contents of thes:source_output
list, separated by newlines ("n"
).The
|
character is used to separate commands within theif
statement.
augroup END
The
augroup END
command is used to end theSourceGrp
autocmd group.
License
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.