[sf-lug] My favorite simple little Linux tips

Akkana Peck akkana at shallowsky.com
Fri Jul 30 21:05:39 PDT 2010


As long as we're talking terminal editing shortcuts, my favorite
is Control-W to erase the last word. It's even better if you set
it to erase only back to the last punctuation -- that way, if
you do something like
$ ls /long/path/to/somewhere/that/you/dont/want/to/type/again
and then realize you had one too many, you can uparrow (or Ctrl-P)
and hit a Ctrl-W or two to delete elements of the path one by one.

That used to be the default in old-timey Unix, but in modern Linux
you have to set it that way: in .bashrc,
bind '\C-w:backward-kill-word'
and in .inputrc, 
set bind-tty-special-chars Off

And that reminded me of another favorite I use a lot: curly brace
and comma substitutions. For instance, if I want to make a
backup copy of a file with a long name, I can say:

$ cp ~/long/path/to/long-file-name{,-bak}.jpg

which expands to

$ cp ~/long/path/to/long-file-name.jpg ~/long/path/to/long-file-name-bak.jpg

without needing to go to the mouse to doubleclick/middleclick.

Mike Higashi writes:
> The one I'm in the habit of using instead is the !$ shortcut:

Don't forget !^ for the first argument, too. Fun idiom:

$ mv filename filename-sav
  [ do something outside this terminal window ]
$ mv !$ !^

Steve M Bibayoff writes:
> And for all you people who don't use emacs (I'm looking at you Michael
[ ... ]
> Most of bash shortcuts:

Warning: once you get addicted to these emacs-inspired shortcuts,
you'll spend a lot of time swearing at developers of apps that don't
support these once-standard keybindings that make typing so much easier.
Forget about word processors -- none of them support these bindings.

Alex Kleider writes:
> $ ^ls^less
> 
> It's similar to 
> :s/old_string/new_string
> when using vim

And you can use that syntax too:

$ !!:s/ls/less

And with this syntax, you aren't limited to the last command:

$ !x:s/foo/bar

repeats the last command you ran starting with x, replacing the
first foo with bar.

$ !-4:gs/foo/bar

runs the fourth-to-last command, replacing every foo with bar
(gs means global substitute).

	...Akkana




More information about the sf-lug mailing list