Skip to content

Instantly share code, notes, and snippets.

@wsgac
Last active May 19, 2026 23:14
Show Gist options
  • Select an option

  • Save wsgac/e495b550b23d9b96ecc1bf434ffa45cc to your computer and use it in GitHub Desktop.

Select an option

Save wsgac/e495b550b23d9b96ecc1bf434ffa45cc to your computer and use it in GitHub Desktop.
Attempt to emulate Emacs registers in Tmux
# -*- conf -*-
# Initialize alphanumerical registers to avoid the problem with 'delete-buffer'
run "tmux set-buffer -b a \"$(echo ' ')\""
run "tmux set-buffer -b b \"$(echo ' ')\""
run "tmux set-buffer -b c \"$(echo ' ')\""
run "tmux set-buffer -b d \"$(echo ' ')\""
run "tmux set-buffer -b e \"$(echo ' ')\""
run "tmux set-buffer -b f \"$(echo ' ')\""
run "tmux set-buffer -b g \"$(echo ' ')\""
run "tmux set-buffer -b h \"$(echo ' ')\""
run "tmux set-buffer -b i \"$(echo ' ')\""
run "tmux set-buffer -b j \"$(echo ' ')\""
run "tmux set-buffer -b k \"$(echo ' ')\""
run "tmux set-buffer -b l \"$(echo ' ')\""
run "tmux set-buffer -b m \"$(echo ' ')\""
run "tmux set-buffer -b n \"$(echo ' ')\""
run "tmux set-buffer -b o \"$(echo ' ')\""
run "tmux set-buffer -b p \"$(echo ' ')\""
run "tmux set-buffer -b q \"$(echo ' ')\""
run "tmux set-buffer -b r \"$(echo ' ')\""
run "tmux set-buffer -b s \"$(echo ' ')\""
run "tmux set-buffer -b t \"$(echo ' ')\""
run "tmux set-buffer -b u \"$(echo ' ')\""
run "tmux set-buffer -b v \"$(echo ' ')\""
run "tmux set-buffer -b w \"$(echo ' ')\""
run "tmux set-buffer -b x \"$(echo ' ')\""
run "tmux set-buffer -b y \"$(echo ' ')\""
run "tmux set-buffer -b z \"$(echo ' ')\""
# Copy to user-selected register
bind -T copy-mode r command-prompt -1 -p '(register)' 'delete-buffer -b %1 ; send -X copy-pipe "tmux set-buffer -n %1"'
# Paste from user-selected register
bind -T prefix C-] command-prompt -1 -p '(register)' 'paste-buffer -b %1'
# Split window inheriting $PWD from current pane
# Bind intuitive, yet unused keybindings
bind -T prefix | split-window -h -c "#{pane_current_path}"
bind -T prefix - split-window -c "#{pane_current_path}"
# Search backward and forward for PS1
# Tweak this to reflect your PS1
# PS1_MARKER="pimp-my-prompt"
# PS1_MARKER=$'\e]0;pimp-my-prompt\a'
# PS1_MARKER="XX"
# PS1_REGEX="^${PS1_MARKER}"
PS1_REGEX="^❯"
bind-key -N "Search backward for PS1" "C-p" {
copy-mode
send -XF search-backward $PS1_REGEX
}
bind-key -N "Search forward for PS1" "C-n" {
copy-mode
send -XF search-forward $PS1_REGEX
}
# Attempt to kill the highlighted PID in copy-mode. C-k goes straight
# for KILL. M-k uses `dmenu` to let the user choose the signal to be
# sent.
# bind -T copy-mode C-k 'send -X copy-pipe-and-cancel "xargs kill -9 "'
# bind -T copy-mode M-k 'send -X copy-pipe-and-cancel "xargs kill -$(kill -l | tr \" \" \"\n\" | dmenu -i -l 10)"'
bind -T copy-mode C-k 'send -X select-word; send -X copy-pipe-and-cancel "xargs kill -9"'
bind -T copy-mode M-k 'send -X select-word; send -X copy-pipe-and-cancel "xargs kill -$(kill -l | tr \" \" \"\n\" | dmenu -i -l 10)"'
set -g history-limit 1000000
set -g mouse on
unbind -n MouseDrag1Pane
set -g status-bg color28 #deepskyblue1 #brightcyan
@wsgac

wsgac commented Feb 12, 2019

Copy link
Copy Markdown
Author

This is an attempted solution of the problem, combining Tmux' notion of paste buffers. The first command (bound to r in copy-mode) prompts the user for a single character buffer name and then pipes the selection to that buffer (using set-buffer invoked via shell). The second command (bound to <prefix> C-]) simply prompts for the buffer and pastes its content. The current limitation shows when the user wants to overwrite an existing buffer. Instead, a new buffer gets created.

@wsgac

wsgac commented Feb 13, 2019

Copy link
Copy Markdown
Author

I've opted for a not-too-pretty-but-effective solution of pre-initializing a certain number of buffers (in my case, a to z) so that delete-buffer always has something to operate on.

@wsgac

wsgac commented Jul 25, 2024

Copy link
Copy Markdown
Author

I've added bindings for variants of the split commands that inherit $PWD from the current pane. It can be useful e.g. when opening multiple logs stored in some non-trivial directory hierarchy - saves repeated cd-ing to those directories. I picked intuitive keybindings: | makes a horizontal split and - - vertical.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment