Skip to content

Add some useful keyboard bindings #4

@embano1

Description

@embano1

Hi Justin, create work here!

As discussed on Twitter, here are some shortcuts I use regularly in zsh that made my life easier. Note that not all of them apply (i.e. work) depending on the zsh binding mode, e.g. default, emacs/vi, etc. What I am describing here is out of the box UX with zsh (my env is oh-my-zsh on OSX).

search/replace a command from history

Say you executed the following command:

$ echo "Hi, I love BASH"

Using zsh's history search & replace functionality, one can easily change a string (or character, etc.):

$ !!:s/BASH/ZSH <TAB>

This will change BASH to ZSH. Additional functionality:

  • use gs instead of s to replace multiple occurrences
  • !! is the last command executed; but you can also use !-2 to get the second last (i.e. !-n n-last) command
  • if you want to undo a replacement (or any substitution) use CTRL+_ (underscore, that is SHIFT+ -)

get --help faster with zsh

Say you want to see all CLI flags for brew upgrade. Instead of brew upgrade --debug or searching man pages, it's as easy as:

$ brew upgrade --<TAB>

Kill processes faster

Instead of killing a process by finding its PID first, why not use zsh?

# kill a Google Chrome process by just typing the first characters
$ kill Goo<TAB>

Edit that super long command in <your favorite editor>

Say you have this crazy long command and need to replace one character in the middle of the line. What you want is to open your favorite $EDITOR and just do it. Well, zsh got you covered:

$ curl -s -i -k -d '{"texxt":"hello world"}' https://some.server.com

You want to fix this texxt typo, so while you have this line in your terminal just press CTRL-X-E (more precisely CTRL-X followed by CTRL-E). This will open the line in your $EDITOR, where you can make the change, save it (eg :wq in vi) and you will return to your terminal where you can then execute the command.

Stash the current command line

Say you would like to clear the screen before executing the command you already typed in your prompt. But you don't want to erase that long line. So instead of commenting out the line, copy & pasting it back and forth, why not use zsh to stash it :) ?

# this is the line we would like to stash
$ curl -s -i -k -d '{"texxt":"hello world"}' https://some.server.com

While you're in the prompt, just hit ESC-q. Then type the intermediate command you want to execute, e.g. clear (the screen). And after that, zsh will restore the stashed command.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions