Emulating shell worksheets in Vim

Wednesday, 19 May 2004

More neat tricks with Vim, this time inspired by BBEdit’s Shell Worksheets. With these mappings, you can hit Ctrl-Enter on a line to execute it as a shell command and replace it with its output. Ctrl-Shift-Enter will preserve the command line above its output.

function! ShellExecReg()
  execute "r !" . getreg()
  norm o
endfunc
nnoremap <C-Enter>      ddk:call ShellExecReg()<C-M>
inoremap <C-Enter> <Esc>ddk:call ShellExecReg()<C-M>
nnoremap <CS-Enter>      yy:call ShellExecReg()<C-M>
inoremap <CS-Enter> <Esc>yy:call ShellExecReg()<C-M>

There seems to be a temporary file problem that makes it hang when netrw is in use, though. This will need further debugging.

And of course, in its current form, it clobbers the unnamed register, which would have to be rectified.