Skip to content
Hands-on IT IT Drills

Day 3: Editing the command line like a pro

Goal: You fix any command line in seconds, without hammering the arrow keys, and let zsh do the typing. · Duration: approx. 75 min · Prerequisite: Day 2

Briefing

  • The zsh line editor is called ZLE. It has an Emacs mode and a vi mode. The Emacs keys also work in bash, in psql, in Python and in every readline program.
  • Three groups cover 90 percent: jump (⌃A, ⌃E, ⌥B, ⌥F), delete (⌃W, ⌃U, ⌃K) and bring back (⌃Y).
  • You edit long commands in the editor: ⌃X⌃E.
  • History expansion (!!, !$, ^alt^neu), brace expansion ({a,b}) and glob qualifiers (*(.om)) save entire commands.
 ⌃A        ⌥B         ⌥F          ⌃E
 |←────────|←──  ▮  ──→|─────────→|
 git commit -m "fix: rabatt" --amend
 ⌃U deletes ←──  ▮  ──→ ⌃K deletes

Drill 1: Which mode is running?

bindkey -lL main
env -i HOME=$HOME TERM=$TERM EDITOR=nvim zsh -f -i -c 'bindkey -lL main'
env -i HOME=$HOME TERM=$TERM zsh -f -i -c 'bindkey -lL main'

Expected:

bindkey -A emacs main
bindkey -A viins main
bindkey -A emacs main

The trap: if $EDITOR or $VISUAL contains the letters vi, zsh switches to vi mode at startup. With EDITOR=nvim, ⌃A suddenly stops working. That’s why your lab .zshrc has contained bindkey -e since Day 2.

You can query directly which function is bound to a key:

for k in '^A' '^E' '^W' '^U' '^K' '^Y'; do bindkey $k; done

Expected:

"^A" beginning-of-line
"^E" end-of-line
"^W" backward-kill-word
"^U" kill-whole-line
"^K" kill-line
"^Y" yank

Always query only one key per bindkey call: two arguments in a row would rebind the first key to the second. ^A is the notation for ⌃A, ^[ stands for Escape, i.e. for ⌥ as Meta. The list of all functions is in man zshzle, section STANDARD WIDGETS.

Save your progress? Sign up for free

Drill 2: Jumping

Type the line, but without ⏎. Then press the keys and check where the cursor (▮) is. Finally, ⌃C discards the line:

git commit -m "fix: rabatt" --amend
Keys Cursor afterwards
⌃A ▮git commit -m "fix: rabatt" --amend
⌥F git ▮commit -m "fix: rabatt" --amend
⌃E git commit -m "fix: rabatt" --amend▮
⌥B git commit -m "fix: rabatt" ▮--amend
⌥B git commit -m "fix: ▮rabatt" --amend
⌥B git commit -m "▮fix: rabatt" --amend

Watch out, a difference from bash: in zsh, ⌥F jumps to the start of the next word, bash to the end of the current one.

With the “Natural Text Editing” preset from Day 1, ⌥← and ⌥→ work too. Ghostty and tmux, however, send a different sequence for ⌥← (^[[1;3D). You’ll bind that one right away as well.

For ⌥B, a whole word is everything made of letters, digits and the characters in $WORDCHARS:

echo $WORDCHARS

Expected:

*?_-.[]~=/&;!#$%^(){}<>

By default, the slash / is part of the word. So for zsh, /usr/local/bin is a single word. We’ll change that in Drill 3.

Save your progress? Sign up for free

Drill 3: Deleting and bringing back

Whatever you delete with ⌃W, ⌃U, ⌃K or ⌥D lands in the kill ring. ⌃Y brings it back. That’s cut and paste without a mouse.

First the default. Type echo /usr/local/bin, then ⌃W:

echo ▮

The whole path is gone because / is part of $WORDCHARS. Now you create your first ZLE file of your own:

cat > ~/term-lab/config/zsh/zle.zsh <<'EOF'
# ~/term-lab/config/zsh/zle.zsh – line editor (Day 3)
# ⌃W and ⌥⌫ stop at / . - : delete paths piece by piece
WORDCHARS=${WORDCHARS//[\/.-]}

# ⌃U like in bash: delete only left of the cursor
bindkey -M emacs '^U' backward-kill-line

# ⌃X⌃E: edit the current line in $EDITOR
autoload -Uz edit-command-line
zle -N edit-command-line
bindkey -M emacs '^X^E' edit-command-line

# ⌥← / ⌥→ in Ghostty and tmux too (xterm sequences)
bindkey -M emacs '^[[1;3D' backward-word
bindkey -M emacs '^[[1;3C' forward-word

# Space expands !!, !$ and !-2 immediately and visibly
bindkey -M emacs ' ' magic-space
EOF
exec ~/term-lab/lab
echo $WORDCHARS
for k in '^U' '^X^E'; do bindkey $k; done

Expected:

*?_[]~=&;!#$%^(){}<>
"^U" backward-kill-line
"^X^E" edit-command-line

${WORDCHARS//[\/.-]} is parameter expansion: replace every /, . and - with nothing. -M emacs binds specifically in the Emacs keymap. That way it doesn’t get in the way if another file switches the mode in between.

Now the exercises. On the left is the input, in the middle the keys, on the right the buffer afterwards:

Input Keys Result
echo /usr/local/bin ⌃W ⌃W echo /usr/
echo deploy/k8s/deployment.yaml ⌥⌫ echo deploy/k8s/deployment.
echo eins zwei drei ⌃A ⌥F ⌥D echo zwei drei
echo aaa bbb ccc ⌥B ⌥B ⌃K echo aaa
echo eins zwei drei ⌥B ⌃U drei
echo eins zwei ⌃W ⌃Y ⌃Y echo eins zweizwei
echo abc ⌃A ⌃K ⌃Y ⌃Y echo abcecho abc

The last row is the classic: ⌃A ⌃K cuts the whole line, ⌃Y pastes it back. That’s how you park a half-finished command, quickly type something else and bring it back. After ⌃Y, ⌥Y cycles through older entries in the kill ring.

Warning: ⌥⌫ needs “Option as Meta” (Day 1). With the “Natural Text Editing” preset, iTerm2 sends ^W for it and ^U for ⌘⌫. The result is the same.

Save your progress? Sign up for free

Drill 4: Undo, transpose, park

Input Keys Result
echo eins zwei ⌃W ⌃W ⌃_ echo eins
echo hallo welt ⌃T echo hallo wetl
echo abc def ⌥T echo def abc
echo shop ⌥B ⌥U echo SHOP
echo shop ⌥B ⌥C echo Shop

⌃_ is undo. On German keyboards that’s ⌃⇧- and therefore awkward. ⌃X⌃U is equivalent. ⌃T swaps the two characters before the cursor, ⌥T the two words. That rescues every typo like sl instead of ls.

The most underrated trick is push-line with ⌥Q:

  1. Type git commit -m "wip", but not ⏎.
  2. You realize you want to see the status first: ⌥Q. The line disappears.
  3. Type git status and ⏎.
  4. The parked command is back at the prompt, cursor at the end.

Expected after step 3:

[lab] projekt % git status
Auf Branch main
nichts zu committen, Arbeitsverzeichnis unverändert
[lab] projekt % git commit -m "wip"▮

Save your progress? Sign up for free

Drill 5: Long commands in the editor

For anything longer than one line, the editor is faster. Type:

kubectl --context staging --namespace webshop get pods -o wide

Not ⏎, but ⌃X⌃E. Neovim opens the command as a file. Change staging to prod with :s/staging/prod/ and save with :wq. The modified command is back at the prompt, not yet executed:

[lab] ~ % kubectl --context prod --namespace webshop get pods -o wide▮

With :cq you leave Neovim with an error code. Then zsh discards the line. And a command you’ve already sent, you pull straight into the editor with fc (Day 4).

Tip: For multi-line commands, such as a for loop or a curl with a JSON body, ⌃X⌃E is the only sensible way. Write them cleanly in the editor, wrapped with \, save, check, then ⏎.

Save your progress? Sign up for free

Drill 6: History expansion

Instead of retyping the last command, you refer to it. Thanks to magic-space from Drill 3, you see the substitution as soon as you press Space.

cd ~/term-lab/projekt
wc -l logs/app.log
grep ERROR !$ | head -2

Expected:

     500 logs/app.log
grep ERROR logs/app.log | head -2
2026-10-05T08:06:42 checkout ERROR request_id=r0006 user=u06
2026-10-05T08:16:52 checkout ERROR request_id=r0016 user=u16

!$ is the last word of the previous command. zsh shows the expanded line before it runs. Now the same search for warnings:

^ERROR^WARN

Expected:

grep WARN logs/app.log | head -2
2026-10-05T08:04:28 http WARN request_id=r0004 user=u04
2026-10-05T08:14:38 http WARN request_id=r0014 user=u14
Expression Meaning Typical use
!! entire last command sudo !!
!$ last word mkdir neu && cd !$
!^ first argument vim !^
!* all arguments ls -l !*
!-2 the command before last
!git last command starting with git
^alt^neu repeat last command with substitution typos

Even faster than !$ is ⌥.: it inserts the last word immediately. Pressing it repeatedly goes further back in the history.

ls internal/cart
cd ⌥.

Expected in the buffer:

[lab] projekt % cd internal/cart▮

Save your progress? Sign up for free

Drill 7: Brace expansion

Braces generate words before the command runs. With echo in front, you see what would happen:

echo cp config/app.yaml{,.bak}
echo mkdir -p tmp/{src,test}/{go,py}
echo {01..10..3} {a..e}

Expected:

cp config/app.yaml config/app.yaml.bak
mkdir -p tmp/src/go tmp/src/py tmp/test/go tmp/test/py
01 04 07 10 a b c d e

{,.bak} is the fastest backup in the world: an empty word plus .bak. Putting echo in front is a senior habit: look first, then ⌃A ⌥D ⌃D removes echo and ⏎ runs it.

Save your progress? Sign up for free

Drill 8: Globbing with qualifiers

With EXTENDED_GLOB (Day 2), zsh can do more than *. Many find calls become unnecessary:

print -l **/*.go
print -l **/*.(go|py)
print -l config/^*.json
print -l *(/) | head -3

Expected:

cmd/shop/main.go
internal/cart/cart.go
cmd/shop/main.go
internal/cart/cart.go
scripts/report.py
config/app.yaml
cmd
config
deploy

** goes recursively through all folders, (go|py) is an alternation, ^ negates. The parentheses at the end are a qualifier:

print -l **/*(.Lk+10)
print -l **/*(.om[1,2])
print -l **/*(.x)
print **/*.go(:t)

Expected:

logs/access.log
logs/app.log
logs/access.log
logs/app.log
scripts/report.py
cart.go main.go
Qualifier Meaning find equivalent
(.) / (/) files only / folders only -type f / -type d
(.Lk+10) larger than 10 KB -size +10k
(.om[1,2]) by modification time, newest two ls -t, two of them
(.mh-1) modified in the last hour -mmin -60
(.x) executable -perm +111
(N) no error without a match
(:t) / (:r) file name only / without extension basename

Once you’ve typed a glob, ⇥ shows the matches directly as completion. That’s how you check it before putting rm in front.

Warning: If a glob finds nothing, zsh aborts with no matches found, unlike bash. That’s intentional and protects you. With (N) or in quotes ('*.log' for find or rg), you bypass it deliberately.

Save your progress? Sign up for free

Shortcut table

Key Effect Mnemonic
⌃A / ⌃E start of line / end of line A = first letter, End
⌥B / ⌥F word back / forward Back, Forward
⌃B / ⌃F character back / forward same as above, just smaller
⌃W / ⌥⌫ delete word to the left Word gone
⌥D delete word to the right Delete word
⌃U delete everything left of the cursor Undo the line
⌃K delete everything right of the cursor Kill rest
⌃Y / ⌥Y paste / older entries Yank
⌃_ / ⌃X⌃U undo underscore = back
⌃T / ⌥T transpose characters / words Transpose
⌥U / ⌥L / ⌥C UPPER / lower / Capitalize Upper, Lower, Capital
⌥Q park the line, back after ⏎ Queue
⌥. last word of the previous command dot = the last one
⌃X⌃E edit the line in the editor eXternal Edit
⌃V + key insert key raw Verbatim
⌃C discard the line Cancel
⌃L clear the screen cLear

Checkpoint

LAB=${LAB:-~/term-lab}
ok() { local m=$1; shift; "$@" >/dev/null 2>&1 && echo "✔ $m" || echo "✘ $m"; }
lab() { (cd "$LAB/projekt" && env ZDOTDIR="$LAB/zsh" zsh -i -c "$1" 2>/dev/null); }
ok "Emacs-Modus"          test "$(lab 'bindkey -lL main')" = "bindkey -A emacs main"
ok "⌃U links löschen"     lab '[[ $(bindkey "^U") == *backward-kill-line ]]'
ok "⌃X⌃E Editor"          lab '[[ $(bindkey "^X^E") == *edit-command-line ]]'
ok "Leertaste expandiert" lab '[[ $(bindkey " ") == *magic-space ]]'
ok "/ trennt Wörter"      lab '[[ $WORDCHARS != */* ]]'
ok "Glob: 3 Quelldateien" test "$(lab 'print -l **/*.(go|py) | wc -l')" -eq 3

Expected:

✔ Emacs-Modus
✔ ⌃U links löschen
✔ ⌃X⌃E Editor
✔ Leertaste expandiert
✔ / trennt Wörter
✔ Glob: 3 Quelldateien

Plus the timed hands-on test: type kubectl get pods -n webshop -o wide and, without arrow keys, change pods to deploy and wide to yaml. Under ten seconds is good.

Challenge

In the project, all .yaml files should be renamed to .yml, including in subfolders. One command, no loop, and a dry run first.

Hint: zsh ships the zmv function. It just has to be loaded with autoload. -n only shows what would happen.

Solution

cd ~/term-lab/projekt
autoload -Uz zmv
zmv -n '(**/)(*).yaml' '$1$2.yml'

Expected:

mv -- config/app.yaml config/app.yml
mv -- deploy/k8s/deployment.yaml deploy/k8s/deployment.yml

The parentheses in the pattern capture parts, $1 is the folder path, $2 the name without the extension. The single quotes are mandatory, otherwise the shell expands beforehand. Without -n, it renames. You go back with zmv '(**/)(*).yml' '$1$2.yaml' or with git restore and git clean. A shorter way to write the same thing is zmv -W '**/*.yaml' '**/*.yml'.

Save your progress? Sign up for free

Enterprise Note

  • The Emacs keys also work in bash on servers, in psql, mysql, python3 and redis-cli. What you practice today, you take to every jump host.
  • ⌃X⌃E is the safe way to build dangerous commands: check in the editor first, then run. Especially with kubectl delete or terraform with many flags.
  • History expansion is powerful, but treacherous in scripts and runbooks. The full command belongs there, not !!.
  • Be careful with **/* in huge monorepos or on network drives: it walks through everything, including node_modules. That’s what fd and rg with .gitignore support are for (Day 6).
  • If someone on the team prefers vi keys: bindkey -v plus KEYTIMEOUT=1. Mixing is allowed, each person maintains that in their own configuration.