Bash tips
October 1st, 2006
Bash has some useful shortcuts that most of users are unaware of. Here I’m describing some of them that I found very very useful:
- Screen clear: are you tired of typing
clearall the times you want to get your screen cleared? Hint: pressctrl + L. - Reverse search: sometimes could happen that you have to retype some command you previously typed. So why don’t look for the command in the history? Press
ctrl + Rand begin typing the command. Bash should autocomplete it! - Command substitution: if you wrote your command and you typed a wrong letter, why to rewrite all the command? Just substitute the mistaken word. How? Use
^texttosobstitute^sobstitution. For example, if you typedapt-get updatke, you can fix it by typing as next command^updatke^update(or simpler:^tk^t). - Latest action: do you want to repeat the last command? Just use
!!. It will (re)execute the latest command you given. - Latest parameter: and if you want to use once again the latest parameter you typed in the last command?
!$is made for you… For example, let suppose you renamed film.avi in my_new_divx.avi withmv film.avi my_new_divx.avi. If you want to see it you can just typemplayer !$. That’s all (and there’s!*that refers to all the argument passed in the previous command…).
I use these shortcuts from the time I’ve discovered them and I found them very useful (especially the number 1!). So, why don’t share such things with the other guy over here?
I really enjoy bash tips, though I find it impossible to remember many. I was glad to see the ctrl R, as I hadn’t seen it. As to your use of !$ to refer to the last parameter, I find Alt + as fast or faster though, and searches I often find ! will re-run the last use of command occasionally useful, but because you can’t edit it, that one is problematic.
Thanks for the tips!
Comment by Oldman — October 2nd, 2006 at 0:55
CTRL + L
I use that every day !
move to the start of the current line
CTRL + a
to the end
CTRL + e
$exit alias….
kill -1 $$
Comment by texilee — October 2nd, 2006 at 12:08