
Shortcuts to Move Faster in Bash Command Line - teohm
http://teohm.github.com/blog/2012/01/04/shortcuts-to-move-faster-in-bash-command-line/
======
jashmenn
One of my most life-changing bash configurations is having history-search-
backward / history-search-forward bound to the up and down arrows.

Put the following in your ~/.inputrc

    
    
      "\e[A": history-search-backward
      "\e[B": history-search-forward
      "\e[C": forward-char
      "\e[D": backward-char
      set show-all-if-ambiguous on
      set completion-ignore-case on 
    

Now if you type, say:

    
    
      cat <UP ARROW>
    

You will cycle through commands in your history that start with the prefix
"cat ".

~~~
digisign
I use this with the fish shell, and it seems to do a better job of searching
than the ^R in bash. Not sure of the difference, but I think bash only
searches from the front of the string.

------
julian37
This is a description of the Emacs bindings, which are the default. Vi
bindings are also available and can be enabled by adding

    
    
        set -o vi
    

to ~/.bashrc.

Peteris Krumins also has an article on bash command line editing with some
extra information on completion:

[http://www.catonmat.net/blog/bash-emacs-editing-mode-
cheat-s...](http://www.catonmat.net/blog/bash-emacs-editing-mode-cheat-sheet/)

~~~
alxndr
Beat me to it. (My first thought upon seeing the headline was, "wow, faster
than vi mode?")

I haven't actually put the set into my .bashrc yet, I just end up typing it
when I realize I'm about to navigate a lot.

~~~
chimeracoder
Semi-related, but what's the fastest vi way to get to the space right _after_
the first word? For example, I might type

> ls /etc/foo/bar

and have my cursor at the end when I realize I want to type ls -l instead. I
know there's got to be a faster way than what I'm currently doing, but I'm
blanking.

(Yes, I know sometimes tacking on the command line switch at the end will
work, but that's not true for all commands).

~~~
wumpushunter
Shift-T (the opposite of up`t`o) and then `s`.

You can hit `;` if there are multiple incidences of 's'.

------
lelf
Bash is out of place here actually, they are just general readline's and work
in many places

<http://cnswww.cns.cwru.edu/php/chet/readline/readline.html>

------
emillon
My favorite tip is to stay in emacs mode, but whenever you have something
"complicated" to do, use Ctrl-X Ctrl-E : it opens the current line in $EDITOR.

~~~
Evbn
This is amazing. One of my persistent nuisances had been when and how to
transition from command line to script. This makes it smooth!

~~~
mhitza
Except all the line formatting gets lost in bash after executing the command;
and it is automatically executed.

In zsh you still have the opportunity to inspect it once more when you leave
the editor, and it maintains it's multiline format in the history.

------
res0nat0r
Also install the awesome autojump: <https://github.com/joelthelion/autojump>

If I was previously sitting in directory:

' _/home/user/code/github.com/cookbooks/cassandra/recipes_ '

and I wanted to jump back to that dir from anywhere on the file system I could
just type ' _j cass rec_ ' and it would autocomplete and jump to this
location. Super helpful.

~~~
sneak
"cd -"

~~~
res0nat0r
This is much more sophisticated than just jumping back to your previous
directory. It keeps a weighted history of dir's you cd into most and uses
those first for autocompletion etc.

~~~
sneak
I know. "cd -" is available on every system I use, though.

------
telemachos
Worth a mention is that you can customize how Readline handles these sorts of
shortcuts in your _~/.inputrc_ file. For example, I use forward and backward
_by word_ far more often than _by character_ , so I do this:

    
    
        $if Bash
            # various other stuff left out
            # easier back and forth by word
            "\C-b": backward-word
            "\eb": backward-char
            "\C-f": forward-word
            "\ef": forward-char
        $endif
    

For more examples and syntax, see
[http://docs.freebsd.org/info/readline/readline.info.Sample_I...](http://docs.freebsd.org/info/readline/readline.info.Sample_Init_File.html)
and
[http://docs.freebsd.org/info/readline/readline.info.Readline...](http://docs.freebsd.org/info/readline/readline.info.Readline_Init_File.html).

------
pieter
Note that you don't need to 'enable' the Meta key in Terminal on OS X.

By default, option does what it does in other programs, allowing you to type
alternative characters. If you'd still like to keep that behaviour, you can
send meta by pressing escape first -- Meta-b becomes escape, then b.

------
Scaevolus
$_ and !$ are additional ways to get the last argument of the previous
command.

<http://samrowe.com/wordpress/advancing-in-the-bash-shell/> is a good list of
extra tips

------
vlandham
Very similar list here:

[http://www.skorks.com/2009/09/bash-shortcuts-for-maximum-
pro...](http://www.skorks.com/2009/09/bash-shortcuts-for-maximum-
productivity/)

which includes some other nice shortcuts - like ctrl + xx

------
jameswyse
It's also worth noting that in OSX many of these shortcuts work anywhere you
can enter text. I use ctrl+a/ctrl+e all the time.

------
darkstalker
What's wrong with using the arrow keys?

~~~
Adrock
It amounts to significantly less hand and finger movement.

~~~
csmatt
Exactly. That's why I'm such a fan of emacs. The closer I stay to the home-
row, the better.

Also, 'Ctrl + p' and 'Ctrl + n' will navigate through your command history. I
do love 'Ctrl + r' for searching through it, too.

------
username3
Is there a redo to go with undo?

