I use vim a lot and often find it useful to drop into the command line using !bash
.
However, I need to type exit
to return to vim and sometimes I'm not sure whether I'm in a subshell or whether that will close my session.
What I'd really like to do is type something like !bash -prompt "subshell"
so that I get something like this:
subshell$ <commands go here>
Is this possible?
Bash (also known as the “Bourne Again SHell”) is an implementation of Shell and allows you to efficiently perform many tasks. For example, you can use Bash to perform operations on multiple files quickly via the command line.
To access the shell, simply type 'bash' in the Windows command prompt, and everything is good to go.
CMD is the command line for Microsoft Windows operating system, with command-based features. Powershell is a task-based command-line interface, specifically designed for system admins and is based on the . Net Framework. Bash is a command-line and scripting language for most Unix/Linux-based operating systems.
Bash (AKA Bourne Again Shell) is a type of interpreter that processes shell commands. A shell interpreter takes commands in plain text format and calls Operating System services to do something. For example, ls command lists the files and folders in a directory. Bash is the improved version of Sh (Bourne Shell).
The most direct way to do this is to set the PS1
environment variable within vim:
:let $PS1="subshell$ "
And start your sub-shells using the command :shell
instead of :!bash
.
Using the $
sign with let
modifies an environment variable. Add this to your .vimrc to persist the setting.
Alternately, using :shell
you can specify a more specific command, including arguments, using the shell option, see help shell
and help 'shell'
.
So:
:set shell=bash\ --rcfile\ ~/.vimbashrc
In .vimbashrc add PS1="subshell "
, and invoke the sub-shells using :shell
instead of !bash
. Add this to your .vimrc to persist the setting.
So you have two options:
let $PS1="subshell "
to your .vimrc, and start sub-shells using :shell
instead of :!bash
.PS1="subshell "
to it, and modify the shell
option in your .vimrc: set shell=bash\ --rcfile\ ~/.vimbashrc
.Finally, if you must use :!bash
to start the sub-shells there are a couple of more options. Note that you can also pass a more specific command line using !
, e.g.:
:PS1="subshell$ " bash
should work.:!bash\ --rcfile\ ~/.vimbashrc
, and set PS1
in .vimbashrc as aboveBut you'll need to type these every time, or define a mapping for it.
Use shell variable $SHLVL
seems to be another option here. Add $SHLVL
in your $PS1
:
export PS1="$PS1 $SHLVL"
so your prompt looks like this:
[tim@RackAblade47 ~]$ 2
when you start shell from VIM, $SHLVL
will increase:
[tim@RackAblade47 ~]$ 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With