Search code examples
keyboard-shortcutscopy-pastetmux

Copy word under cursor in tmux?


I assume this is common operation but can't find any information about this. When in copy mode (Ctrl + [) Is there a shortcut to copy the word under the cursor without manually creating the selection?

I've tried to switch to vi-mode and use yw but it doesn't look like it works (in fact, I'm not entirely sure what vi-mode does besides mapping the arrow keys to hjkl).

Any idea how to achieve this?


Solution

  • Edit: As pointed out by @Sebastian Graf, tmux now has a native command: send-keys -X select-word for selecting a word. What follows I leave for posterity ...

    bind -n -T copy-mode-vi C-d send-keys b \; send -X begin-selection \; send-keys E
    

    I'm using the above to at least select the current word under the cursor. The shortcut is C-d as inspired by Sublime Text. You will need to have also set set-window-option -g mode-keys vi, but I believe that is default. In vi-mode the b moves the cursor to the beginning of the current word, then it begins selecting with begin-selection, then moves the cursor to the end of the current word with E.

    So you could also copy the word with:

    bind -n -T copy-mode-vi C-d send-keys b \; send -X begin-selection \; send-keys E \; send -x copy-selection.
    

    I've included all the commands here, because I couldn't find anywhere on the Internet that actually formatted it nicely.

    Function                   Vi         Emacs
    -------------------------------------------
    Back to indentation        ^          M-m
    Bottom of history          G          M-<
    Clear selection            Esc        C-g
    Copy selection             Enter      M-w
    Cursor down                j          Down
    Cursor left                h          Left
    Cursor right               l          Right
    Cursor to bottom line      L     
    Cursor to middle line      M          M-r
    Cursor to top line         H          M-R
    Cursor up                  k          Up
    Delete entire line         d          C-u
    Delete/Copy to end of line D          C-k
    End of line                $          C-e
    Go to line                 :          g
    Half page down             C-d        M-Down
    Half page up               C-u        M-Up
    Jump forward               f          f
    Jump backward              F          F
    Jump again                 ;          ;
    Jump again in reverse      ,          ,
    Next page                  C-f        PgDown
    Next space                 W
    Next space, end of word    E
    Next word                  w
    Next word end              e          M-f
    Paste buffer               p          C-y
    Previous page              C-b        PgUp
    Previous word              b          M-b
    Previous space             B
    Quit mode                  q          Esc
    Rectangle toggle           v          R
    Scroll down                C-Down/C-e C-Down
    Scroll up                  C-Up/C-y   C-Up
    Search again               n          n
    Search again in reverse    N          N
    Search backward            ?          C-r
    Search forward             /          C-s
    Start of line              0          C-a
    Start selection            Space      C-Space
    Top of history             g          M->
    Transpose chars            C-t