Skip to content

Mouse support to move (readline) cursor on xterm (Bash script)

Notifications You must be signed in to change notification settings

tinmarino/mouse_xterm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Mouse support on readline

The following code enables clicks to move cursor in bash/readline on terminal emulators.

  1. Enable xterm mouse tracking reporting
  2. Set readline bindings to consume the escape sequence generated by clicks

Tested on xterm and: alacritty, kitty, gnome-terminal

Quickstart

In a bash shell, source mouse.sh.

eval "$(curl -X GET https://raw.githubusercontent.com/tinmarino/mouse_xterm/master/mouse.sh)" && mousetrack_start

Or permanently

git clone --depth=1 https://github.com/tinmarino/mouse_xterm Mouse && cd Mouse
source mouse.sh && mousetrack_start  # This can be in your bashrc

TODO

  • Avoid terminal blinking when trigger readline
  • Clearify arithmetic
  • 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

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
vim /usr/share/doc/xterm/ctlseqs.txt.gz  # ctlseqs local documentation
  • Mouse click looks like \e[<0;3;21M and a release \e[<0;3;21. Where 2 is x (from left) and 22 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

Bash, Readline

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
}

Perl (reply)

TODO no comment yet, I could not invoke a readline command or I would have lost $term->{point}

Python (ipython)

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

Limitations

  • OK : bash, ipython3, tmux
  • NO : python, reply
  • DISABLED : vim

Changelog

  • Feature: If at after last character of a line, put cursor at lat char of this line <= and not the next line as calculated now
  • Get log with call depth
  • Fix: sleep at read cursor if keep cmouse click
  • Add date to log

Links

About

Mouse support to move (readline) cursor on xterm (Bash script)

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages