Skip to content
Hans Chen edited this page Apr 22, 2021 · 4 revisions

Tips and snippets shared by users

This is a collection of tips and configuration snippets shared by users. Note that anyone can edit this page, so make sure you understand what the commands do before you add them to your .vimrc.

Open terminal with IPython

Vim 8.1 and later and Neovim support opening a terminal directly in the text editor. Below are some examples of how to open the terminal in a vertical split, launch IPython, and set it up for vim-slime and vim-ipython-cell. Add the code snippet to your .vimrc.

Neovim:

let g:slime_target = 'neovim'
let g:slime_dont_ask_default = 1

function! IPythonOpen()
    " open a new terminal in vertical split and run IPython
    vnew|call termopen('ipython --matplotlib')
    file ipython " name the new buffer

    " set slime target to new terminal
    if !exists('g:slime_default_config')
        let g:slime_default_config = {}
    end
    let g:slime_default_config['jobid'] = b:terminal_job_id

    wincmd p " switch to the previous buffer
endfunction

Vim:

" Not added yet - if anyone knows the equivalent for Vim, please edit this

To call the function, use :call IPythonOpen(), or bind it to a keyboard mapping.

Automatically reload modules in IPython

To automatically reload e.g. imported modules before executing scripts, add the following lines to ~/.ipython/profile_default/startup/00_autoreload.ipy (create the directory/file if it does not exist):

%load_ext autoreload
%autoreload 2

See IPython documentation on autoreload for more information.

Open IPython in the current working directory

These settings will make Vim set its global current directory to match the location of the current buffer, and map <Leader>st (<Leader> is \ by default) to launch IPython in the current working directory.

" Change automatically current directory to open file directory
autocmd BufEnter * silent! lcd %:p:h

" map to start ipython in current file directory
nnoremap <Leader>st :execute 'SlimeSend1 cd 'fnameescape(expand('%:p:h')):execute 'SlimeSend1 clear':SlimeSend1 ipython3