4

I often run my research experiments through my terminal. The command is a very long one, and often I find the need to change a parameter to the call. The problem is that the call line is long, and the parameter is for example in the middle of the line. To replace the parameter I must keep holding the left arrow/right arrow until I reach the point where the parameter is, then delete and rewrite the characters.

Is there a way to navigate the terminal command line using the mouse cursor position? Clicking in the middle of the line doesn't do anything, of course.

Example:

Suppose I have, in the terminal:

~> runThisProgram(a,b,c,d,......,v,w,x,y,z).

Now I want to run it but with X instead of the parameter c. I have to go to the end of that line, keep pressing the left arrow until I reach the c, and then delete and write X. (or, symmetrically, go to the beginning of the line, keep pressing the right arrow until I reach the c and then delete and write X). Is there a command to just point at the 'c' position using my cursor and go there?

3

4 Answers 4

3

You can move word at a time in shell: Alt+B moves cursor one word backward, Alt+F moves one word forward. Alt+D deletes word after cursor. More hotkey combinations for bash are explained in the manual.

1
  • 3
    not an answer for "Is there a way to navigate the terminal command line using the mouse cursor position?" I don't get why linux and ubuntu in particular is so unfrendly to users. even windows console can navigate through text with mouse clicks
    – deviant
    Sep 14, 2018 at 10:37
3

There are several ways to do what you want.

Interactive mode

Modern (& most popular) shells, like bash and zsh use GNU readline as input library. See man 3 readline, section Editing Commands. There are two modes : emacs and vi.

 Emacs style

set -o emacs

@sebasth's answer covers this part.

CTRL+P (or UP): Last command

ALT+B: Word backward

ALT+F: Word forward

ALT+D: Delete next word

ALT+backspace: Delete previous word

CTRL+A: Beginning of the line

CTRL+E: End of the line

 Vi style

set -o vi

Press I to go in insertion mode and ESC to go in command mode. Go in command mode and then:

K: Last command

B: Word backward

W: Word forward

DW: Delete next word

DB: Delete previous word

0: Beginning of the line

$: End of the line

Using an external editor

fc will paste your last command (you can also specify another command in your history) in your favorite editor (see $EDITOR variable). The command will be run when exiting the editor.

When the command line start getting longer and longer you might want to use a real editor instead of shell's readline. As a bonus, you can save it as a script file on your disk.

Modify just 1 parameter

Using ^before^after allows to replace string before by after.

For instance:

abitbol@localhost $ echo 1 2 3 4 5 6 7 
1 2 3 4 5 6 7

abitbol@localhost $ ^3^42
echo 1 2 42 4 5 6 7 
1 2 42 4 5 6 7
1
  • This is certainly useful information for someone wanting to edit long lines in the terminal, but it does not seem to answer the question. Jul 24, 2021 at 21:06
1

Wrap it in a simple shell script and use shell variables for the parameters that you'd like to tweak:

#!/bin/sh

param_c=340

./myprogram 1 2 "$param_c" \
            4 5 6 7
3
  • Most of the time the program I want to call would change. Sometimes the parameter I want to change changes. This is not relevant, I'm sorry. It is not logical to start creating scripts for every single kind of program I want to call and parameter I want to change.
    – TheNotMe
    Aug 19, 2017 at 13:49
  • @TheNotMe I thought you were referring to a single specific program when you wrote "The command is a very long one, and often I find the need to change a parameter to the call".
    – Kusalananda
    Aug 19, 2017 at 13:51
  • Sorry, my bad for not making it clearer. I upped your answer since it can be useful in that case.
    – TheNotMe
    Aug 19, 2017 at 14:08
1

It seems you are using bash so you could use a readline function that sets the variable READLINE_POINT, which is the position of the text cursor, to the desired column. A cheap way to find the column would be to select (with the mouse) all the text from the start of the line to the desired position, and use xsel to read this selection.

For example, to try it out, bind character ! to a function gotocolumn:

$ bind '-x "!":gotocolumn' 
$ gotocolumn(){ local x=$(xsel);READLINE_POINT=${#x}; }

Recall a long line, select a few characters from the front, type ! and the cursor is moved.


I just noticed that moving the text cursor to be at the mouse position is actually built-in to xterm with the readline-button function, and just needs a suitable binding. For example, run

xterm -xrm 'XTerm.VT100.translations:  #override\
 Mod4 <Btn1Down>: readline-button()\n'

then when you click the mouse left button with the Meta (or Super) key over a character in an input line, xterm generates left or right keys until the cursor lines up.

Similarly, urxvt has a built-in perl extension (see man urxvt-readline), enabled by default, with a binding of Shift and left button.

3
  • This seems to be the only answer here that actually attempts to answer the question. However, it is not clear to me after reading this answer how I would “use a readline function that sets the variable READLINE_POINT”. Is the example a script that I would source on login? Jul 24, 2021 at 21:10
  • 1
    The example 2 lines can be typed in to your bash shell to try it out. You can put the same 2 lines (without the $ ) into your ~/.bashrc which is sourced by any interactive bash. It should then be available in any terminal.
    – meuh
    Jul 25, 2021 at 12:11
  • 1
    @RandyCragun Just noticed there is a built-in solution for xterm and urxvt, details in updated answer.
    – meuh
    Jul 25, 2021 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.