Live data from Hacker News

Bash Shortcuts For Maximum Productivity

skorks.com

21–30 of 58 posts

Re: Bash Shortcuts For Maximum Productivity

#21
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…

I used to use !$ all the time, but these days I tend to go for M-. which inserts the last argument interactively, and cycles through the last argument of all previous commands (but you don't get modifiers like !$:h).

http://www.gnu.org/s/bash/manual/html_node/Commands-For-Hist...

M-C-y (yank-nth-arg) works similarly for the first argument of the previous command, but without cycling through all previous commands.

After the more complex modifiers like !$:p, :e, :s/old/new/, etc, before hitting Return I often use M-C-e (shell-expand-line) just to check I got it right.

http://www.gnu.org/s/bash/manual/html_node/Miscellaneous-Com...

Re: Bash Shortcuts For Maximum Productivity

#22
I'm a zsh user, but here's a couple of things I find pretty invaluable:-

    bindkey '^Q' push-line
The 'push-line' widget allows you to quickly type another command and will restore the previous one once you've entered it.

    # re-run the previous command with sudo
    rerun-with-sudo () {
      LBUFFER="sudo !!"
      zle accept-line
    }
    zle -N rerun-with-sudo
    bindkey '^Xx' rerun-with-sudo
Re-run the previous command prefixed with sudo by typing ^X then x.

And:-

    # build git clone command from clipboard
    git-clone-clipboard() {
        REPO_URL="$($CLIPBOARD)"
        REPO_BASE_NAME=${$(basename $REPO_URL)%.git}
        LBUFFER="nocorrect git clone $REPO_URL $REPO_BASE_NAME"
    }
    zle -N git-clone-clipboard
    bindkey "^Xg" git-clone-clipboard
^Xg prefixes a git url with "git clone" and fills out the command line.

Couple similar things for wget, youtube-dl here: https://github.com/gaving/dotfiles/blob/master/.zsh/config

Re: Bash Shortcuts For Maximum Productivity

#23
post #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.

I think with the parent's setup you can do ver to get the same effect.

Re: Bash Shortcuts For Maximum Productivity

#24

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.

There's no need for seq or jot. In bash you can replace `seq 1 10` with {1..10}. Or {01..10} if you want zero padding when using $i to label files.

I use an function called "up" that takes an optional integer argument to go up a certain number of levels. It won't necessarily be faster, but somehow I prefer it to typing lots of dots in a row:

   function up () { if test $# = 1 ; then s=$( printf "%$1s" ); s=${s// /..\/}; cd $s ; else cd .. ; fi; }

Re: Bash Shortcuts For Maximum Productivity

#25
post #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?

I do too.

I think it's faster than a cycle of :p, verify it's what you want and then reexec. It's also mentally less taxing (albeit a tiny amount). But summed over a day of shell work, I think it makes a difference.

Re: Bash Shortcuts For Maximum Productivity

#26
post #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

Nice little timesaver for such a common task, I'd just been doing the following (with vi editing on) but yours is much easier:

  esc-0-i-sudo

Re: Bash Shortcuts For Maximum Productivity

#27
post #19

Earlier quoted context omitted.

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

Nice little timesaver for such a common task, I'd just been doing the following (with vi editing on) but yours is much easier: esc-0-i-sudo

It may not be less keystrokes, but for what it's worth, I find using shift+i is easier than 0-i

Re: Bash Shortcuts For Maximum Productivity

#28
post #22

I'm a zsh user, but here's a couple of things I find pretty invaluable:- bindkey '^Q' push-line The 'push-line' widget allows you to quickly type another command and will restore the previous one once you've entered it. # re-run the previous command with sudo rerun-with-sudo () { LBUFFER="sudo !!" zle accept-line } zle -N rerun-with-sudo bindkey '^Xx' rerun-with-sudo Re-run the previous command prefixed with sudo by…

If I have a URL in my clipboard and want to prefix it I use:

    git clone `xclip -o`
Much simpler and no customization needed.

Re: Bash Shortcuts For Maximum Productivity

#29

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…

I use ctrl-P/N for history-beginning-search-{backward,forward} in Zsh. Type the start of a previous command and cycle through the matches.

Re: Bash Shortcuts For Maximum Productivity

#30
post #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.

For Zsh, setopt autopushd
Post reply on HN