Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some useful keyboard bindings #4

Open
embano1 opened this issue Jun 2, 2019 · 2 comments
Open

Add some useful keyboard bindings #4

embano1 opened this issue Jun 2, 2019 · 2 comments

Comments

@embano1
Copy link

embano1 commented Jun 2, 2019

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.

@rothgar
Copy link
Owner

rothgar commented Jun 2, 2019

Thanks for opening the issue. These are all great tips and I think they are all covered in various sections except for team completion on kill.

Check out the history, line movement, and widgets sections. Let me know if you think the explainations of those keyboard shortcuts aren't clear. On some cases I r bound the widgets to different keyboard shortcuts because I didn't look up what the defaults were. (I should have)

@php-coder
Copy link

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.

I'm typically use Ctrl+l (it's L) for that. It works regardless a shell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants