The following code enables clicks to move cursor in bash/readline on terminal emulators.
- Enable xterm mouse tracking reporting
- Set readline bindings to consume the escape sequence generated by clicks
Tested on xterm and: alacritty, kitty, gnome-terminal
In a bash shell, source mouse.sh.
eval "$(curl -X GET https://raw.githubusercontent.com/tinmarino/mouse_xterm/master/mouse.sh)" && mouse_track_start
Or permanently
git clone --depth=1 https://github.com/tinmarino/mouse_xterm Mouse && cd Mouse
source mouse.sh && mouse_track_start # This can be in your bashrc
- Create a tmux bind-key to MouseDown1Pane (currently it is select-pane)
- Take care of other buttons: 2 and 3 that are entering escpe sequeence in terminal
- Pressing Escape and mouse is escaping the mouse and then do not get the readline binding
Xterm have a mouse tracking feature
printf '\e[?1000;1006;1015h' # Enable tracking
printf '\e[?1000;1006;1015l' # Disable tracking
read # Read and prrint stdin full escape sequences, escape look like ^[, click like ^[[<0;36;26M
man console_codes # Some of them
- Mouse click looks like
\e[<0;3;21M
and a release\e[<0;3;21
. Where2
is x (from left) and22
is y (from top) - Mouse whell up :
\e[<64;3;21M
- Mouse whell down :
\e[<65;3;21M
- Press
C-v
after enabling the mouse tracking to see that
Multiple lines: press <C-v><C-j>
for line continuation (or just <C-J>
, if bind '"\n": self-insert'
)
Readline can trigger a bash callback
bind -x '"\e[<64;": mouse_void_cb' # Cannot be put in .inputrc
bind '"\C-h" : "$(date) \e\C-e\ef\ef\ef\ef\ef"' #Can be put in .inputrc
Readline can call multiple functions
# Mouse cursor to begining-of-line before calling click callback
bind '"\C-98" : beginning-of-line'
bind -x '"\C-99" : mouse_0_cb'
bind '"\e[<0;": "\C-98\C-99"'
Readline callback can change cursor (point) position with READLINE_POINT
environment variable
bind -x '"\C-h" : xterm_test'
function xterm_test {
printf "%s" "line is $READLINE_LINE and point $READLINE_POINT and mark $READLINE_LINE"
READLINE_POINT=24 # The cursor position (0 for begining of command)
READLINE_LINE='coco' # The command line current content
}
TODO no comment yet, I could not invoke a readline command or I would have lost $term->{point}
Ipython supports mouse. See Ipython/terminal/shortcuts -> Prompt-toolkit/bingin.mouse
ipython --TerminalInteractiveShell.mouse_support=True
Or to enable at startup write in .ipython/profile_default/ipython_config.py
c = get_config()
c.TerminalInteractiveShell.mouse_support
- OK : bash, ipython3, tmux
- NO : python, reply
- DISABLED : vim
- Xterm control sequences
- Ctrl keys as used in vim source
- zsh script for mouse tracking : the same but in zsh (not bash)