
Moving efficiently in the CLI - okket
https://clementc.github.io/blog/2018/01/25/moving_cli/
======
Exuma
Here's a few more that the diagram is missing, along with a function I created
that inserts these commands (in dark gray so it's not intrusive) every time I
start ZSH shell, like flash cards.

Keep in mind, most of these work anywhere in OS X, not just in the terminal.

Put this in your .bashrc or .zshrc

    
    
        # MOTD
        function echo_color() {
          local color="$1"
          printf "${color}$2\033[0m\n"
        }
        echo_color "\033[0;90m" "c-f  Move forward"
        echo_color "\033[0;90m" "c-b  Move backward"
        echo_color "\033[0;90m" "c-p  Move up"
        echo_color "\033[0;90m" "c-n  Move down"
        echo_color "\033[0;90m" "c-a  Jump to beginning of line"
        echo_color "\033[0;90m" "c-e  Jump to end of line"
        echo_color "\033[0;90m" "c-d  Delete forward"
        echo_color "\033[0;90m" "c-h  Delete backward"
        echo_color "\033[0;90m" "c-k  Delete forward to end of line"
        echo_color "\033[0;90m" "c-u  Delete entire line"

~~~
zeptomu
Nice idea and important quick-movements.

However as a Vim user (mostly motivated by the fact that I can move around
pressing single keys which I find more "wrist-ergonomic" \- potential fast
navigation is a nice plus), I always wonder how heavy emacs users deal with
CTRL-acrobatics and movement - do you use c-f/c-b for going forward/backward
or the usual cursor keys?

For vi users it makes sense to set $readline (~/.inputrc) to vi-style, so for
completion here the vi-style bindings (although probably obvious if you used
to Vim).

    
    
      l "Move forward"
      h "Move backward"
      k "Move up" (step history backward)
      j "Move down" (step history forward)
      ^ "Jump to beginning of line"
      $ "Jump to end of line"
    

Deleting works as usual with `d` combinations. Furthermore it is convenient to
search the history via `/`.

~~~
molteanu
Honestly, I press both ctrl keys with my palms:
[http://ergoemacs.org/emacs/swap_CapsLock_Ctrl.html](http://ergoemacs.org/emacs/swap_CapsLock_Ctrl.html)

I'm hearing people say that "that's impossible" or "it's weird", but I've used
this method for ~2 years now. It has become muscle memory.

~~~
azaras
It is the BEST method.

I put a little pad over the Ctrl Keys to press it easily with muy palm.

------
itomato

        set -o vi
    

Vi keys in the editor, Emacs on the CLI? This has never made any sense to me.

[https://popcon.debian.org/main/editors/by_inst](https://popcon.debian.org/main/editors/by_inst)

~~~
mooreds
I came here to post that, glad you beat me to it. Reaching for that control
key? Ouch. Though I guess when you use vi keybindings, you are occasionally
reaching for the escape key.

But, there is something to be said for meeting people where they are. If folks
know the emacs keybindings, let them 'control' away!

~~~
Lio
As a dyed in the wool Vi user. I still want ctrl where I can reach it easily.

Ctrl-[ for escape and when in Insert mode, ctrl-h backspace; ctrl-w delete
word; ctrl-u delete line; Ctrl-m return; ctrl-i tab, etc.

~~~
SmirkingRevenge
Im a broken record in this thread right now... for goodness sake, remap
capslock to ctrl!!

~~~
wyclif
That's the first thing I do when I start a new MBP.

~~~
Lio
Yep, me too. The second thing I do is to remap the right alt key (used to be a
numeric keypad 'enter' key back on my original MacBook Pro) to a second ctrl
key.

I'm always confused by laptop designers who only include one ctrl key. How are
we supposed to properly touch type modifiers otherwise?

~~~
henriquemaia
> I'm always confused ...

Just stating the obvious: you’re not their target consumer.

~~~
Lio
Maybe, there's probably some truth to that.

Apple do make it easy to remap the Alt key though and include a terminal and
CLI commands by default so they must have some interest in business from
people like me. ;)

(I would like to start a campaign to get the Mac Unix science page back again
[https://www.macobserver.com/tmo/article/apple-drops-
science-...](https://www.macobserver.com/tmo/article/apple-drops-science-as-
core-market-web-page-deleted))

------
nextos
Those shortcuts are a subset of those offered by GNU Readline, which is e.g.
used by Bash and emulated by Zsh. Readline can offer Emacs-like (by default)
or Vi-like keybindings:

[https://en.wikipedia.org/wiki/GNU_Readline](https://en.wikipedia.org/wiki/GNU_Readline)

------
aequitas
I recently discovered that macOS has native support for these Emacs key
bindings ([https://jblevins.org/log/kbd](https://jblevins.org/log/kbd)) on
it's textfields. This includes things like text editors, browser address bars,
input fields and of course all terminal emulators.

I still prefer Vim as my editor, but learned these key combo's because they
are just so portable.

~~~
bocklund
Except option as meta (terminal excepted). I just want to M-f and M-b through
words and I cannot.

~~~
callalex
Good news! If you add these shortcuts to the override file in your home
directory it will override the alt-special-character behavior.

------
craigsmansion
To memorise moving around the cli:

C-f -> f for forward

C-b -> b for backward

C-p -> p for previous

C-n -> n for next

C-a -> a for Anfang

C-e -> e for Ende

C-k -> k for kill forward

C-u -> just memorise this one. It's the one you use when you suspect you've
mistyped a password in ssh or login shell.

Alt/meta sometimes does what Ctrl does, but bigger.

BONUS TIP: In GNU screen, you can use C-a-a for C-a to jump to the beginning
of the line. Now you no longer have a reason to passionately _hate_ the
default C-a meta-binding for screen. You're welcome.

------
inetknght
This is handy. But it would be far more handy to show _exactly_ where the
cursor goes. Moving left-ward puts the cursor at the beginning of the word.
Moving right-ward puts the cursor one-past-the-end of the word, not at the
last letter as is depicted in the diagram.

Anyway, it's far more intuitive to use arrow keys with modifiers:

* control + left-arrow = alt + b

* control + right-arrow = alt + f

* alt + backspace = control + w

I get super upset (pet peeve) when programs don't support word navigation like
this.

What I would _like_ would be for someone to build a web app which allows you
to select what modifiers you want for each of the shortcuts, and then has a
button which would generate configuration files for various programs (vim,
emacs, tmux, gnome-terminal, etc) that you can download or copy/paste. That
would be grand.

~~~
brucephillips
Arrow keys are slower than letters, since you have to move your hand.

~~~
inetknght
Not all keyboards are laid out the same.

~~~
brucephillips
There's no value in nitpicking. 99% of keyboards are standard qwerty.

~~~
SmirkingRevenge
And 100% of those keyboards can have simple keys remapped

~~~
brucephillips
What does that have to do with anything? The claim is that arrow keys are more
intuitive keybindings. If you're remapping keys, then the familiarity of keys
is irrelevant.

------
gumby
They are simply basic Emacs keystrokes. They also work in Mac text fields.

Exception: ^U and ^W are old unix "cooked" io keystrokes but the relevant
Emacs ones also work (e.g. meta-delete instead of ^W).

~~~
ubernostrum
They're readline keybindings. Anything which uses readline will support them.

~~~
gumby
I am well aware of this as Brian Fox wrote readline at my suggestion and we
were both by then Emacs users of many many years.

However the mac text widget doesn't use readline.

edit:s/than/then/

------
sigjuice
If you like to use the mouse:

In Terminal.app on macOS, alt + mouse click will move your cursor anywhere. I
am not sure how exactly this works. It also seems to work while I am inside
Terminal.app and logged on to a remote Linux system.

Also, simply clicking works while running a shell in Emacs using M-x shell

------
zython
If anyone else likes linux tools explained visually heres one for htop:

[https://codeahoy.com/2017/01/20/hhtop-explained-
visually/](https://codeahoy.com/2017/01/20/hhtop-explained-visually/)

------
paws
Random tidbit: back when Mac OS X first mapped these bindings to Cocoa, it
became possible to use them for evil -- to exploit OS X to grant access it
wasn't supposed to. [1]

For example if you had a screensaver with a password prompt, you could type
some characters, then: 1\. ctrl-a 2\. ctrl-k 3\. ctrl-y ...repeat steps 1-3
several times and hit enter.

The screensaver would crash to desktop, or on the logon prompt, you'd crash to
the console mode available by entering >console as a username.

I think these were largely patched by 10.3.0.[2]

[1] [https://www.cnet.com/news/mac-os-x-screensaver-security-
cras...](https://www.cnet.com/news/mac-os-x-screensaver-security-crashing-
screen-effects/) [2]
[https://apple.slashdot.org/story/03/07/05/187203/screensaver...](https://apple.slashdot.org/story/03/07/05/187203/screensaver-
bug-in-mac-os-x)

------
Yhippa
I moved to a 60% keyboard the past year and I wish I'd taken the time to
research this. This is a lifesaver.

------
niark
For Mac (iTerm) users, thanks to this thread
[https://stackoverflow.com/a/29403520/5233291](https://stackoverflow.com/a/29403520/5233291)
you can easily configure

    
    
      - alt+arrow to jump words,
      - cmd+arrow to jump to start/end of line,
      - cmd+z/cmd+shift+z for undo/redo,
      - alt+delete to delete word
    

Just discovered it, it's amazing

------
blunte
I believe these work because of readline, and the standard text controls in
MacOS use readline...

Learning keyboard shortcuts is very beneficial, at least in cases where you
often do the same things. CMD or Ctrl L is one of my favorites (jump to
address text box in most browsers).

It's a bit like coffee^h^h code (wow, autocorrect) optimization; a few
improvements in a few places can save an immense amount of work or time.

~~~
c256
The standard text boxes on macOS don’t use readline, they use NSText*, which
happens to implement similar things to the defaults for readline (because they
were created by people who used Emacs a lot, and readline defaults to Emacs
style). This is really only important because readline customizations won’t
affect macOS text boxes — but it will affect readline programs on macOS,
including bash.

------
petercooper
As lovely as this diagram is, are there any explanations as to _why_ these are
the characters used? I know a few of them but don't see any overall pattern.
Is there a logic to it, or do you just have to memorize it?

 _Update: Never mind. Turns out alt+most keys on macOS just types special
characters, making the ones I didn 't know useless to me anyway :-D_

~~~
ken
From Emacs: F/B are forward/back, D=delete, K=kill. E=end, and A was probably
used because S was already Search. W=word, maybe? Not sure how U came about
here (it's something different in Emacs).

BTW, if you're using Terminal.app, you have to explicitly enable "Use Option
as Meta key" in the preferences.

~~~
hoosieree
^H comes from ASCII.

------
foobarian
My favorite X-windows customization for improving the CLI experience (though
it helps with all editing) is cranking up the key repeat rate to a lot higher
than is possible in GUI control panels (and lowering the repeat delay):

xset r rate 160 80

I haven't found how to do this in OSX yet, but there is probably a way.

~~~
alsetmusic
> I haven't found how to do this in OSX yet, but there is probably a way.
    
    
      defaults write -g KeyRepeat -int 0.02
    
    

[https://github.com/herrbischoff/awesome-macos-command-
line](https://github.com/herrbischoff/awesome-macos-command-line)

~~~
foobarian
I never got that to work somehow. I'm sure I have the wrong OSX version.

------
octref
Thanks for sharing! I've been using C-A/E a lot, but not really Alt-B/F.
Wondering if there is a way to swap Alt-B/F to Ctrl-B/F? Moving between words
seems much useful than going one char forward/backward, and arrow keys get
those covered.

------
kakarot
The terminal might be the most ubiquitous piece of software between *nix
flavors, if there's a single application you should probably learn the
defaults for, it's gonna be that one.

Last thing you want to do is look like an idiot trying to fix your friend's
computer because you can't even remember how to get around in their default
bash shell.

This gives me an interesting idea, though... Are there any "portable shell"
applications that painlessly provide a mechanism for storing your preferred
shell, necessary dependencies and configuration files on a thumb drive that's
as easy to use as `/mnt/shelldrive/zsh`?

------
Kiro
I want a shortcut that moves from current location to 50% of the remainder.

~~~
craftyguy
Ohh, this would be interesting, assuming the jump would diminish by 50% each
time the shortcut was hit!

------
fbcpck
In macOS, you can use Karabiner Elements[1] and enable a set of complex
modification rules[2] to remap these to a more intuitive key combinations.

[1] [https://github.com/tekezo/Karabiner-
Elements](https://github.com/tekezo/Karabiner-Elements) [2]
[https://pqrs.org/osx/karabiner/complex_modifications/#termin...](https://pqrs.org/osx/karabiner/complex_modifications/#terminal_navi)

------
Symbiote
This is covered within the first 2-3 minutes of the Emacs tutorial, along with
some other shortcuts for moving around.

Run "emacs" and then press C-h t (Control-H then T) to start the tutorial.

Or you can read it [1], but the tutorial is interactive, so open it in Emacs
if you have it.

[1]
[http://www.cs.put.poznan.pl/anstroinski/data/uploads/sop1/ma...](http://www.cs.put.poznan.pl/anstroinski/data/uploads/sop1/materials/tutorial.txt)

------
dbdr
I sometimes wish to "delete forward/backward until character X" (where X might
be one of the quotes, a dash, period, etc). I know about Ctrl-W, but that will
use a fixed definition of what a word is, while what I need will depend on the
context. Is this possible in readline's emacs mode, or is it time to bite the
bullet and learn vi?

~~~
chadcmulligan
d/X in vi - you'll never go back

------
why-el
Some of these actually work in a surprising number of places, for instance in
the URL bar of both Safari and Chrome (assuming OSX). In fact I am so used to
them that when they are missing they are the first reminder I am not in my
preferred platform (or running a program that does not respect them).

------
acomjean
I use cntr -a cntrl e all the time.

I use the default mac terminal. I was excited because I'm always forward and
backing up in previous commands.

But alas... Alt F and Alt B just print characters (ƒ∫). I tried to see if
there was a configuration I could set, but to no avail. Maybe I need a better
terminal.

~~~
em500
The configuration is in Terminal Preferences -> Keyboard -> Use Option as Meta
key

~~~
acomjean
That works. Thanks! though you have to set it for each terminal type. (I use 3
colors...). It was a little annoying but done.

------
yla92
Here's a gist I made awhile ago for moving around and other useful shortcuts
and tools.

[https://gist.github.com/yelinaung/849c78c40784c56e05f5a04dde...](https://gist.github.com/yelinaung/849c78c40784c56e05f5a04dded9082d)

------
conradk
I use i3wm and always feel left out of these kinds of tips, because i3wm uses
Alt for all kinds of Window Management stuff. If I do Alt-F, my shell goes
fullscreen, because i3wm is setup to do that.

Any tips for i3wm users that have these kinds of keybind conflicts ?

~~~
sevanteri
I use Super instead of Alt. When I changed to i3 I instantly knew that alt
would conflict with everything in terminals.

------
erikcw
My favorite recent find in iPython is “alt-9-backspace” which deletes 9
“words” back. In my case clearing a uuid string in a Django ORM query:

MyModel.objects.filter(UUID=‘123e4567-e89b-12d3-a456-426655440000‘)

~~~
Symbiote
Alt+<number>, command is Emacs / readline etc for "Repeat this command number
times".

The number doesn't have to be a single digit, so Alt-3, Alt-6, Backspace, or
Alt-3, Alt-6, Ctrl-H also work (delete 36 characters.)

(Your command would normally be described is Alt-9, Alt-Backspace; or M-9
M-Backspace; to show the Alt/"Meta" key is held for both strokes.)

------
fiveFeet
The diagram is awesome! It helps in remembering these shortcuts better.

~~~
TheGrassyKnoll
Yes _VERY_ nice diagram.

------
herewulf
I use most of these on a regular computer but this is very helpful for
something like Termux. My smartphone doesn't even have arrow keys! The diagram
is very helpful.

------
xuxiaodong
More command line tips: [https://talk.linuxtoy.org/cli-
tips/#2](https://talk.linuxtoy.org/cli-tips/#2)

------
fulafel
In the OS X Terminal, is there yet a way to simultaneously get a working alt
key and the ability to type characters like []\/ when using an European KB
layout?

------
qplex
What about the arrow keys? Home/End? Delete? Backspace?

Seems pretty silly to key CTRL-something when there are dedicated buttons for
the functions already.

------
alant
I always wanted these! I’ve been using cmd/alt and arrow keys by accident many
times but no luck. Thank you!

------
wwarner
All those, and ctl-r <chars><ret> for searching history.

~~~
wwarner
Also -- ctl-r <chars> ctl-r ctl-r ret

To match two commands containing <chars>

------
pcmaffey
My dream command is

[somekey] + up

that scrolls through history sorted by frequency of use.

~~~
rossjudson
I've used this for years:

[https://github.com/dvorka/hstr](https://github.com/dvorka/hstr)

------
mattalbie
I'll just be over here, using acme.

~~~
c256
Hi! Where did you find a good 3-button mouse these days? Are you still living
off a hoard collected from back when?

------
mattacular
$ set -o vi

------
wu_tang_chris
lol no.

> set -o vi

------
ktaylor
set -o vi

------
ConSeannery
set -o vi

------
the_cat_kittles
oh my god. i cant be the only one here who had no idea these existed. thank
you

------
vladsanchez
Chris Pizzi taught me all these little tricks while we worked at Ezyield.com.
Thanks Chris!

------
ersing
we need this because first apple and later others ditched the navigation keys
from keyboard. good old days with home, end , page up/down delete keys etc.
nowadays they fuck with function keys first they replaced it with screen dim
and sound now the touchbar , future is not bright!

~~~
JadeNB
> they replaced it with screen dim and sound now the touchbar , future is not
> bright!

Well, sure; that's because you've used the screen dimmer.

