run :!command in a real terminal - vim

I can use pudb (an ncurses Python debugger) in vim, because, for instance, :!python % runs in an actual terminal window. I prefer to use gvim, but gvim runs :!python % in the vim "dumb terminal."
Is there a way to change this behavior so gvim opens a separate terminal for commands? I think I recall having it work this way in the past.

You can tell Vim to run a terminal, and run python in the terminal:
:!xterm -e 'python %; read'
read is there to let you see the output of your script before exiting the terminal.

Related

Why does Neovim not allow me to run an IPython embed when I run it in a shell?

In my init.vim for Neovim, I have the same line as in my .vimrc in Vim, which, when pressing F12, runs the file currently in the buffer using the python3 interpreter:
autocmd FileType python nnoremap <silent> <F12> :!clear;python3 %<CR>
Now I'm trying to run this tiny "test.py" script by pressing F12 in normal mode:
import IPython
IPython.embed()
Works fine in Vim:
But doesn't work in neovim despite exactly the same line in my ~/config/nvim/init.vim:
So it does run IPython, but then immediately (red arrow) inexplicably asks if I want to exit. It's also got a bunch of weird escape sequences inserted (yellow arrow) which I suspect are the reason why it wants to exit, and which don't appear with normal vim.
I don't really like the internal neovim terminal, so how can I get neovim to behave exactly like vim in this case?
This is a known limitation of NeoVim, :! is non-interactive and it will not allocate a pseudo-terminal which is typically required for full-screen applications such as IPython to run properly.
See issue #1496 for details.
An alternative is to use NeoVim's (or Vim 8's) support for terminal, with the :terminal command, or with a function such aa termopen() (in NeoVim) or term_start() (in Vim 8) to run full-screen applications such as IPython.
In your case, something as simple as :term python3 %, running the command in a terminal in a split, might be an adequate replacement.
You might also find the vim-bang-terminal plug-in interesting. It replaces a :! command with a similar command invocation that runs inside a Vim/NeoVim terminal instead.

Hide the output of shell commands in Vim

How can I hide the output of any command in the form :!command in Vim. I don't want to get any messages from Vim after running this command (like Press ENTER or type command to continue) also. I want shell commands to run in background completely.
"I want shell commands to run in background completely." --- This is not about Vim. It is about how to run commands in background.
To not print any output, you can run command like this: yourCmd >/dev/null 2>&1, if it doesn't work, use nohup (https://www.computerhope.com/unix/unohup.htm).
To not print Press ENTER or type command to continue, you can execute commands in Vim like this:
:silent !<command>
silent blank-issue:
https://vi.stackexchange.com/questions/2809/silent-makes-my-vim-go-blank
Vim grep causes characters to temporarily disappear
As explained here, you can also do:
:call system('command')

How to run scripts in background in Neovim?

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
NVIM
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
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.
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
You could use :silent !./vendor/bin/phpunit {file} to run the script background.

new line in vim doesn't display?

I invoke ghci in gvim by using :!ghci % to load my haskell file, However, the newline is displayed as ^J as the image below shows:
If I invoke ghci in vim instead of gvim, then everything is OK, so How could I get newline in gvim ?
For the graphical version of Vim, GVIM, a crude built-in terminal emulation is used for shell commands (cp. :help gui-shell). As the help page mentions, this has some limitations, and it is not meant to be used for interactive commands.
Switching to Vim in a terminal will run the shell command inside the terminal (with full capabilities); I guess that would be the best alternative if you cannot live with the discrepancies, and don't want to launch a separate terminal from gvim (i.e. :! xterm -e ghci %)

How to run Vim commands from Terminal

My questions is one that I haven't seen answered, the usual question is how to run certain commands to a file.
My question is how to run Vim commands or functions, from outside Vim (i.e. Terminal), these wouldn't affect any file, just Vim itself. Is this is even possible? If so, how?
If this is not possible like this, is there a way to go into Vim, run a command automatically, and then exit when that ends? Or run another command and then exit?
Thanks for your help!
Use
vim --cmd 'Command launched before vimrc' \
-c 'Command launched after vimrc' \
-c 'qa!' # Quit vim
. For running a command in an existing vim session you have to use +clientserver feature: run one vim with
vim --servername vim
and others with
vim --remote-send '<C-\><C-n>:Command<CR>'

Resources