2

I would like to set up something in a shell to give me an IDE-like experience using only the shell (no X). The closest I have come is using tmux, with vim in one pane and the command line in the other. That way from vim I can .w !bash to run a line.

But this is not satisfactory to me because the environment does not persist between commands run seperately.

If I run:

var1=FOO

and then

echo $var1

each using .w !bash, FOO will not be echoed.

What I really want to do is go through a text file in one pane, and use a shortcut to send a line to the terminal in the other pane, as part of that terminal session. What is the right combination of tools to achieve this? I understand emacs can be configured to do something like this with its own terminal emulator, but I would like a solution that works with the stock tools (such as on an installer).

Edit: tmux select-copy-paste isn't so bad but still a little awkward.

1 Answer 1

2

A prototype solution with sh, vim and tmux, quick and dirty proof of concept.

Your .w !bash runs bash and sends the current line to its stdin. Let's replace bash with a script that injects a line from its stdin into a different pane in tmux.

This is the basic script:

#!/bin/sh
pane="$(tmux list-panes -F '#{pane_active} #D' | awk '$1==0 {print $2;exit}')"
IFS= read -r line
tmux send-keys -l -t "$pane" "$line"
tmux send-keys -t "$pane" 'C-j'

The procedure, short explanation:

  1. Identify a pane that is not active.
  2. Read a line from stdin.
  3. Send the line to the pane as if it was typed.
  4. Send Enter to the pane.

Save it as trun, make it executable and reachable via $PATH. Now if you have two panes in tmux and run trun in one of them, and type a line and hit Enter, then the line will be injected into the other pane.

In vim instead of .w !bash invoke .w !trun, it should work. Bind this to a key for easy access.

Notes:

  • The script doesn't check if there are exactly two panes.
  • The script doesn't care what runs in the target pane; it just sends keys.
1
  • 1
    Yes, that works like a charm, thank you!
    – Stonecraft
    Jul 6, 2019 at 10:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .