Live data from Hacker News

Bash Shortcuts For Maximum Productivity

skorks.com

31–40 of 58 posts

Re: Bash Shortcuts For Maximum Productivity

#31
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?

If you're on a non-interactive terminal. Some of us didn't throw out our teletypes. Also: when rooting boxes often one lacks space in the exploit payload to set up the tty line discipline.

Re: Bash Shortcuts For Maximum Productivity

#33
I am a huge fan of maximizing productivity in the shell. I wrote these three articles a while ago:

Bash emacs-like keyboard shortcuts:

http://www.catonmat.net/blog/bash-emacs-editing-mode-cheat-s...

Bash vi-like keyboard shortcuts:

http://www.catonmat.net/blog/bash-vi-editing-mode-cheat-shee...

Definitive bash guide to command line history:

http://www.catonmat.net/blog/the-definitive-guide-to-bash-co...

And they all come with cheat sheets. See downloads box at the bottom of each post for download links.

Re: Bash Shortcuts For Maximum Productivity

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

Thank you! The backup tip was great. I never thought of using expansion like that...

You post inspired me to quickly create two (hopefully) handy bash functions:

  # Create a backup of the given file(s)
  # USAGE: bk file1 file2 file3
  #        -> result: file1.bak file2.bak file3.bak
  bk() {
  for file in "$@"
    do
      cp $file{,.bak}
    done
  }

  # Restores the file from backup
  # USAGE: rbk file.bak OR rbk file
  #        -> result: replaces file with file.bak
  rbk() {
    if [[ $1 == *.bak ]]
    then
      backup_file=$1
      old_file=${backup_file:0:$((${#backup_file}-4))}
    else
      old_file=$1
      backup_file=${old_file}".bak"
    fi

    read -p "Replace the old backup? (y/N) " -n 2
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
      `cp $backup_file $old_file`
    fi
  }
I've put them in my newly created .functions file (https://github.com/pooriaazimi/dotfiles/blob/master/.functio...).

Re: Bash Shortcuts For Maximum Productivity

#35
Sometimes I find myself typing out a long command and then deciding that I don't want to run it quite yet. Normally I just hit C-c run some other command first and then copy paste the long command. Surely there is someway to put the long command in history?

Re: Bash Shortcuts For Maximum Productivity

#36
post #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 m…

I'm not sure if you're just trolling, but I don't understand your complaints at all. What would your "Donald-Normanesque keyboard shortcuts" even look like? In Windows the arrow keys act pretty similar, don't they? left-arrow, right-arrow for moving by character, Ctrl-left-arrow, Ctrl-right-arrow for moving by word. Windows is about as Donald-Normanesque as you can get...

The bash shortcuts are from readline, which (in this case, but by default?) are the same as Emacs. C-f,C-b for moving by character, M-f,M-b for moving by word. C-n,C-p for next and previous line. I think there's a vi switch for readline which would allow you to have vi style shortcuts in bash. This is nice, because it means you have one set of keyboard shortcuts in your editor and shell. Emacs users in OS X get this in any text box (at least any that supports the same defaults as the system, I'm spitting at you MS Outlook/Entourage). I'm sure you could set something up in other windows systems, like GNOME or KDE.

Sure the keyboard shortcuts might be cryptic, but any keyboard shortcuts will be. Don't slag something just because you don't know the history and background behind the choices.

Re: Bash Shortcuts For Maximum Productivity

#37

Sometimes I find myself typing out a long command and then deciding that I don't want to run it quite yet. Normally I just hit C-c run some other command first and then copy paste the long command. Surely there is someway to put the long command in history?

Comment it out (put a # at the front), unless your shell is set to ignore commented lines.

Re: Bash Shortcuts For Maximum Productivity

#39

Sometimes I find myself typing out a long command and then deciding that I don't want to run it quite yet. Normally I just hit C-c run some other command first and then copy paste the long command. Surely there is someway to put the long command in history?

Comment it out (put a # at the front), unless your shell is set to ignore commented lines.

This works great! There is even a shortcut: M-#

Re: Bash Shortcuts For Maximum Productivity

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

!! and !$ are alright, but I really dislike !blah, which is blindly executing the latest command starting with blah, whatever it is. The author of the article should remove this "tip", it is bad advice, or at least make a note that the !blah:p is highly preferable.
Post reply on HN