How to run scripts in background in Neovim?

6.1k views Asked by At

I wish I could mimic original VIM behaviour by running scripts in the background and not in the internal Neovim terminal emulator.

Basically the reason is that I cannot seem to get colors to work properly, plus, I like to Ctrl+Z (put the editor in the background) and check what was the output of the last command.

Anyway I can configure nvim to do the same as vim in this regard?

Here is the comparison:

EDIT: My tests are run by using :! ./vendor/bin/phpunit {file}

VIM

enter image description here

NVIM

enter image description here

EDIT

By "Background" I mean, not async, in the background while Neovim is on the "top". I mean, to place the editor in the background (like when we do ctrl+z, and then run the tests "on top". Then I hit a key and Neovim comes back to the top.

In other words, I want to configure nvim in a way that when I run a test, it is the same as doing CTRL-Z; execute test.

Sorry, this may be super confusing :D

3

There are 3 answers

3
饶晓文 On

In newest version(HEAD version in github repo) of vim with terminal feature enabled.

You can run current file in background with following command:

:terminal ++hidden ./%

for neovim

You may need to install some plugin like:

https://github.com/tracyone/neomake-multiprocess

or

https://github.com/skywind3000/asyncrun.vim

If you are using tmux, you can

https://gist.github.com/tracyone/65cffd685fc9b9308e50c1a1783d1fb0

2
nelstrom On

Instead of using :!{cmd}, I'd encourage you to experiment with running the tests via the :te[rminal] {cmd} command:

:te ./vendor/bin/phpunit {file}

That way the output from phpunit will be captured in a terminal buffer. You can switch between the terminal buffer and the test file using <C-^> (or :b#). Or you can open the terminal buffer and the test file side by side in separate windows. When you're finished with the terminal buffer, you can delete it using :bwipeout {num}.

One cool feature of terminal buffers is that if you place your cursor on a filepath and press gf, Vim will open the specified file. Better still, you use gF, then Vim will open the file at the specified line/column number, if those are present.

For more info, look up :help terminal in Neovim.

1
Hongbo Liu On

You could use :silent !./vendor/bin/phpunit {file} to run the script background.