The problem you're having is that when you run :make
and it fails, Vim tries to parse the output according to the 'errorformat'
setting.
The output for the script with the syntax error looks something like this:
test
/path/to/script.js:2
fklsjfj
^
ReferenceError: fklsjfj is not defined
at Object.<anonymous> (/path/to/script.js:2:1)
But the default 'errorformat'
setting (mostly aimed at C compilers such as gcc) parses that output and thinks the error happened in a file named "at Object.<anonymous> (/path/to/script.js
. It tries to open that file, which doesn't actually exist, so it puts you in a new Vim buffer with that filename, and that's why it looks like you "lost" the file you're editing.
You can see that you're in a new buffer using the :ls
command:
:ls
1 # "script.js" line 2
2 %a "at Object.<anonymous> (/path/to/script.js" line 1
The buffer with the %
flag is the current buffer, while the buffer with the #
flag is your original script.
You can go back to your script with :e#
, possibly followed by :bd#
to delete the bogus buffer (you don't really need to, but you might as well.)
A more proper and permanent solution to this problem would be to set 'errorformat'
appropriately, so that when you're using node %
as a 'makeprg'
to run your JavaScript code and there's an error, it will properly parse the output format of the output to correctly populate the quickfix list with the errors and move you to the actual file (and line) from where the first error happened.
You can find a Vim plug-in that sets that up in felixge/vim-nodejs-errorformat, which you can configure using vim-plug or native Vim packages or whatever other Vim plug-in manager you're using. Note that this plug-in will also already set up 'makeprg'
to run 'node %'
, so it takes care of that part as well.
Note that the script will set up this particular 'makeprg'
and 'errorformat'
for every JavaScript file you open in Vim (any file detected as filetype javascript
), which might not be the best approach overall... (Other plug-ins will typically configure :make
to run something like eslint
or use the npm package definition in a JavaScript/NodeJS project.) But perhaps this setup suits your use case well and might be what you're looking for.
make
to execute your script is an ideal approach. Can't you run it with:!
.... ?:! node %
solved the issue for me. :)<C-l>
or:redraw[!]
?:make
none of those commands are working too :/... The screen gets empty and I don't manage to go back to the file even if I try:ls
and:e myfile
... However,:! node %
solved this issue already, it just shows the execution output and goes back to my file regardless if there are errors or not.