0

I'm running vim for windows in a Powershell 7 terminal. I'd like to make my own command, called SB that would do the following:

  1. Save the current file
  2. Run the script build.ps1

So I added the following to my _vimrc file:

command! SB w|!powershell.exe -file .\build.ps1

This works OK, but when I run :SB the screen clears out and I get the following message:

Press ENTER or type command to continue

I have to press "Enter" to get back to Vim, which is inconvenient since I aim to use SB quite a lot.

I found a potential solution, namely inserting a carriage return after the shell command as follows:

command! SB w|!powershell.exe -file .\build.ps1|<CR>

But this doesn't work and I get an error:

) was unexpected at this time.

shell returned 1

Press ENTER or type command to continue

I feel like the carriage return could be a good solution to this problem. What am I doing wrong above?

user32882
  • 5,094
  • 5
  • 43
  • 82

2 Answers2

2

To generalize your own answer:

  • silent is indeed the command that suppresses the Press ENTER or type command to continue prompt.

    • However, the primary terminal screen showing the command's output is still switched to for the duration of the command execution, and I haven't found a way to suppress that (:help :redir mentions using silent with a function call, but at least with external commands that doesn't seem to help).
  • As :help :silent notes (emphasis added):

    • When using this for an external command, this may cause the screen to be messed up.
      Use |CTRL-L| to clean it up then.

    • The command equivalent of Ctrl-L is :redraw!

Therefore:

command! SB w | silent exe '!powershell.exe -file ./build.ps1' | redraw!

As for what you originally tried:

I found a potential solution, namely inserting a carriage return after the shell command as follows: command! SB w|!powershell.exe -file .\build.ps1|<CR>

Using <CR> in the way you intend only works in the context of a :map command, where you essentially define a string to send to VIM as if it had been typed interactively.

By contrast, in your case |<CR> was simply considered part of the shell command (|), which caused a syntax error.

Therefore, you could do something like the following with :map (using a simplified example) - but there's no good reason to do so, given that :silent is the right solution, as shown above:

map <F5> :!whoami<CR><CR>

Afterwards, if you press F5, whoami executes, and the second <CR> then auto-responds to the Press ENTER or type command to continue prompt and in effect returns to VIM's edit buffer.

mklement0
  • 382,024
  • 64
  • 607
  • 775
0

I got it to work like this:

command! SB w | silent exec "!powershell.exe -file .\\build.ps1"

It still exits out of the screen, but at least gets me back to it automatically without any fuss.

user32882
  • 5,094
  • 5
  • 43
  • 82