Go back to the main page

Increased Developer Productivity with Tmux, Part 5: Send-keys command

This article is over 2 years old. Proceed with caution.

Regards ♨ – Minimul



Leveraging the Tmux send-keys command

In Part 4 I covered Tmux starter templates. In this edition, I display the power of the send-keys command. In a nutshell the send-keys command enables you to send a command to another window and/or pane. When understood this command will sky-rocket your productively, principly by keeping important output easily visible while your focus "stays put" in your primary working window pane. I will start teaching the most common ways to use send-keys, which is within a single window split vertically, but will also instruct on how to send commmands to other windows.

Let's explore the wonderful send-keys command.

  1. To get started I launch Tmux with two windows, the first being vertically split in two.
  2. Typical window split into left and right panes.
    This section is at the 1:26 mark in the screencast.
  3. I then run the command grunt watch in the right pane while staying in the left pane.
  4. The -t switch is for "target".
  5. The previous step simply sends the command to the right pane but to send and run the command I need to also send C-m.
  6. C-m is a carriage return in Tmux.
  7. Next, I open the Gruntfile.js in Vim and make some edits to the watch section. From the editor I send a "close and re-run grunt watch" command to the right pane to test my changes while never leaving my editor.
  8. This is the great benefit of send-keys. I test my modifications while staying in my editor. Moreover, the command output is present to aid me in my next iteration.
    Testing grunt watch is at the 2:50 mark.
  9. How about using send-keys to beam commands to other windows?
  10. Modify the -t switch with the window index number.
    Target hit.
    Minimul says —

    I never use send-keys to send commands to other windows but thought I would demonstrate as there might be some edge cases where this is helpful. By far the most important use of send-keys is to send commands to other panes within the current working window. So much so that I created a bash function to assist me. Read on.

    My bash function for send-keys is at the 5:40 mark.
  11. Since send-keys is such an important part of my productivity arsenal I created a bash function to quickly call it.
  12.   function ts {
        tmux send-keys -t right "$args" C-m
    "ts" bash function usage. Note that single quotes within the command need to be escaped.
  13. Another common workflow of mine is to edit code in the left pane and to run specs in the right pane. I do this in combination with the bash function above and a Vim leader command.
  14. Explanation of my common send-keys use cases can be found at the 8:04 mark.
    Coding in the left pane and running specs in the right pane. Developer nirvana. I use a Vim leader command to optimize this use case.
        map rt :w:call RunCurrentTest('!ts be rspec')
        map rl :w:call RunCurrentLineInTest('!ts be rspec')
    To see and use the RunCurrentTest & RunCurrentLineInTest Vim functions see my .vimrc
  15. Another way I leverage send-keys is to run a git diff after starting to write (but then forgetting what key modifications are in this commit) my git commit message. I don't need to exit out of the commit message because I can send-keys over a git diff to the right pane.
  16. Summoning a git diff is one of the many great uses of the send-keys command.

That concludes installment 5. The Tmux send-keys command

I can't stress enough the importance of this commmand and I would consider it near top of the list in twisting Tmux to increase your productivity. Next in part 6 I teach on using the console-based browser, Elinks. While you are waiting for the article check out the screencast that is already baked.

Comment on this article?

If you like this article and screencast go to the dedicated page for this series at http://minimul.com/teaches/tmux.