How to navigate long commands faster?

Is there a way speed up Linux CLI navigation when I must enter long commands?
I simply use arrows right now, and – if I have a long command it takes some time to get from start of the command to the middle of it.

Is there a way to for example jump to the middle of the command without using arrows?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Some useful line editing key bindings provided by the Readline library:

  • Ctrl + A: go to the beginning of line
  • Ctrl + E: go to the end of line
  • Alt + B: skip one word backward
  • Alt + F: skip one word forward
  • Ctrl + U: delete to the beginning of line
  • Ctrl + K: delete to the end of line
  • Alt + D: delete to the end of word

Solution 2

Some more shortcuts from here

Ctrl + a  – Go to the start of the command line
Ctrl + e  – Go to the end of the command line
Ctrl + k  – Delete from cursor to the end of the command line
Ctrl + u  – Delete from cursor to the start of the command line
Ctrl + w  – Delete from cursor to start of word (i.e. delete backwards one word)
Ctrl + y  – Paste word or text that was cut using one of the deletion shortcuts (such as the one above) after the cursor
Ctrl + xx – Move between start of command line and current cursor position (and back again)
Alt  + b  – Move backward one word (or go to start of word the cursor is currently on)
Alt  + f  – Move forward one word (or go to end of word the cursor is currently on)
Alt  + d  – Delete to end of word starting at cursor (whole word if cursor is at the beginning of word)
Alt  + c  – Capitalize to end of word starting at cursor (whole word if cursor is at the beginning of word)
Alt  + u  – Make uppercase from cursor to end of word
Alt  + l  – Make lowercase from cursor to end of word
Alt  + t  – Swap current word with previous
Ctrl + f  – Move forward one character
Ctrl + b  – Move backward one character
Ctrl + d  – Delete character under the cursor
Ctrl + h  – Delete character before the cursor
Ctrl + t  – Swap character under cursor with the previous one

Solution 3

If you’re a vi[m] and bash user, you may find it useful to make readline (used by bash) use vi-style editing by adding set editing-mode vi to your ~/.inputrc or /etc/inputrc files. Or, you could just make bash use vi-style editing by running the bash command set -o vi. Add the command to your ~/.bashrc file to make the behavior persistent.

If you’re a zsh user, add bindkey -v to your .zshrc file for vi-style editting.

Solution 4

I do not know of a way to specifically jump to the middle without using the cursor keys. However, I can recommend using Ctrl + cursor key to move from blank to blank (i.e., jump from one word to another).

Solution 5

Source the code-snippet below in your .bashrc. Ctrl-a jumps to the start and pressing Ctrl-a again jumps to the middle.

jump_mid() {
    if [ "$READLINE_POINT" -eq "0" ]; then
        LEN=${#READLINE_LINE}
        POS=$(($LEN / 2))
        READLINE_POINT=$POS
    else
        READLINE_POINT=0
    fi
}
bind -x '"\C-a" : jump_mid'

Or if you want to use Ctrl-Something to directly jump to the middle, change the code to:

jump_mid() {
    LEN=${#READLINE_LINE}
    POS=$(($LEN / 2))
    READLINE_POINT=$POS
}

And bind it to something different than Ctrl-a.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply