Last Updated: January 16, 2023
·
11.35K
· codingfu

Shortcuts that save time

Imagine the following situation: you would like to clone textmate repo and make a typo in a word git ('_' is your cursor position):

$ gir clone https://github.com/textmate/textmate _

So you'd like to correct it and... Well, the vast majority of programmers that I know will hit left arrow key over 30 times to get there because they don't know that:

Ctrl-A returns cursor to the beginning of current line,
Ctrl-E moves cursor to the end of the line,
Ctrl-K deletes the rest of the line, beginning from the cursor.

Those shortcuts work on OSX almost everywhere, for example in Safari address bar.

26 Responses
Add your response

You can avoid reaching for the arrow keys altogether with Ctrl-F and Ctrl-B for forward and backward character.

over 1 year ago ·

You can hit C-X C-E to open your $EDITOR with the command typed in the buffer and quickly edit your error. C-X C-E is a 'bash'ism that can be added to zsh quite easily by adding

autoload edit-command-line
zle -N edit-command-line
bindkey '^X^E' edit-command-line

to your .zshrc.

over 1 year ago ·

On a related note, C-X C-X will move your cursor back and forth between the start and the end of the current line every time you type it.

over 1 year ago ·

For those that don't know, many of these are Emacs keybindings.

over 1 year ago ·

And if you've already hit enter and the shell complains about your typo (gir e.g.), you can use carats to rerun with replacements.
$ gir clone https://github.com/textmate/textmate No command 'gir' found, did you mean: [...] $ ^gir^git^ git clone https://github.com/textmate/textmate

over 1 year ago ·

More info:

This is (lib)readline functionality, and by default it comes with 2 line editing modes, emacs (the default) and vim. So if you know emacs (or don't), you should try these too:

ALT-B and ALT-F (move one word backward and forward)

CTRL-Y (to Yank/Paste what was cut using CTRL-K), and yes it works just like a clipboard.

ALT-D (to remove the next word)

You could also switch libreadline to use the vim shortcuts if vim is your thing. I prefer to get used to the emacs ones since they are the default everywhere.

Enjoy

over 1 year ago ·

The other shortcut I always use is Ctrl-W to delete the word before the cursor.

over 1 year ago ·

@brucardoso2 There is no HOME and END keys on Apple keyboard, and Ctrl+Arrow does not work out of box on Mac OS X.

over 1 year ago ·

I just set -o vi (or set editing-mode vi on my .inputrc). Replaces all the bindings mentioned above with vi ones. To fix this particular typo: ESC 0 fr rt (spaces added for readability)
ESC: enter editing mode
0: go to start of line
fr: goto first r (the r in 'gir'), alternatively ll (right, right)
rt: replace letter with t
A mouthful indeed, but second nature if you're used to vim. A primary advantage is that your hands don't have to leave the home position in search of arrow keys or what not...

over 1 year ago ·

My personal favourite is Ctrl + U to delete all characters on the current line, quite useful if you've just typed the wrong command/set of commands.

over 1 year ago ·

@cyluss there are on keyboards with numeric pad, on those without one, you can combine Fn + or .

over 1 year ago ·

I have Microsoft keyboard with these home and end keys, but they don't work in text field. Ctrl+Arrow does not work in Terminal. OP's method is just works everywhere, with any keyboard.

over 1 year ago ·

@amr by default on OS X alt+key enters those strange phonetic symbols in th terminal, so that doesn't work :(

over 1 year ago ·

You can also position your cursor anywhere on the current command line text using opt-click at that position in OS X Terminal. Nice if you want to position the cursor somewhere in the middle of the command and you're using ssh on a slow link.

over 1 year ago ·

@man8 nice! Also you can copy and paste rectangular selection via holding Opt when selecting text. It also works in iTerm 2, however you should hold Cmd+Opt in this case.

over 1 year ago ·

If you're wanting to save time, just hit return and then y in response to the question:

zsh: correct 'gir' to 'git' [nyae]?

(Yes, you need to be using zsh, of course.)

over 1 year ago ·

Ctrl+A/Ctrl+E are Unix shortcuts.

over 1 year ago ·

@jcmuller You made my day.

over 1 year ago ·

ctrl+u deletes the line from the end. So you don't have to ctrl+a ctrl+k.

over 1 year ago ·

ctrl+u</code> deletes the line 'from your cursor to beginning of line'

ctrl+y</code> pastes the last text cut with ctrl+u.

These two in combination are essentially a second clipboard for the command-line

over 1 year ago ·

In Ubuntu, I used these shortcuts.

over 1 year ago ·

"...the vast majority of programmers that I know will hit left arrow key over 30 times to get there..."

...or you could just hit the key once and hold it down until the cursor arrives where you want it.

over 1 year ago ·

@codingfu yup, that's pity, but you can easily avoid that using ESC key as an alternative to ALT key for some shortcuts on mac os x terminal.
For example delete next word will be ESC D instead of ALT D

over 1 year ago ·

Beautiful.

over 1 year ago ·

nice shortcut..appreciate it

over 1 year ago ·

Also, opt-del will delete a word backward, ctrl-n in chrome with url drop down typeahead will allows you to go down the list and ctrl-p back up the list of options. (Hope these weren't already mentioned).

over 1 year ago ·