/sourcevimrc.vim

Automatically sources your .vimrc (Vim) or init.vim (NeoVim) files on write.

Primary LanguageVim ScriptApache License 2.0Apache-2.0

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 called SourceGrp.

augroup SourceGrp

The augroup command is used to create a new group of autocmds, called SourceGrp. 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 the SourceGrp 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. The let 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 the catch block.

set shortmess+=c

The set shortmess+=c command is used to add the c flag to the shortmess 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 the c flag from the shortmess 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 the source command. These error messages typically begin with Vim: Error, followed by a specific error message.

let s:source_output = []

The let s:source_output = [] command is used to reset the s: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 the efm option to the s:save_efm variable. This allows the script to restore the original value of the efm option after it has been temporarily changed.

set efm=%f

The set efm=%f command is used to temporarily set the efm 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. The readfile($MYVIMRC) command is used to read the contents of the vimrc or init.vim file into a list of lines. The call 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 the efm 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 message Error: is concatenated with the output of the substitute function.

The substitute function is used to extract the error message from the error message generated by the source command. The submatch(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 message Press 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 the input 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 the try block.

echom 'Warning: Reloaded ' . $MYVIMRC . '!!'

The echom command is used to display a message in the Vim command line. The message Warning: 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 the s:source_output variable is not empty. If it is not empty, the echom command is used to display the contents of the s:source_output list, separated by newlines ("n").

The | character is used to separate commands within the if statement.

augroup END

The augroup END command is used to end the SourceGrp 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.