Key bindings to send lines of test code with tmux+vim?

不打扰是莪最后的温柔 提交于 2021-02-11 14:26:44

问题


I am trying to create an ide-like setup with tmux + vim. I've seen a lot written about this on the internet, but I've been unable to figure out how to do one critical thing: execute test lines of code.

For example, you may create a tmux session with a vim pane for writing code and a python pane for executing tests, like so:

# Create a new Tmux session.
session="ide"
tmux start-server
tmux new-session -d -s $session -n ide

# Split the pane horizontally and launch vim.
tmux selectp -t 1
tmux splitw -v -p 50
tmux selectp -t 1
tmux send-keys "vim" C-m

# Enter insert mode and start coding...
tmux send-keys "i"
tmux send-keys "print('hello world')" C-m

# Select the second pane and launch python.
tmux selectp -t 2
tmux send-keys "python" C-m

# Attach to tmux session.
tmux attach -t ide

So, I am aware of how you might use tmux send-keys or tmux's command mode (<prefix> :) to send commands to another pane. However, I'd like to make a vim key binding that copies whatever line I'm on and sends this to the opposite pane in a couple of quick keystrokes.

If you have some magic sauce, I'd be grateful if you could share!


回答1:


I think https://github.com/jpalardy/vim-slime is what you are be looking for.

Once installed and configured (tmux is not the default used, screen is), you can execute the text under the cursor with <C-c><C-c>.

The following worked for me:

  • Install Vim-slime. I'm using Vundle, so I added the following to my .vimrc:
    Plugin 'jpalardy/vim-slime', and ran :PluginInstall in vim.

  • Configure slime for tmux by adding the following to your .vimrc:
    let g:slime_target = "tmux"

  • Try sending a line with vim-slime with the default key-binding (C-c C-c). Vim-Slime should prompt you for Tmux's socket and target pane. I found Tmux's socket with:
    echo $TMUX | cut -f1 -d','.

  • Alternatively, you can use :SlimeConfig.

  • Now it's working!




回答2:


It looks like vim-slime is the way to go.

But updating this in-case it is useful for others.

You could make a mapping as follows.
The change from -t + to -t .+ should cause tmux to paste to an alternate pane

vnoremap <leader>tp :call system('tmux send-keys -t .+ -l ' . shellescape(join([getline("."), "\n"])) )<CR>

Reference:
Send literal string from python-vim script to a tmux pane



来源:https://stackoverflow.com/questions/56031810/key-bindings-to-send-lines-of-test-code-with-tmuxvim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!