Left Right Left... Vim Shell Vim...

2012-03-06

I added a couple of configuration lines and can now switch between Vim and a child shell session with Alt+Left/Right. Like this:

+-----+                 +-------+
| Vim | -- Alt+Right -> | shell |
|     |                 |       |
|     | <- Alt+Left --- |       | 
+-----+                 +-------+

It feels like a faster way for switching between file editing and project tasks via shell and helped me reduce the number of tabs I keep in GNOME Terminal.

.vimrc changes:

map <A-Right> :sh<CR>
map <A-Left> :q<CR>

.inputrc changes:

$if term=xterm
    "\e[1;3D":  "\C-d"
$endif

Warning: this makes Alt+Left work like Ctrl+D (exit) in all of your shells.

The one drawback is that when I return to a tab and see a shell window I am not sure if the shell was opened from Vim, or Vim was closed. A visual clue would be nice, but I am not sure yet how to add it.

Back story:

I usually keep 2-3 terminal tabs open for each code repository I work with (Vim, shell, server processes, etc.). Sometimes I may have ~10 tabs in my GNOME Terminal and am quite used to quickly switching between them.

However last week I was constantly committing changes to two repositories simultaneously and I had to do this more than often. I noticed that I am spending too much brain cycles for tracking which tab I’m on at the moment and how to get to the tab that I need.

I was writing some make tasks to reduce the number of commands I have to use to deploy my code, restart servers, etc. I had an idea of how I could gather all my interactions with a repository under one terminal tab. The couple of key bindings did the trick.

Comments

MantasK

I'd say go with standard Unix foreground / background process control instead:
$ vim// do stuff in VIM
CTRL+Z ← suspend vim
// so stuff in shell
$ fg ← restore vim

@jmcclare

I'm with MantasK. I just move vim in and out of the foreground to do other things in the same shell. I also prefer tmux windows to Gnome Terminal tabs. Just as with vim, I prefer things to be hidden when I'm not using them.tmux also makes switching very fast.
'Ctrl-a, a' to see your list of terminals.
'Ctrl-a 3' to switch to terminal 3, etc.
If you want your prompt to tell you when you're inside a vim shell, add something to your $PS1 when the $VIM environment variable is set.
I have $PROMPT_COMMAND set to function that updates my $PS1 each time it is displayed. The whole thing is pretty large. It has colors and a lot of other indicators like this. Here's a short example for vim detection. In your ~/.bashrc, or a file sourced from it:
build_custom_prompt () {
$status="${USER}@${HOSTNAME} $(pwd)";

Add an indicator if we are inside a Vim shell
if [ ! -z "${VIM}"]; then $status="Vim "${status} fi
PS1="${status} \$ "
}
PROMPT_COMMAND=build_custom_prompt

I also make the Vim indicator bold green.