0

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?

raylight
  • 535
  • 1
  • 4
  • 12
  • 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 Jun 23 '21 at 07:51
  • :terminal % may be more convenient – D. Ben Knoble Jun 23 '21 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 Jun 23 '21 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 Jun 23 '21 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 Jun 23 '21 at 14:55
  • @D.BenKnoble My mistake, it's a bash script on the question but I wrote node.js on my comment. But the result of doing it from inside a bash file is the same. I've tried using :terminal bash % on the bash file from my question but I still received the error bash: file.bash: No such file or directory. – raylight Jun 23 '21 at 18:19
  • Well, then, does a file named file.bash exist in :pwd? – D. Ben Knoble Jun 23 '21 at 18:58
  • @D.BenKnoble Ah yeah, actually :terminal bash % is exactly what I need here... Sorry, I don't know what I did wrong when I tested it in the afternoon, but I did something wrong probably... I'm testing it now and it works fine. – raylight Jun 24 '21 at 02:17
  • @D.BenKnoble Well, the new discovered problem is that it limits the number of lines printed on the terminal to around 10 thousand lines... But perhaps that's a subject for a new question... – raylight Jun 24 '21 at 04:31

1 Answers1

2

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.

D. Ben Knoble
  • 26,070
  • 3
  • 29
  • 65