20

Is there any terminal shell/emulator out there that supports editing your current command with the mouse?

Specifically things like placing the cursor by clicking (useful for long commands) or double clicking and pressing delete to select and delete a word etc.

For example, the terminal at the bottom of Midnight Commander (mc) has support for placing the cursor by mouse click. I'm looking for something similar that is more terminal-focused, as mc is mainly a file manager.

It's fine if it only works under a GUI environment (I'm on Ubuntu 18.04 with GNOME3).

4
  • 2
    Midnight Commander is as much a terminal application as any other, and what you are looking for is shells, that understand mouse input because their ZLE/libedit/Readline libraries do. The terminal emulators have provided mouse input capabilities for years. What you are looking for is shells that enable and understand such input. Commented May 18, 2018 at 14:19
  • 1
    I could also imagine a terminal emulator (use whatever word) that would fully work like a normal GUI application, say like MATLAB, where you can properly edit your command in the modern way with a thin line cursor, right click menus etc., then press enter and then the whole line would be given to bash as one command. However I can see how this would be problematic for interactive terminal applications. I'm just brainstorming because I find the current ways quite inconvenient. Commented May 18, 2018 at 14:30
  • That wouldn't work, ironically because the ZLE/libedit/Readline libraries would get in the way. In the POSIX General Terminal Interface architecture, editing is either a function of the applications softwares themselves (such as shells with ZLE/libedit/Readline) or of the line discipline when in canonical mode. Terminals are not block mode and there is no local editing. What you are looking for is shells where the editor library knows about mouse reports. Commented May 18, 2018 at 15:10
  • Konsole from KDE (and other terminals) do this for copy and paste by default. The challenge is for cut and delete. Commented Aug 8, 2023 at 22:10

3 Answers 3

17

zsh can be extended to support mouse operation like you describe, using Stéphane Chazelas’ mouse.zsh ZLE widget:

wget http://stchaz.free.fr/mouse.zsh
. ./mouse.zsh
zle-toggle-mouse

(and once you’ve tested it, add it to your ~/.zshrc).

It will work in any terminal with VT200 mouse tracking, and in the Linux console with gpm.

4
  • 2
    This goes in the right direction, but unfortunately the mouse can either be used to edit the current command or to select text from previous outputs but not both at the same time. Commented May 18, 2018 at 19:22
  • 2
    @isarandi it’s a bit complicated to provide both simultaneously in this kind of context, within an existing terminal. If you bind zle-toggle-mouse to a key, as suggested in the documentation, you can toggle the behaviour even while you’re editing a command, which alleviates the problem somewhat... Commented May 18, 2018 at 19:31
  • 3
    For the record, I (the author) never really used it in the end. It was more to see if it could be done. It was also written almost 20 years ago, it could probably be improved to use newer features of zsh. I've noticed a few forks on github with some improvements. IIRC it was pulled into oh-my-zsh at some point, I don't know if it's still there. Commented Jun 21, 2022 at 13:21
  • @StéphaneChazelas (btw, im a big fan of your contributions) I'm trying to take a crack at getting ctrl+shift+left/right (or alt+left/right) for selection and maybe cut working.. the plugin didnt change much for me.. any ideas? I sourced it in .zshrc but Konsole already has mouse copy and paste.. cut doesnt seem to work (maybe it wasnt intended to do that). Any idea how I might map alt+right ^[[1;3C to mouse drag right selection? Commented Aug 8, 2023 at 22:20
0

This can be done in emacs vterm.

(defun my-vterm-goto-char-at-mouse-position ()
  "Jump to the current point in the vterm buffer."
  (interactive)
 (let ((current-point (point)))
                      (vterm-goto-char current-point)
                      (message "Moved to char position %d" current-point)))


(with-eval-after-load 'vterm
  (define-key vterm-mode-map [mouse-1] 'my-vterm-goto-char-at-mouse-position))

(defun my-vterm-smart-backspace ()
  "In vterm, if a region is active, move to its end and send backspaces to delete it.
Otherwise, send a normal backspace."
  (interactive)
  (if (use-region-p)
      (let* ((start (min (region-beginning) (region-end)))
             (end (max (region-beginning) (region-end)))
             (n (- end start)))
        (deactivate-mark)
        (vterm-goto-char end)
        (dotimes (_ n)
          (vterm-send-backspace)))
    (vterm-send-backspace)))

(with-eval-after-load 'vterm
  (define-key vterm-mode-map (kbd "<backspace>") 'my-vterm-smart-backspace))

Emacs-libvterm (vterm) is fully-fledged terminal emulator inside GNU Emacs based on libvterm, a C library.

Terminal (macOS), iTerm, and others support option-clicking for setting point.

-1

While not built-in, you can install package gpm:

sudo apt-get install gpm
sudo /etc/init.d/gpm stop | start

Works well for CTRL+ALT+Fx console, with any shell.

1
  • 7
    This doesn’t answer the question: the OP is looking for the ability to edit the current command with the mouse (at minimum, place the cursor), which gpm doesn’t provide. Commented Jun 21, 2022 at 8:23

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.