Setting up vim and tmux to edit/run Julia code
How to use this setup
Suppose you want to edit a file (a.jl say) in neovim and run Julia at the
same time.
-
Start a
tmuxsession (if not already started). -
Type
J a.jlto split the tmux window vertically, launching neovim editinga.jlin the top window and running Julia in the bottom window. -
In the neovim window, type
C-c C-cto send the code in present paragraph to Julia, or type<space>jto send just the current line. (By ‘paragraph’, I mean a block of code ended with a blank line or with the end of file.) -
Move from paragraph to paragraph, running each. Or, just don’t use paragraphs, in which case use either
C-c C-cor<space>j, as you wish.
Setup
This requires editing some dot files. I am not going to explain the details of how this works, and I cannot claim that this will work on all systems.
Step 1: edit ~/.zshrc (or similar) file
If you use the Z shell, edit your ~/.zshrc file, adding the following (see
e.g. References 1 and 2). (If you use another shell, edit the appropriate file
in a similar way.)
function J() {
tmux send-keys "vim $1" Enter \; \
split-window -v \; \
rename-window julia \; \
send-keys "julia" Enter \; \
select-pane -t 0 \;
}
Step 2: edit ~/.vimrc (or similar) file
Edit your ~/.vimrc file, adding the following to your Plug setup. I got much
of this from references 1 and 2. The remapping of <Space>j is something I made
up, corresponding to <Space>r in R mode.
" Slime
let g:slime_target = "tmux"
let g:slime_default_config = {"socket_name": "default", "target_pane": "{last}"}
let g:slime_dont_ask_default = 1
" Julia: make <Space>j run current line. Note that <C-c><C-c> runs paragraph.
autocmd Filetype julia xnoremap <Space>j :SlimeSendCurrentLine<cr>
autocmd Filetype julia nnoremap <Space>j :SlimeSendCurrentLine<cr>