If you need this, you might as well refer to the code. The code is not as complicated as you think. 😄
If you don't want the default key mappings, set:
let g:slime_no_mappings = 1
before the plugin is loaded.
If you are using lazy.nvim as package manager, this can also be done within the init
function:
{
"jpalardy/vim-slime",
init = function()
vim.g.slime_no_mappings = 1
end
}
The default mappings are:
xmap <c-c><c-c> <Plug>SlimeRegionSend
nmap <c-c><c-c> <Plug>SlimeParagraphSend
nmap <c-c>v <Plug>SlimeConfig
Example of how to set vim-style mappings:
"disables default bindings
let g:slime_no_mappings = 1
"send visual selection
xmap <leader>s <Plug>SlimeRegionSend
"send based on motion or text object
nmap <leader>s <Plug>SlimeMotionSend
"send line
nmap <leader>ss <Plug>SlimeLineSend
Of course these mappings are just examples; you can set them according to your preference.
If you want vim-slime
to prefill the prompt answers, you can set a default configuration:
" screen:
let g:slime_default_config = {"sessionname": "xxx", "windowname": "0"}
" tmux:
let g:slime_default_config = {"socket_name": "default", "target_pane": "1"}
If you want vim-slime
to bypass the prompt and use the specified default configuration options, set the g:slime_dont_ask_default
option:
let g:slime_dont_ask_default = 1
By default, vim-slime
will try to restore your cursor position after it runs. If you don't want that behavior, unset the g:slime_preserve_curpos
option:
let g:slime_preserve_curpos = 0
If you want to send blocks of code between two delimiters, emulating the cell-like mode of REPL environments like ipython, matlab, etc., you can set the cell delimiter to any buffer-local variable b:slime_cell_delimiter
or global g:slime_cell_delimiter
variable and use the <Plug>SlimeSendCell
mapping to send the block of code. For example, if you are using ipython you could use the following:
let g:slime_cell_delimiter = "#%%"
nmap <leader>sc <Plug>SlimeSendCell
b:slime_cell_delimiter
and set the variable in ftplugin
for each relevant filetype.
If you need more advanced cell features, such as syntax highlighting or cell navigation, you might want to have a look at vim-slime-cells.
At the end of the day, you might find that vim-slime
ALMOST does everything
you need, but not quite the way you like it. You might be tempted to fork it,
but the idea of writing and maintaining vimscript is daunting (trust me: I sympathize 😐).
You can override some logic and still benefit from the rest of vim-slime
.
Here's the mental model you need to understand how things work:
- you invoke a key binding and
vim-slime
grabs a chunk of text - depending on which language you are using (see below), the text might be "transformed" and "massaged" to paste correctly
- if the config is missing, the user is prompted to fill in the blanks
- a target-specific function is called to delegate the "send this text to the right target" part
- the target receives the right text, the right way, and everything works
There is some good news, for step 2, 3, 4, you can override the logic with your
own functions! Put these functions in your .vimrc
and hijack the part you
need.
You can override any or all (zero to many) of these functions, as needed.
Why is this awesome?
- skip vimscript: delegate to an external script; written in your own preferred language
- optimize for you: treat yourself with just-for-you customizations and hardcoded values
- ultimate power: beyond config and flags, passing a function means you can do anything you want
You might still need some vimscript to glue things together. Leaning on the
vim-slime
code for examples might get you 90% of what you need. If not, there's
always Learn Vimscript the Hard Way.
If you feel others can benefit from your customizations, open a PR and we'll find a way.
Write a function named SlimeOverride_EscapeText_#{language}
:
function SlimeOverride_EscapeText_python(text)
return system("some-command-line-script", a:text)
endfunction
This example code, for Python in this case, pushes the selected text to some-command-line-script
through STDIN and returns whatever that script produced through STDOUT.
Contract:
- input is selected text
- output is string or an array of strings (see other
ftplugin
for details)
Write a function named SlimeOverrideConfig
:
function SlimeOverrideConfig()
let b:slime_config = {}
let b:slime_config["key"] = input("key: ", "default value")
endfunction
Contract:
- no input, but...
b:slime_config
might containg:slime_default_config
if it was defined, or be undefined otherwise- no output but...
b:slime_config
expected to contain necessary keys and values used by the target send function (up next)
Write a function named SlimeOverrideSend
:
function SlimeOverrideSend(config, text)
echom a:config
call system("send-to-target --key " . a:config["key"], a:text)
endfunction
Contract:
- inputs are config (from config function, above or default) and selected text (post transformation)
- no output but...
- expected to do whatever is needed to send to target, probably a call to
system
but see code for details