Live data from Hacker News

Bash Shortcuts For Maximum Productivity

skorks.com

11–20 of 58 posts

Re: Bash Shortcuts For Maximum Productivity

#11
post #8

The things he calls "bang commands" are actually "event designators" (!!, !blah), "word designators" ($) and "word modifiers" (:p). There are links to docs for each at the bottom of this page: http://www.gnu.org/s/bash/manual/html_node/History-Interacti... I use them all the time, and anyone watching over my shoulder always asks what that was. Learn !!, !$, and a few modifiers. You'll be glad you did. Personally, I u…

A neat thing about csh/tcsh is that you can use history expansion with aliases. There are some legacy aliases at work that allow SQL queries from the command line using this:

  $ alias select='sqlcommand -s "select !*"'
would translate:

  $ select * from tablename
to:

  $ sqlcommand -s "select * from tablename"
(Note: I may be getting the specifics messed up as it's been a couple of years since I looked at the implementation, but the general idea is there.)

Re: Bash Shortcuts For Maximum Productivity

#12

One of my favorite bash shortcuts is binding the up arrow to history-search-backward. This is the first page I found that concisely explains how; the variant in the second comment is the one actually in my .bashrc: http://hints.macworld.com/article.php?story=2003102617423686... So if you've typed `verylongcommand` some indeterminate time in the past, you can probably just type "ver ", and you'll have it. Very conveni…

A quick look at the article shows it's binding history search. It's already bind to ctrl-r - you can do a ctrl-r, type a few letters of the long commands, and keep doing a ctrl-r till you find the right match.

Apart from additional bind your setup requires, arrow keys are near impossible to touch type. Even if you can touch type them, that means taking your hands off the home row, and positioning them again.

Re: Bash Shortcuts For Maximum Productivity

#13
post #9
post #7

I'm not an Emacs user, but aren't a lot of the text-editing shortcuts straight from Emacs?

Yup, but you can also put Bash in a vi mode. Some else will to chip in with the exact incantation, since I'm on my phone now.

  set -o emacs
or

  set -o vi

Re: Bash Shortcuts For Maximum Productivity

#14
post #7

I'm not an Emacs user, but aren't a lot of the text-editing shortcuts straight from Emacs?

The text editing shortcuts are straight from readline http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html emacs bindings, which bash and many other programs use. readline supports both vi and emacs bindings.

I am a dyed in the wool vim enthusiast, but when it comes to command line bindings, emacs' bindings are more convenient.

Re: Bash Shortcuts For Maximum Productivity

#15

Great tips. !! is a nice timesaver when you've forgotten to prepend sudo to a command: $ /etc/init.d/rabbitmq-server restart Failed, need root access $ sudo !! Success cd - is also quite cool, it takes you to the previous working directory. $ pwd /some/really/long/path/goes/here $ cd /var/log/app Work in this directory for a while, then go to previous dir $ cd - $ pwd /some/really/long/path/goes/here

Don't forget pushd and popd.

Re: Bash Shortcuts For Maximum Productivity

#16
I use `..` to go upper directory, `...` to go up two levels, I added this to bashrc:

  str='..'
  level='./../'
  for i in `seq 1 10`;
  do
      eval "alias '$str=cd $level'"
      level=$level'../'
    str=$str'.'
  done
Note, seq is not available in Mac, should use jot instead.

Re: Bash Shortcuts For Maximum Productivity

#17
It's well known that !! is a sweet little helper in case where you forgot to sudo a command. However, I find that for those cases I usually use `Up-arrow CTRL-A` to get the previous command and then go to the beginning of the line where I can type `sudo`. Is there any reason to prefer the !! variant?

Re: Bash Shortcuts For Maximum Productivity

#18
commandline and vim have easily got the most powerful, yet the most rage-inducing key mappings – ever.

The worst, and most easily rectifiable aspect is the way command pairs (eg move back/forward a word) require two totally separate commands (esc + b/esc + f) instead of having one command + a generic 'reverse' meta key.

In the future I hope someone has the balls to do away with this legacy tripe and popularise some more Donald-Normanesque keyboard shortcuts.

Re: Bash Shortcuts For Maximum Productivity

#19

Great tips. !! is a nice timesaver when you've forgotten to prepend sudo to a command: $ /etc/init.d/rabbitmq-server restart Failed, need root access $ sudo !! Success cd - is also quite cool, it takes you to the previous working directory. $ pwd /some/really/long/path/goes/here $ cd /var/log/app Work in this directory for a while, then go to previous dir $ cd - $ pwd /some/really/long/path/goes/here

I have the following in my zshrc for sudo. It adds sudo at the beginning of the current line or writes sudo !! if the current line is empty. I've aliased it to alt+s

    run-with-sudo() {
      if [[ -z $LBUFFER ]]; then
        LBUFFER="sudo !!";
      else
        LBUFFER="sudo $LBUFFER";
      fi
    };
    zle -N run-with-sudo;
    bindkey '\es' run-with-sudo

Re: Bash Shortcuts For Maximum Productivity

#20

A note to Mac users: Instead of Alt, you must press escape key. For example, Esc+b moves the cursor backward one word in bash, but you'll probably want to use option instead, so check 'Use option as meta key' in Terminal's settings (under keyboard tab). But if it's not very handy, you can set 'option+left arrow' to move cursor one word backward. To do that, open Terminal's settings, go to keyboard and add a shortcut…

Thanks very, very much. I'm embarrassed to admit I probably spend 3-5 hours a day on a command line, and have just gotten used to set -o vi when I need to bounce around long lines a lot. esc+f/esc+b were always awkward. It should have occurred to me to just map the keys on Terminal.app!

In the interesting news department - Lion already has option left cursor/right cursor already mapped to move a word forward and back - not sure if it was there in snow leopard.

Post reply on HN