Let's say I have a file with the following bash code:

echo "line 1"
echo "line 2"
echo "line 3"
echo "line 4"
echo "line 5"
echo "line 6"
echo "line 7"
echo "line 8"
echo "line 9"
echo "line 10"
echo "line 11"
echo "line 12"
echo "line 13"
echo "line 14"
echo "line 15"

Let's also say that I'm editing the line 11, if I use the command :!% bash I'll execute this file and its output will substitute the current file that I'm editing. So I can use Vim for navigating on its output and then I can press u for going back to what the code file was originally...

So far everything is fine, except for the fact that when I press u it sends the cursor back to line 1. I know I can set a marker on the line that I'm editing before executing it, something like ma and then 'a (backtick instead of single quotes) to go back to the line that I was... But that becomes troublesome doing it all the time...

I also know that I could execute the file with :set makeprg=bash\ % | make while seeing its output on the terminal, but in this way, I won't be able to navigate on the output with the Vim editor, and that's a functionality I'd like to keep... Is there any way of executing a file from inside Vim, see its output and edit it with Vim, while also being able to go back to the original code on the same line that I was?

  • The last line (as well as the subject/title) is missing something. Currently, the answer is "Yes. Just like you described." Obviously, you have something else in mind but it's not clear what. "...go back to the original code on the same line that I was using a maximum of three keystrokes"? Or is it two? One? Through mind control only? ;) What exactly is the criteria?
    – B Layer
    Commented Jun 23, 2021 at 7:51
  • :terminal % may be more convenient
    – D. Ben Knoble
    Commented Jun 23, 2021 at 12:16
  • @BLayer What bothers me the most is having to remember to create a marker with mx before executing the code all the time... I often forget to create the marker when I'm executing the code many times and then I can't go back to it with 'x. So my main criteria here is to go back to the liine that I was without having to remember to create this marker before executing the file. If there is a command that I can do it without having to create the marker that would be enough... But in this case, using '' doesn't work as well...
    – raylight
    Commented Jun 23, 2021 at 14:25
  • @D.BenKnoble When I type :terminal % my screen splits and I see the message executing job failed: No such file or directory, where my only choice seems to be going out of it with :q. I'm not sure if I'm missing something. Did you mean just typing :terminal % from inside the node.js file that I'm editing?
    – raylight
    Commented Jun 23, 2021 at 14:27
  • 1
    Thought it was a bash script? You can always be more specific: :terminal bash %, :terminal node %, etc. You might have to use %:p for a full path to execute directly.
    – D. Ben Knoble
    Commented Jun 23, 2021 at 14:55

1 Answer 1


One way to run the command and leave the file intact is :terminal, like in :terminal bash %. Vim will split a terminal running the program and leave you editing the file. It’s also asynchronous in the sense that you can edit while the program runs.

Your Answer

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

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