VimShell

Written by: on February 5, 2013 Posted in:

If you are a Vim user and are using a shell outside Vim such as bash or zsh, you’ve been wasting your time(*1). Install VimShell and use it for everything. It works particularly well on your local computer.

There are three steps to using VimShell effectively. At first you will feel that VimShell isn’t as handy as shells outside Vim, but later you will not be able to remember the era without VimShell.

1. Bare VimShell as Shell and Interactive commands

Install VimShell and its depencency vimproc. You can place these files under &runtimepath manually, but personally I recommend you to use plugin managers such as neobundle.vim.

1
2
NeoBundle 'shougo/vimproc'
NeoBundle 'shougo/vimshell'

You have to build C files in vimproc if you install it manually. If you use neobundle.vim, it takes care of compiling, so you don’t have to manually run make.

After installing vimproc and vimshell, read vimshell’s docs quickly by :h vimshell, and try it.

You may feel the shell is weird and different compared to other shells at first.

vimshell1

This screencast is with the following config. The colorscheme is neverland

1
2
let g:vimshell_user_prompt = 'fnamemodify(getcwd(), ":~")'
let g:vimshell_prompt =  '$ '

2. VimShell with neocomplcache and unite

I’m pretty sure that most Vim programmers are using or at least have heard of neocomplcache, a Vim plugin for auto-completion. I don’t talk a lot about neocomplcache itself in this article, but if you haven’t tried neocomplcache yet, you should consider installing it, particularly for vimshell.

vimshell2

neocomplcache and vimshell work well together without any vimshell specific configuration; just install neocomplcache and set it up properly to enable its vimshell integration. Since neocomplcache is not just a completion plugin but an auto-complete plugin, it by nature shows completion candidates such as commands and filenames with no need to use TAB.

unite.vim is also a vital Vim plugin that provides an interface for anything. I may write another article about unite.vim in the future, but here in short I will just say that it is an alternative implementation to select an item from candidates.

VimShell supports unite by default for searching history like bash’s <C-r>. Type <C-l> in Insert mode in a VimShell buffer to open the unite interface of VimShell’s command history, choose a candidate, and type <Cr> to execute it.

vimshell3

You can just insert the selected candidates with TAB as well.

3. Integrate VimShell with anything you use

VimShell is not only a shell alternative but also provides an environment for interactive programs, such as irb or REPLs in general. You can make a new buffer for something with iexe {command} in a VimShell buffer. Note that you can still use neocomplcache and unite functionality in the buffer.

vimshell4

VimShell has the following commands for control of VimShell buffers (both shell and interactive environments) from outside VimShell.

  • :VimShellSendBuffer {bufname} to specify the destination VimShell buffer
  • :VimShellSendString {string} to send {string}
  • :[range]VimShellSendString to send strings that you chose

The details are in the vimshell docs. Read :h vimshell from beginning to end.

vimshell5

The strongest aspect of VimShell is that it’s easily controllable since it’s written in Vim script to run on Vim. You can use the above commands or other functions to control VimShell easily, or if it’s necessary, you can extend VimShell not only with C but also the friendly Vim script language.

Below is an example for Scala with SBT. Scala’s compiler and runtime system are really slow to start, so Scala programmers commonly run an interactive SBT console and keep using it, instead of making lots of new Scala processes every time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
command! -nargs=0 StartSBT execute 'VimShellInteractive sbt' | let t:sbt_bufname = bufname('%')

function! s:sbt_run()
  let cmds = get(t:, 'sbt_cmds', 'run')

  let sbt_bufname = get(t:, 'sbt_bufname')
  if sbt_bufname !=# ''
    call vimshell#interactive#set_send_buffer(sbt_bufname)
    call vimshell#interactive#send(cmds)
  else
    echoerr 'try StartSBT'
  endif
endfunction

function! s:vimrc_scala()
  nnoremap <buffer> <Space>m :<C-u>write<Cr>:call <SID>sbt_run()<Cr>
endfunction

augroup vimrc_scala
  autocmd!
  autocmd FileType scala call s:vimrc_scala()
  autocmd FileType scala nnoremap <buffer> <Space>st :<C-u>StartSBT
augroup END

vimshell6

The above Vim script code is in my vimrc. Feel free to use it.

The future of VimShell itself

VimShell doesn’t work well with ssh. There are some plugins like vimshell-ssh or unite-ssh to integrate more features that work remotely from your local Vim process, but they aren’t mature enough yet. It’s a known issue that we are working on.

Notes

  • No VimShell, No Life.
  • 50% of programmers on my team are using Vim with VimShell.
  • The animated gifs above were created with MacVim. I simply took many screenshots with Cmd-Shift-3, cropped, converted each to a gif, and converted to a single gif file using convert from ImageMagick.
  • (*1) or you’ve been saving your time; living without using some awesome Vim plugins doesn’t make you tempted to write lots of code in Vim script.