Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to specify the bash prompt using a command-line option?

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?

like image 415
voltrevo Avatar asked Sep 05 '12 01:09

voltrevo


People also ask

Does command line use Bash?

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.

How do I Bash from command prompt?

To access the shell, simply type 'bash' in the Windows command prompt, and everything is good to go.

Is Bash and command prompt the same?

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.

What is Bash in command line?

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).


2 Answers

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:

  • Add let $PS1="subshell " to your .vimrc, and start sub-shells using :shell instead of :!bash.
  • Make a custom rc file for your vim sub-shells, add your specific 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 above

But you'll need to type these every time, or define a mapping for it.

like image 58
pb2q Avatar answered Sep 28 '22 14:09

pb2q


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
like image 21
procleaf Avatar answered Sep 28 '22 15:09

procleaf