2

The other day I was using one of my non-interactive scripts to set up my dot files for my home directory, including my .vimrc file. This is what I had the script put into my .vimrc file:

. /etc/external/config.conf

This was before I knew vim can interpret 'source' but not '.' when sourcing external configuration. I thought my script worked well and then I got this error later when opening up vim:

search hit BOTTOM, continuing at TOPError detected while processing 
/home/demo/.vimrc:
line    1:
E486: Pattern not found: etc

It would have been really nice if I could have run some sort of sanity check on my .vimrc in my script so that I could have found out about my misconfigured settings sooner rather than later. How can I run a sanity check on my .vimrc configuration from the shell programmatically, without user interaction?

3
  • 1
    I think most people just try sourcing it (:so % if you're currently in the buffer or :so %MYVIMRC if you're not) or simply restart Vim.
    – Rich
    Apr 4, 2018 at 9:21
  • @Rich I should have been more clear with my question. I need to be able to run the sanity check from the shell. I have a script that needs to run non-interactively, and in that script I am setting up my dot files in my home directory. If I have to open vim to check my .vimrc syntax, then my script would require user interaction (which I do not want). Apr 4, 2018 at 16:01
  • Ohhh. I missed (or didn't grok) the words "in a script" in your original question. It makes a lot more sense now!
    – Rich
    Apr 4, 2018 at 16:23

2 Answers 2

3

It might be preferable to install an external tool, configurable depending on your needs: it may even have warnings you didn't know about. I recommend vint, which I use with ale for live linting.


But if you go down this path you need the command to fail (exit code != 0). You can achieve this with an intermediary file (instead of an inline command for readability), i.e. test.vim:

try
  source $HOME/.vimrc
  quit
catch
  cquit
endtry

The pattern (see :h :catch) should not only contain "Error detected while processing", or you would miss some messages.

Then you have to to verify if vim -u test.vim fails, for example in bash:

vim -N -u test.vim; [[ $? -ne 0 ]] && echo "Invalid .vimrc"

In my case I needed -N to avoid errors as I don't explicitly set nocompatible in my configuration.

Also, check this answer about silent mode and running Vim non-interactively.

3
vim -q fakefile >/dev/null 2>/tmp/checkforerrors; grep -q -F 'Error detected while processing ' /tmp/checkforerrors && echo '.vimrc syntax is bad' || echo '.vimrc syntax is good!!'; rm /tmp/checkforerrors

I would love to hear from some vi/vim experts about whether this command is safe and effective. Very ugly and hackish, but it seems to work well as a way to run a sanity check on my .vimrc file.

1
  • You can use the -q switch with grep instead of redirecting stdout and stderr to /dev/null. Ex: grep -qF 'Error detected while processing ' && ...
    – ZeroKnight
    Apr 6, 2018 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.