What is the best approach of using shell commands from vim? I know about the possibility of !shell_command. But this doesn't know all commands e.g.
export OSTYPE; make install So I have to run it outside vim. Is there better approach?
I know this is a bit late, but my preferred approach is suspending the vim process (Ctrl+z). You return to your shell/bash command prompt.
Then execute whatever command(s) you like.
Return to vim by typing fg
You can start a shell from Vim using the :sh command. When the shell exits
(after the exit command or Ctrl+D) you return to Vim. The name for the shell command comes from the shell option.
For terminal Vim (on unix-like systems) you can also use Ctrl+Z to suspend Vim and get back to the shell from which it was run. To resume the Vim process, use the fg command.
Related
I am running gvim on Windows 7.
I use this mapping to execute the current file with powershell:
nnoremap <C-q> :! & '%:p'<cr>
It works great except I can't access vim until I close the powershell window. Sometimes I want the shell to remain open so I can run additional commands or I want to access vim with the shell still open so I can check the lines where errors were generated.
Ideally (don't know if this is possible) I want to have an already open shell execute the command. So I always have vim and a shell open (on separate monitors) and I can execute the script in that same shell.
How can I achieve this?
GVIM on Windows has a special :!start command to execute the external command asynchronously; i.e. Vim doesn't wait for its return. Just replace the :! with it. See :help :!start for more information.
On Unix, such special isn't necessary; you can just append & (a shell feature) to execute the command asynchronously.
I have observed in zsh if I run a command like,
some_command &
and close the terminal, it would kill the process started by some_command.
While it doesn't happen with bash.
So, I'm curious if the behaviour of putting "&" after command is shell specific?
Yes, the exact specifics on background vs detach from controlling terminal vs process group, etc are shell-specific. You should look up the nohup command for some additional detail.
This occurs when I set the vim's shell to be interactive:
set shellcmdflag=-ic
or
set shell=/bin/bash\ -i
I like these because they give syntax highlighting to the output (eg.: !ls)
But the cost is that I have to type fg # every time.
Is this a default behavior?
How can I get interactive shell in vim without having to make it run foreground?
You can't. This behavior is perfectly normal and expected and in line with Vim's author's philosophy. It's very unlikely to change in the future.
If you want a shell inside Vim, you'll have to install a plugin like Conque or Vimshell.
I usually just use tmux instead. You can split the terminal and have a normal interactive shell and an instance of vim running side by side - very handy.
I'm using W32 GNU Emacs 24.1.50.1 with Cygwin bash 3.2.51. I know that this combination is very troublesome (see EmacsWiki and Cygwin list). However the problem I want to solve now is not confined to Cygwin. I got the issue with MinGW as well. So, the problem is:
In Emacs start a shell via M-x shell provided you have set up the variables shell-file-name and explicit-bash-args appropriately (see e.g. here). Then issue a command which expects stdin input, e.g. cat. To cancel out of cat you can usually type Ctrl-D once and you return to the shell prompt. In W32 emacs when you do this (or even when you issue explicitly M-x comint-send-eof) the cat command gets exited and the bash shell quits as well.
I tried to increases bash's "resilience' by setting the environment variable IGNOREEOF to, say, 5, but then a single Ctrl-D results in the following output
$ Use "exit" to leave the shell.
$ Use "exit" to leave the shell.
$ Use "exit" to leave the shell.
$ Use "exit" to leave the shell.
$ Use "exit" to leave the shell.
$ exit
Process shell finished
and, of course, the bash has exited again. This problem only happens when the shell runs in W32 emacs - Cygwin bash through the Cygwin tty window and MinGW bash through its own MinTTY work fine.
Why is this happening in W32 Emacs and how can I just exit the running subcommand (e.g. cat) when using W32 Emacs without exiting the bash shell?
The notion of tty like you expect it to behave doesn't really exist in the W32 worl. If you want to see this Unixy behavior, you need not only to run a Cygwin program but that Cygwin program needs to talk to another Cygwin program (Emacs, terminal, you name it). IOW you'll only get that with the Cygwin version of Emacs.
The GNU Emacs FAQ for MS Windows notes that this issue exists for any sub-process in the shell buffer. So it affects the DOS command line as well (but of course you don't usually use Ctrl-D in DOS, so it's not so noticeable). No workaround is suggested in the FAQ, so I'm guessing that there's no easy fix.
Usually you can use Ctrl-Z as well as Ctrl-D. Take a look at this note:
http://ignaciopp.wordpress.com/2009/07/02/emacs-shell-workaround-for-killing-input-in-windows-version/
Ok, I hope this question makes some sense but what is the difference between a bash shell and a bash terminal? Example. When I first open up terminal I have a prompt with my current dir and my user name. In the terminal window header it says -bash- , when I type echo $SHELL I get bash. So by default the shell is bash. Where my confusion comes into play is when I type bash. My prompt then goes to bash-3.2$.Obviously it's a different mode. Are ther any major differences? I have read the man page with no answer. I understand what a bash shell is but just do not get the difference. Thanks for the clarity in advance.
There is no difference, they are both instances of the bash shell.
The reason you are seeing a different prompt is that your initial login shell sources ~/.bash_profile where presumably you have your prompt set. When you type bash it invokes another shell but because this one isn't a login shell, it doesn't source ~/.bash_profile and so you get the default prompt.
If you were call bash -l, (which invokes bash as if it were a login shell) I bet you would see that your original prompt remains