Impress your next date with these neat bash tricks(*)(**)

I use bash with shopt -s histverify on, meaning the command i am pulling out of history won’t be immediately executed. Instead it will be displayed for validation. saved my ass a couple of times when the command i pulled out was definitely not the one i wanted.

I am a big shell history user. i never type a command twice. To optimize history use, i seldom move around filesystem and try to always use absolute paths, so the commands stored in history are always valid, not matter my position in filesystem.

๐Ÿ“ control+r “searchterm” will start displaying the most recent command containing what you’are actually typing. Way better than spamming the โฌ†ย 

๐Ÿ“ !$ – my personal game changer shortcut. Easily accessible on azerty keyboard. Will recall the previous command’s last argument:

$ mkdir /tmp/this/is/a/very/long/name/for/a/directory
$ vim !$/Readme.md # edit Readme.md file in previous command’s latest argument, ie the path of newly created directory

๐Ÿ“ ^typo^correction will recall previous command, replacing “typo” with “correction”

$ nano /etc/fstab #just kidding
$ ^nano^vim # replace “nano” by “vim” in previous command
vim /etc/fstab

If you want to make multiple replace and not only the first occurence:

$ cp file1 /path/to/dir1/copy1
$ !:gs/1/2/ # replace all “1” by “2” in previous command
cp file2 /path/to/dir2/copy2

๐Ÿ“ !!:X : We can suffix !! with:X where X is the number (starting at 0) of each argument of the previous command

$ seldomUSedLongCommand -l /tmp/test
newCommand: -l : unknown option
$ man !!:0 # get manpage of 0’th argument of previous commandline, ie the command iteslf
man seldomUSedLongCommand

๐Ÿ“!!:h – will strip filepath lastest element ( like dirname )

$ less ~/Documents/Dokuwiki/data/pages/maison/running.md
$ ls -lrt !$:h # get previous’s command full path without latest element
ls -lrt ~/Documents/Dokuwiki/data/pages/maison/

(*) Or make them run away, whatever.

(**) Will enlarge your console productivity


Posted

in

by

Tags: