Reputation: 380
I am trying to build a jupyter like environment in vim where you can run cells of code.
For that i use eclim and want to embed the screen in a terminal. I am a beginner in vim scripting but managed to do it.
The minimal vimrc is
set encoding=utf8
"""" START Vundle Configuration
" Disable file type for vundle
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'
"
" IPython cell compilation
Plugin 'jpalardy/vim-slime'
Plugin 'hanschen/vim-ipython-cell'
if has('autocmd')
filetype plugin indent on
endif
"------------------------------------------------------------------------------
" slime configuration
"------------------------------------------------------------------------------
" always use screen
let g:slime_target = 'screen'
" The function in this script creates a list of [<buffer name>, <window number>] entries by applying map() to the range of window numbers in the current tabpage.
" winnr('$') returns the number of windows in the tabpage. Then it finds the entry with the given buffer name, and extracts the associated window number.
" Then it jumps to that window.
function! s:win_by_bufname(bufname)
let bufmap = map(range(1, winnr('$')), '[bufname(winbufnr(v:val)), v:val]')
let thewindow = filter(bufmap, 'v:val[0] =~ a:bufname')[0][1]
execute thewindow 'wincmd w'
endfunction
command! -nargs=* WinGo call s:win_by_bufname(<q-args>)
" open new window, deactivate conda (bug in conda), start a screen with embedded ipython
let g:ipython=0
function! QuitIpython()
!bash ~/vimQuitIpython
endfunction
nnoremap <expr> <F9> IPythonToggle() ? ':vertical terminal ++close bash<CR>conda deactivate<CR>screen -S vim-ipython-session -t vim-ipython-window<CR>ipython<CR><C-w><C-w>:wincmd g:winid<CR>':':call QuitIpython()<CR>'
function! IPythonToggle()
let g:winid=win_getid()
if g:ipython
let g:ipython=0
else
let g:ipython=1
endif
return g:ipython
endfunction
let g:slime_default_config = {"sessionname": "vim-ipython-session", "windowname": "vim-ipython-window"}
let g:slime_dont_ask_default = 1
"------------------------------------------------------------------------------
" ipython-cell configuration
"------------------------------------------------------------------------------
" Use '##' to define cells instead of using marks
let g:ipython_cell_delimit_cells_by = 'tags'
and a bash script with the below content is required to saved as ~/vimQuitIpython as well:
#!/bin/bash
screen_pid=$(screen -ls | awk '/\.vim-ipython-session\t/ {print strtonum($1)}' | head -n 1)
vim_rank=$(pstree -laps $screen_pid | grep -n -m 1 vim | sed 's/\([0-9]*\).*/\1/')
bash_rank=$(($vim_rank+1))
bash_pid=$(pstree -laps $screen_pid | sed "${bash_rank}q;d" | cut -d "," -f2)
kill -n 9 $bash_pid
screen -X -S vim-ipython-session quit
This works like so: When i hit the split opens and ipython is started (in a screen which is running in an embedded terminal). When i hit again bash is killed and the split screen closes.
Fine. I have two hiccups though:
Error when running "BufWinLeave auto command for "*"": E16:
Invalid field/area: call eclim#common#buffers#TabLastOpenIn()
Any tips on how to overcome these? Maybe there is also advice on how to make the script more elegant (e.g. without calling a separate bash script)
Upvotes: 2
Views: 182