qcb67q
Last Updated: February 25, 2016
·
11.05K
· codingfu
0f5d6d034fec42219ff13d6a470406a5

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.

Say Thanks
Respond

28 Responses
Add your response

845
Baa615b6f6d95f163ba87cb36362c9c4

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

over 1 year ago ·
847
2d20d0edc591c7e4524dd7e2e1a91b96

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 ·
850
F9d47b3d6001500a435163fa341f3a4d

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 ·
854
Fb1d7bd4fe188a5c093ae1f590c91c2a

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

over 1 year ago ·
861
2cd3c6586f719bc64a324e8bf2d74333

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 ·
865
6660b11915eb8a3f1916c7c8bc9710d3

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 ·
869
657b80c8a3765da586652fb8da6d9ef7

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

over 1 year ago ·
870

What's wrong with the HOME and END keys? Or the Ctrl+Arrow combination? Too old to be used?

over 1 year ago ·
873
7cef33cd28a38c894b9f68812fd085cd

@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 ·
874
E1969577c59e1501820aeccf403b36b0

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 ·
933
Me 2014

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 ·
955
543aba69f8cc74c8b0a9c78417e19ecd

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

over 1 year ago ·
987
7cef33cd28a38c894b9f68812fd085cd

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 ·
994
0f5d6d034fec42219ff13d6a470406a5

@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 ·
1274
5f80219d708a330825a60d7078efd11f

bash, replace first r with t.
$ gir clone foo
gir: command not found
$ !!:s/r/t
git clone foo
Initialized empty Git repository in /tmp/foo
warning: You appear to have cloned an empty repository.

over 1 year ago ·
5706
B328001a26b1e0d04ec877aa3f100abf

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 ·
5707
0f5d6d034fec42219ff13d6a470406a5

@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 ·
6399
B766563b73ab8bac01ce0e3811c9cea7

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 ·
6401
Avatar ika

Ctrl+A/Ctrl+E are Unix shortcuts.

over 1 year ago ·
6415
064d413d41a6347b63bacc0c56d4bb85

@jcmuller You made my day.

over 1 year ago ·
6470
Eb3fbc788f1c52fec3e8fe7f54598abe

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

over 1 year ago ·
6493
03a4308a1cdad717ab7ace9886e268c5

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 ·
8079
Ba1ed7ea63b8f2502a687a26a3e61ae6

In Ubuntu, I used these shortcuts.

over 1 year ago ·
11003
Mm at beach 1022x731

"...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 ·
11883
B9ejipjj normal

@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 ·
12195
Koala

Beautiful.

over 1 year ago ·
12897
My face

nice shortcut..appreciate it

over 1 year ago ·
16941
Images

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 ·
Filed Under