Vim: change prompt on shell escape

bash vi mode
bash vi mode indicator
zsh vi mode
inputrc
set -o vi
tcsh vi mode
bash vim
set -o vi

I often use :sh while editing a file in vim so that I can perform small tasks like git commits before getting back to editing the file. However, sometimes I get confused whether my shell was started by my terminal emulator or it was started as a vim subshell, so typing exit at the prompt always runs the risk of closing the terminal emulator by accident rather than going back to my vim editing session. Is there a way to have vim modify my prompt, perhaps by the $PS1 environment variable, when I start a shell from vim so that I know whether I'm in a subshell started by vim or not?

When you do :sh, a few additional Vim-specific shell variables are available to you. On this machine, I have:

$MYVIMRC
$VIM
$VIMRUNTIME

You can use $VIM, for example, in your *rc file like this:

if [ $VIM ]
then
  # set your vim-specific PS1 here
else
  # set your normal PS1 here
fi

Bonus: in GVim/MacVim the pseudo terminal you get when you do :sh is incapable of displaying colors. because Vim exports it as dumb, you can use the same logic as above to have a monochrome prompt when in GVim/MacVim and a color prompt in your shell:

if [ $TERM == 'dumb' ]
then
  # no colors
else
  # colors
fi

Vim: change prompt on shell escape - shell - php, Vim: change prompt on shell escape - shell. You need to create a rc file to set a different $PS1 and source it inside vim like this set shell=/bin/bash\ --rcfile\� Quit Vim if this is the last window. This fails when changes have been made in current buffer.:qa[ll]* Quit all windows and Vim, unless there are some buffers which have been changed. Prompt-quit (prompts if there are unsaved changes):conf[irm] q[uit]* Quit, but give prompt when there are some buffers which have been changed.

You need to create a rc file to set a different $PS1 and source it inside vim like this

set shell=/bin/bash\ --rcfile\ ~/.bashforvimrc

check here http://nothingtobedoneforall.wordpress.com/2007/02/25/setting-shell-prompt-for-vim/

Update from Neatu Qvidiu Gabriel's comments,

it's preferably to execute source ~/.bashrc before assigning to PS1. Because otherwise you lose all your predefined configuration in bashrc

Change cursor shape (or color) to indicate vi mode in bash, New user-settable variable, show-mode-in-prompt, adds a characters to the While it's not exactly what you want (dynamically changing the cursor), bash 4.4 I just press escape more often (e.g., esc to ensure I'm in command-mode; esc i to� The Quick Answer. If you’re in vi or vim and need to get out—with or without saving your changes—here’s how:. First, press the Esc key a few times. This will ensure vi is out of Insert mode and in Command mode.

I use these two functions, one for git (always on), one for vim (visible when in a shell spawned by :sh):

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
is_vim_sh() {
    if [ "x$VIM" != "x" ]; then
        echo " [vim]"
    fi
}
export PS1="\u@\h\[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\]\$(is_vim_sh)$ "

Can't exit Vim with :!q, [No write since last change] /bin/bash: q: command not found shell returned 127 Press ENTER or type command to continue. And if I click ENTER or anything� To change your Bash prompt, you just have to add, remove, or rearrange the special characters in the PS1 variable. But there are many more variables you can use than the default ones. Leave the text editor for now—in nano, press Ctrl+X to exit.

Speaking of GVim/MacVim, :h guioptions now support the following flag:

  '!'   External commands are executed in a terminal window.  Without
        this flag the MS-Windows GUI will open a console window to
        execute the command.  The Unix GUI will simulate a dumb
        terminal to list the command output.
        The terminal window will be positioned at the bottom, and grow
        upwards as needed.

Set :set go+=!, run :sh in GVim/MacVim, and be surprised :).

Using vi-mode in your shell, I'll introduce one for you here by showing the shell's vi-mode. up that typo, then you would hit Esc to change from INSERT to NORMAL mode. You can change the color of your shell prompt to impress your friend or to make your own life quite easy while working at command prompt. Putting it all together Let us say when you login as root/superuser, you want to get visual confirmation using red color prompt.

How to Change / Set up bash custom prompt (PS1) in Linux, Linux, macOS/Unix. You need to change and set up prompt using PS1 shell variable. The braces are required; \e : an ASCII escape character (033); \h : the hostname up to the first '.' \H : the vi /etc/bashrc or $ sudo gedit� One can save a file and quit vim / Vi by pressing Esc key, type :x and hit Enter key; Let us see all commands and examples in details. How to open a file in Vim / Vi. You can start vi/vim by typing the following command: vim file vi file vim [options] filename vim helloworld.sh. Understanding vim modes. When we start vim/vi text editor, we are

VIM Editor Commands, Vim is an editor to create or edit a text file. There are two Changing mode from one to another From insert mode to command mode type Esc (escape key). In Linux, much of your work occurs from a command prompt, also known as the shell, or BASH (Bourne-Again Shell). The shell interprets your commands and passes them to the operating system for execution. This tutorial will show you how to customize or change your Linux BASH prompt.

How to exit (quit) Linux vi editor with or without saving changes Step , How to exit (quit) Linux vi editor with or without saving changes Step-by-Step. tagged GURU-Tip From the command prompt: change to path where file is located with “cd”. vi filename. This will edit Press < Escape> . (You must be in insert� Display username only. To display the username only, just add the following line in ~/.bashrc file.. export PS1="\u "Here, \u is the escape sequence. Here are some more values to add to your PS1 variable to change the BASH prompt.

Comments
  • Use ctrl+z to suspend your VIM session instead of :sh?
  • When I run :sh from vim I already get a different prompt, although I'm not sure where it's defined. Do you have a custom prompt setup in your bash profile?
  • I use zsh, but yes, my prompt is heavily customized.
  • @mbratch I removed your addition of the zsh tag because this question is not zsh specific. In fact, it's not specific to any shell.
  • OK, I thought it might be since I use bash and it's behavior does not appear to be the same.
  • This works best for me. I have those same environment variables as well. Thanks very much!
  • I like this, because I find myself once in awhile shelling out from vim, forgetting I did so, and starting vi again. Changing prompts reminds me I'm in a vim shell.
  • Brilliant, thanks! This will get me started on the right path. I'll accept your answer when I have a moment to look into this and confirm that it works for me.
  • vim chokes on the --rcfile option. I'm using vim version 7.3 . Do you know where I can find documentation for the set shell command? EDIT: whoops, didn't see that it's a shell command flag rather than a vim set shell flag.
  • vimdoc.sourceforge.net/htmldoc/options.html near the 60 percept or you can search 'shell' 'sh'
  • Got it. See my edit to my comment above. I also managed to get the info I wanted from :help set ;)
  • The only thing missing from that blog post is that it's preferably to execute source ~/.bashrc before assigning to PS1. Because otherwise you lose all your predefined configuration in bashrc :)