332 points · by ianthehenry · 5y ago · 193 comments
I'm a huge fan of a command window, where you type words to do a fuzzy search of all the commands in a program.
Sublime text and jetbrains products have it down so well to the point of me not needing to memorize key shortcuts. I just have to remember the first few letters and I'm good to go.
That said, I agree with your larger point completely -- I love Sublime's command palette, and I love using ivy/counsel for M-x in emacs. It would be even better if tmux had something like that.
That said, so looking forward to copying text in tmux going forward. The instructions in the OP didn't work for me, but I found two other posts - one from 2013, one from 2016, the combination of which worked. Not only can I copy text to a tmux buffer now, but it automatically goes to the system clipboard. This feels like overcoming a major disability.
:w !tmux load-buffer -
Writes whatever you have selected in vim into the tmux buffer through standard in.Elsewhere you can paste like pbpaste.
:r !tmux save-buffer -
The save-buffer command is’t the most intuitive name, but it’s saving to a file, stdout in this case.For example, from Windows these days I'm having to vim yank to file ~/clipboard and then I have a Windows keyboard shortcut to pscp that file locally && notepad file. Hacky, yes, but the muscle memory is there now so meh.
But that's just the flavor of the month this month...
Just because an app has some features doesn't mean that everyone knows about them. In fact the opposite is true, most features are never discovered and even fewer are used, even "obvious" ones.
Right, but you dont run things _inside_ sublime text. Keep in mind tmux has to play well with hundreds of existing shortcuts that people expect to work.
When you are attached to a tmux, you don't want to override the shortcuts of something you may run inside the tmux.
All the terminal shortcuts are expected to work (Ctrl-E, Ctrl-Arrow, Ctrl-A, Ctrl-E, Ctrl-E, Ctrl-Q, etc..). All main program shortcuts are expected to work (vim, emacs, etc) And of course not desirable to override screen shortcuts.
ctrl-b ? is helpful if you can’t remember the hot keys.
That reconfiguration occurred very soon after I was introduced to screen, circa. 1993 or thereabouts.
I've used Unix in the 1980s the first time and learned that whatever your preferred editor is, you need to know vi, because vi is always installed. That has been true in my experience with a single exception. In a recent embedded Linux I used they had only nano available.
So what is the story behind JOE and what is good about it?
GUIs for decades have demonstrated practical ways of making commands discoverable and also teaching users the keyboard shortcuts. All without having to ask people to RTFM. It is a pity that so many TUI apps haven't learnt the same lessons.
There is a reason why Nano became such a popular default editor for email. The shortcuts are printed at the bottom of the screen(!)
I hope more TUI apps pick up on the idea of the command palette though. It is a fast and keyboard friendly way of improving usability.
The truth is that being pretty/fashionable matter more than discoverability..
Ubuntu has some wrapper for tmux called byobu that does exactly this. It is the only time I really enjoy using this sort of tool.
Well, tmux is not an editor but a terminal multiplexer — its keyboard shortcuts are like this as to make sure they don’t interfere with any editor’s shortcuts running in the terminal.
It's hotkeys are... less arcane to remember by virtue of being primarily the F-keys at the top of the keyboard. https://manpages.ubuntu.com/manpages/eoan/en/man1/byobu.1.ht...
That is to say, it's dead simple once it's explained to you. The issue is with documentation, not the keyboard shortcuts themselves.
Sublime Text and Jetbrains are also vi-inspired. For where Jetbrains doesn't go far enough there is a vim mode plugin. I would say these are strong indicators of vi's dominance among keyboard shortcut layouts.
You want Vim-like controls? Use dvtm. It lets you open the content of any terminal in Vim, and from that point you can do anything from selecting/copying text up to saving a snippet in a file with a simple ":w foo".
All that aside, clipboard copy-pasting in Vim is absolutely horrible out of the box.
> Jetbrains are also vi-inspired
Not sure about that. The tab management and window splitting alone feel near unusable to me, because like many other modern apps they are convinced I only want to switch tabs in a seemingly random order(instead of, you know, the order that's literally shown on screen), and none actually have Vim's tabbing where a tab can contain multiple buffers, not the other way around. And guess what, no Vim plugin I know ever touched tabs. Probably because people not using Vim think the UX is all just in some arcane keybindings.
I'm more irritated though that Vim has nice keybindings for any sort of changes you might want to make to your tab and split setup. I don't think any of the GUI tools support keyboard commands for any of that. Or at least if they do, I haven't been able to figure out what they are, and am probably honestly too lazy to try to develop muscle memory for how they work.
If you mean the behavior of Ctrl-Tab, yes, that's confusing for me too. But you can switch using the order shown on screen though, by using Alt-Left / Alt-Right.
But unfortunately it doesn't look at $VISUAL, so... yeah, you usually have to set it explicitly.
It took quite a few years fir this to be picked up by SublimeText, but wow that was another huge boost. No longer needing to learn tons of keyboard commands to navigate an application.
I wish an OS would deeply bake this idea in. Apple has done good job with Spotlight for the system level (I still use LaunchBar on my own systems) , but I want this functionality to be shared between System and Apps
On Linux, I mostly restrict myself to the CLI. When I try Linux GUIs, I am frustrated with the lack of keyboard consistency in the GUI, but I have also never given it the months of time it would take to bend the Linux GUI to my way. (I do customize the heck out of the CLI)
I never tried it, seems like such a wasted opportunity.
Not to mention they all seem to come from an "old unix" perspective, with weird combinations and few use of bound combinations
Hence why I prefer keeping my terminal tabs not on tmux but on iTerm (or your favourite terminal emulator - except for the Gnome one)
For a start tmux does have similar interfaces, ability to search all panes etc.
Also comparing tmux to an ide is also possible wrong.
This copy/paste workflow is really nice for working with kubernetes on the command line.
E.g., imagine that you need to delete a kubernetes pod.
You'd run a few commands like this:
1. `kubectl get pods`
2. Find the pod id, and copy it using mouse & some shortcut
3. `kubectl delete pod <pasted-pod-id>`
Instead of this mouse workflow, you could do something like this in tmux:
1. `kubectl get pods`
2. Find pod id, `Ctrl+b [` to enter tmux's vi selection mode
3. Navigate to the section you'd like to copy; e.g. `k6ww` (up six lines and over two words, for example)
4. Press space bar to start selection, and use common vim movements to highlight what you'd like to copy (e.g. w or $)
5. Press return/enter to copy the selection
6. This text can be pasted with either Cmd/Ctrl+v (it gets copied to the system clipboard for me on macOS 11.2.1 with tmux 3.1c_1 from brew), or using the default tmux shortcut `Ctrl+b ]`. So that would allow you to type `kubectl delete pod Ctrl+b ]`.
So yeah, it's a few more steps, but it's a lot faster for me.
These are the relevant sections of my ~/.tmux.conf file:
# Use vim keybindings in copy mode
setw -g mode-keys vi
# Use emacs keybindings in the status bar
set -g status-keys emacs
(I mention the `set -g status-keys emacs` config line just as an FYI since it's been really helpful for me with tmux. I've never liked using readline-type inputs with vi-mode.)[1]: https://k9scli.io/
kubectl delete pod <tab>
and it lists the pods.For bash or zsh:
source <(kubectl completion $(basename $SHELL))Where I really like this tmux copy/paste workflow is for grabbing multiple lines of command output. From there I can paste that into some message on slack, or use it as the body of a PR message that describes some error or shows some output.
For the latter case, I have a zsh function that calls `hub` to open a PR, drops me off into vim, and once I `:wq`, it will send the API request to open the PR, and then open my browser up to it. This is the command I have in the function: `hub pull-request --push --file "$filename" --browse --draft`.
I have basically the same setup as you, and I think that's the only thing I've manually configured. I also run the oh-my-zsh tmux plugin, and iTerm2's tmux integration, which seemed to work out of the box for me.
Listed the way the parent does it seems very complicated, but if you have the sequence memorized and trained it can be executed very quickly and without moving your hands from the keyboard.
With tmux you end up with four distinct copy/paste buffers: the OS, tmux, the shell, vi/emacs. You do get used to the mental gymnastic of juggling all those but it's not ideal.
Same goes with managing the windows of the OS, tmux and vi/emacs.
The cognitive load you are concerned about can be configured away, and for me is a solved problem.
It's the best option I've found, but it's not all smooth sailing for me at least.
tmux will copy to your system clipboard by default if your terminal emulator supports the right escape sequences. If it doesn't, then you do need to configure it to shell out to xclip or pbcopy or whatever. It's annoying, but I find it less annoying to configure it than to try to keep multiple copy buffers straight.
(Or `:set clipboard=unnamed`)
Thus to copy I use the y (yield) key, with optional selection keys like iw (in word), t<char> (to <char> included), f<char>( to <char> not included), etc.
But my mouse selection copy works differently between vim and tmux. In vim, when I select text with the mouse I have to press y to copy it. In tmux it is copied without pressing any key. So there is improvement here because often the paste fails after selecting text in vim because I forgot to press the y key.
tmux is what made drop tiling window managers after I realized I was only using tiling in a terminal. (I use Gnome on Linux.)
There are also tmux plugins to make some operations smoother.
https://github.com/fcsonline/tmux-thumbs
Like keyboard driven browsers uses hints, so file paths, git SHAs etc. are highlighted using a small hint and if you press it it is copied.
https://github.com/laktak/extrakto
Fuzzy search in current pane to insert/copy things of interest.
0: https://github.com/alacritty/alacritty/blob/master/docs/feat...
I tried playing with it but it doesn't seem to recognize my control key (???) which makes a lot of things... hard. A quick google and glance through the settings didn't reveal any likely culprits, but I expect it's some macOS thing. Anyone know what's up with that?
Or maybe it's something with .alacritty.yml or keybinding?
https://github.com/fcsonline/tmux-thumbs
asciinema demo here:
Do you have any particular issue? Does it give you a real tty?
If I need to copy, I'll use the terminal normal mode (ctrl-w N) and visually highlight text and copy it to the + register. Then I can paste the contents in another application. The only issue I've encountered is that some text that's not supposed to be wrapped has line breaks added at the screen width. In that case, I'll paste the text into another vim buffer first and remove the line breaks using gJ. then copy and paste it into another application.
For copying text from another application back into vim, I'll just highlight it with the mouse and paste it into vim using ctrl-w "*
# copy on select
bind-key -T copy-mode-vi MouseDragEnd1Pane send -X copy-pipe-no-clear 'xclip -i -selection primary'
# paste on middle click
bind-key -n MouseDown2Pane { select-pane -t=; run 'tmux set-buffer "$(xclip -o -selection primary)"'; paste-buffer }
(s/xclip/xsel, depending on your system.)Your solution is almost perfect. The "paste" code works as expected. But the "copy" code, apart from copying the selection into the clipboard, leaves the tmux pane in an odd state (copy mode) from which you have to exit manually. Would it be possible to bind the command to something that quits copy mode afterwards?
Fortunately, thanks to ianthehenry's solution on this thread, you can do the same thing without pressing shift!
Or if you are copying it out of iTerm, replace "slightly" with "to the other terminal" :-)
- https://github.com/tmux-plugins/tmux-yank
If your terminal emulator supports the clipboard escape codes, then yes -- it just works, and there's nothing for you to do to set it up. This is because when you hit "copy" tmux just prints a bunch of escape codes, and ssh will re-print the escape codes locally, and your terminal emulator will see them, and know to set the contents of the system clipboard.
If your terminal emulator doesn't support the clipboard escape codes, then you have to configure an explicit copy-pipe command, like this:
bind -T copy-mode-vi y send -X copy-pipe 'xclip -i -selection clipboard'
And that will work on a remote tmux, as long as you ssh to the remote server with X forwarding (ssh -X or -Y).If you're not using X and your terminal emulator doesn't support the escape codes, then I think the answer is no. Like if you're using a Mac and using Terminal.app instead of iTerm, I'm not sure how you would make this work.
- I want it yanked into both the selection AND the default clipboard
- I want tmux to exit copy mode after yanking
bind -T copy-mode-vi y send -X copy-pipe-and-cancel 'xclip -in -selection default; xclip -o | xclip -in -selection clipboard'As long as your system & version of tmux supports copying to the local clipboard, you should have no problems pasting with either Ctrl/Cmd+v or `Ctrl+b ]`.
Otherwise, if the clipboard is broken for some reason, you can stick to using `Ctrl+b ]` to paste.
1. Enter copy mode with cmd+shift+c.
2. Basic Vim keybinding, many keystrokes can active different actions.
- v to select by character.
- shift+v to select by line.
- ctrl+v for rectangular selection.
- ctrl+space to stop selecting.
- y to yank/copy the selection (also exits copy mode).
- q to exit copy mode.
[0] - https://iterm2.com/documentation-copymode.htmlFor people unfamiliar with tmux these are some other features I use -
C+a . space -> switches layout from vertical to horizontal and back
C+a . , -> lets me rename the window so I know what I have open there
C+a . [ . C+s -> search in previous output. n lets me me then jump to next occurrence of what I am searching.That aside, I do really enjoy tmux and appreciate the hard work of the devs!
tmux capture-pane -p -S - | vim -
Or bind that to a key. -p is "print to stdout", -S specifies where to start and - means "the beginning of history" (by default capture-pane just gives you the visible contents). I'm not sure how to do the same thing with the current selection -- copy-pipe doesn't seem to work with vim, for some reason. bind-key u capture-pane \; save-buffer /tmp/tmux-buffer \; new-window -n "urlview" '$SHELL -c "urlview < /tmp/tmux-buffer"'
And with that I can just hit `<prefix> u` to pipe the current pane into urlview. Typical use is for example when pushing to a branch and bitbucket prints the URL to create a PR to stdout. I hit `<prefix> u`, `<enter>` and the link is open in Firefox.- ctrl-a [
- move to the text you want
- press space to start selection
- press space to end selection
- go to another screen, ctrl-a ] to paste
copy mode and scrollback in gnu screen is great to quickly scan tail -f commands.
Tmux don’t support this with `set -g mouse on`, but you can add this to your tmux.conf:
``` unbind-key MouseDown2Pane bind-key -n MouseDown2Pane run "tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer" ```
However, this only works fine within tmux alone. When you select text in tmux, you can’t use middle button to paste them into other programs, the opposite is the same. I don’t know why.
bind-key -T copy-mode-vi MouseDragEnd1Pane send -X copy-pipe-no-clear 'xclip -i -selection primary'
(or -T copy-mode, if you use emacs-flavored tmux)Then you can paste your tmux selections into other programs with middle-click -- I think by default tmux only uses the "clipboard" selection, but middle click pastes the "primary" selection.
But my tmux-fu isn't really up to scratch on how you do that...
git log - see a commitish I want to fixup cmd-f type first few letters hit tab to get to the end of the commitish cmd-c esc cmd-v to paste it
The other great thing is when you have cmd-f going and there are multiple hits in your terminal, hitting enter cycles you through them backwards.
Outside tmux, to send buffers to stdout, I use save-buffer.
I usually paste inside tmux. I re-attach then paste with the keyboard sortcut "Ctrl-B ]"
I put tmux commands like capture, load-buffer, save-buffer and list-buffers in one line shell scripts with short names for use outside tmux.
Otherwise typing the commands is too verbose, e.g.,
tmux capture -t0 -p|grep -o PATTERN|tmux loadb -;tmux lsb tmuxup(){tmux copy-mode -u}
zle -N tmuxup
bindkey '^[v' tmuxup
The advantage of this is that you don't hijack M-v from a real emacs running in tmux. It's only active when I need it to: while sitting at the shell prompt.I guess you could also bind it to PgUp or C-u if you are using the wrong editor.
Anyway, when I select something with a mouse, it is not possible (or at least I've not found a solution) to let go of the MB1, and that automatically quits visual mode. And the other thing is when you scroll down to the bottom, it automatically quits visual mode.
# native mode, if your terminal supports it
bind-key -T copy-mode-vi MouseDragEnd1Pane send -X copy-selection-and-cancel
# or if you're using X, and want it to work like any other program
bind-key -T copy-mode-vi MouseDragEnd1Pane send -X copy-pipe-and-cancel 'xclip -i -selection primary'
The scroll thing is a little hairier looking: bind -n WheelUpPane if -Ft= "#{mouse_any_flag}" { send -M } { if -Ft= "#{pane_in_mode}" { send -M } { copy-mode -t= }
That's just the default binding for WheelUpPane, except using bare copy-mode instead of copy-mode -e (-e is auto-exit when you scroll to the bottom).PS: And when the terminal session exits, the history is still a vim buffer - that's awesome!
(In fact there are a host of these; for example, vim-slime now can communicate with Vim’s built-in terminal.)
If your terminal emulator doesn't support copy escape codes, and you're running it on a remote server, you need to ssh with X forwarding in order to share your system clipboard.
I realized over the course of this thread that I know a weird amount about configuring tmux. I rely on this feature pretty heavily, so I've had to set up system clipboard integration on Mac and Linux across on multiple different terminal emulators. I'd be happy to help you get a config that works on your system, if you want.
To be very clear here, I can copy text from pane to pane. (using control+] to paste)
Judging by some recently commented lines in my tmux.conf. I've tried: # bind C-y run "tmux save-buffer - | xclip -selection c"\; display-message "Buffer copied to clipboard" # bind y "xsel -i --clipboard" # bind-key -T copy-mode-vi y send-keys -X copy-pipe "xclip"
It also plays wonderfully nice with tmux
>tmux’s copy commands [may not] integrate with the system clipboard [by default on all terminal emulators]