1

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

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
    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 '18 at 10:37
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
  • 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 '17 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 '17 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 '17 at 14:08
1

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
0

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.

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.