158

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?

Luis Alvarado
  • 211,503
Stann
  • 15,116

5 Answers5

205

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
damadam
  • 2,833
Adam Byrtek
  • 9,811
  • 7
    +1 because this works even when, for some reasons, ctrl-arrows do not work. Worth to note, for screen users, Ctrl-A becomes Ctrl-A A. – enzotib May 27 '11 at 18:54
  • 3
    To undo a deletion (or move text by deleting it), use Ctrl + Y. – Lekensteyn Jun 08 '11 at 22:48
  • 7
    Ctrl+Right arrow, Ctrl+Left arrow worth mentioning. – mac Jan 16 '14 at 15:04
  • 3
    On Ubuntu using Gnome and GnomeTerminal Alt-A opens the menu instead of moving the cursor. How do you use Alt-A with Gnome? I mean, Gnome is the default, so it's likely anyone reading this would be running a terminal in Gnome. – Jason Aug 05 '14 at 23:42
  • 1
    If you're connecting to Ubuntu from OS X over SSH you may have to use "Esc" instead of Ctrl e.g., Esc-A, Esc-E and so on. This is true for iTerm and Terminal. – Friedrich 'Fred' Clausen Jan 28 '15 at 00:30
  • I curious why Readline library doesn't provide jump to the middle, i.e. calculate the length of line, divide by two, and move cursor to this index. Is there any reason Readline library don't/can't provide this kind of handy functionality ? – 林果皞 Jun 05 '15 at 16:39
  • Vi or Emacs mode is more efficient way to navigate. There is an answer by Evan Teitelman. – Kadir Feb 24 '16 at 11:35
  • Some extra context, readline is the library that is used in Linux to read out commands. It uses emacs mode by default which means the same key combinations are available as in the emacs editor. It's possible to switch readline to vi mode. – progonkpa Dec 01 '17 at 08:30
  • Mac OS X actually supports some readline shortcuts throughout the interface -- even in text fields! – SilverWolf Feb 12 '18 at 00:15
  • How do I reassign this legendary Ctrl + Y to Ctrl + Z – Tejas Kale Feb 13 '18 at 14:46
  • Straightforward information without chitchat. Thanks! – neverMind9 Jul 09 '18 at 10:04
  • On ubuntu 14.04 gnome terminal, Alt + left arrow and Alt + right arrow allows you to move one word forward and backward. Alt + B works, but Alt + F opens the menu. – Alan Nov 10 '18 at 18:33
87

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
Pablo Bianchi
  • 15,657
karlacio
  • 1,252
17

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.

  • Great tip! To add on, please see this comment to configure your terminal to indicate the mode you're using. https://unix.stackexchange.com/questions/4870/is-it-possible-to-have-vim-key-bindings-in-terminal#comment1039830_4872 – FriskySaga Dec 02 '21 at 19:59
8

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).

Pablo Bianchi
  • 15,657
Kory Wnuk
  • 1,501
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.

marukse
  • 51