Skip to content

Instantly share code, notes, and snippets.

@MikeDacre
Created April 13, 2015 19:56
Show Gist options
  • Save MikeDacre/cb62c63bc0b9a9f0a8cb to your computer and use it in GitHub Desktop.
Save MikeDacre/cb62c63bc0b9a9f0a8cb to your computer and use it in GitHub Desktop.
Vim-IPython IDE. Execute python and shell lines, cells (like matlab), selected text, and blocks in an interactive session using vim-screen.
"" This part runs commands in an interactive session, it requires
"" [vim-screen](https://github.com/ervandew/screen) to function
" Start Screen Session, Default to IPython in python files
fun StartScreenTmux()
if !g:ScreenShellActive
if &filetype == 'python'
:IPython
elseif &filetype == 'python3'
:IPython
else
:ScreenShell
endif
endif
endfun
" Send the current line to the shell
fun SendLine()
call StartScreenTmux()
let c = getline('.')
call g:ScreenShellSend(c)
endfun
" Send selected text, but remove starting indents
fun SendSelectionDedent()
call StartScreenTmux()
let c = substitute(@v, '\t', ' ', 'g')
let c = substitute(c, '^ \+', '', 'g')
let c = substitute(c, '\r \+', '\n', 'g')
let c = substitute(c, '\n \+', '\n', 'g')
call g:ScreenShellSend(c)
endfunction
" Send all text in the current cell - bounded by '##' and '##'
fun SendCellPython()
call StartScreenTmux()
:?##\|\#^?;/##\|\#$/y b
call g:ScreenShellSend(@b)
endfun
" Key bindings - space executes the current line or selected text
map <C-e> :call StartScreenTmux()<cr>
nmap <silent> <Space> :call SendLine()<cr>
vmap <silent> <Space> :ScreenSend<cr>
nmap <silent> <LocalLeader>sc :call SendCellPython()<cr>
vmap <silent> <LocalLeader>sd "vy :call SendSelectionDedent()<CR>
""" This part does not require vim-screen
""" Captures the output of a shell command to the current buffer
" Capture the output of the current line and
" print it into the current buffer
fun CaptureLine()
let l = getline('.')
let c = system( l )
set paste
set noexpandtab
exe "normal o".c
set nopaste
set expandtab
"redraw!
endfun
" The same thing, but capture start and end times, and put the whole
" output into a comment
fun ExecLine()
let l = getline('.')
set paste
set noexpandtab
exe "normal o###"
exe "normal o# "
exe "normal o# Run start: ".strftime("%y-%m-%d %H:%M:%S")
exe "normal o# "
exe "normal o###"
exe "normal o# Output:::"
exe "normal o# "
let c = system( l )
let c = substitute(c, '^', '# ', 'g')
let c = substitute(c, '\n', '\r# ', 'g')
exe "normal o".c
exe "normal o###"
exe "normal o# "
exe "normal o# Run end ".strftime("%y-%m-%d %H:%M:%S")
exe "normal o# "
exe "normal o###"
set nopaste
set expandtab
redraw!
endfun
" Key bindings
noremap <leader>il :call CaptureLine()<CR>
noremap <leader>el :call ExecLine()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment