Hacker News new | ask | show | jobs
by joeblau 4715 days ago
Pretty comical video, one quick tip. If you typed a command on the terminal and you get the "Operation not permitted." You can run the last command prepending sudo like this:

  sudo !!
4 comments

You could also just replace the incorrect part of the previous command with ^old^new. I use that a lot when I want to doublecheck before taking a permanent action.

eg. Check what you're about to delete

  $ ls *.backup
  a.backup    b.backup    c.backup
  
  $ ^ls^rm
  rm *.backup

Something else that saves a lot of time is to incremental-search backwards through your command history using ctrl-r instead of arrow keys. eg. cycle through every "grep". Press ctrl-r, type grep, and it jumps to to most recent command that contains "grep". Each time you press ctrl-r it will jump further back in time. If it's something you expect to search for a lot, you can even tag commands with # comments then search for the comment. (There's a fine line there though... if you reuse a command really often you should probably alias or script it)

Command history uses the 'readline' library so all(?) the other editing-related emacs chords will work on it ctrl-a/ctrl-e to jump to the start/end of the line, ctrl-r/ctrl-s to search, alt-f/alt-b to jump words, etc. Oh, and an emacs kill-ring too, that's pretty useful.

Enjoy.

...

...

But there's one more thing.

This is a feature of GNU Readline, not a feature of bash. Other apps that use readline will also accept these chords.

Things like the ruby and python shells, mysql, etc.

You think you can do a lot in those tools now? Learning to leverage everything that readline gives you will take you to a whole new level.

Have fun exploring :)

Another neat expansion: !$ is the last argument of the last command you entered. Useful when you mistype the command name.
Alt+. does the same and (at least in Zsh) allows you to cycle backwards.
More like Meta Up/Down, I tried for ten minutes on iTerm2 before remembering to use my Escape key.
Can configure the alt (option)-key behavior in your iTerm2 profile.

Left/Right Option Key Acts As

It is common to use a modifier to send so-called "meta keys". For most users, selecting "+Esc" here is the right choice. The "Meta" option sets the high bit of the input character, and is not compatible with modern systems.

http://www.iterm2.com/#/section/documentation

Another HN user mentioned being frustrated enough that they aliased 'fucking' to 'sudo', so when you use your trick, you get a bit of catharsis:

fucking !!

You just gave me 2 more years back.
Heh, no problem here are 2 more bang tricks. !<charachters> runs the last command you ran that starts with the characters you type.

  !gre      # will run the last command starting with gre (so probably grep)
If you type history, then !<number to the left of the history command>, the shell will execute that command.

  $ history # shows command history
  $ !200    # executes command 200
Keep them coming, imagine the manyears you're recovering!
You can also press Up arrow, Ctrl-A to go back to the beginning of the line, add "sudo " and enter. This type of thing also works in most REPLs (Python, irb, Node, etc.)
Bash/csh have a wonderful history expansion feature[0]. I can never remember all of the modifiers, but I am learning them slowly.

[0]: https://www.gnu.org/software/bash/manual/bashref.html#Event-...