Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I've been using "sudo !!" (which I pronounce as sudo bangbang in my head) for a while now, mostly with vim for editing stuff in /etc. It's very nifty.

Another nifty tool is vi editing mode. You run "set -o vi", and now your shell takes vi modal editing commands. That along with Ctrl-r for reverse history search has made life in the shell so much easier. Bash has a ton of little stuff like that built in. Zsh has a few more.



Probably my biggest overall timesaver has been:

   :w !sudo tee %
If you neglected to sudo before making your changes.


I've had this in my vimrc for years:

   " Let :w!! gain sudo privileges without closing and reopening vim
   cmap w!! w !sudo tee % >/dev/null


Its much more easier to remember when you know how that actually works. Here is link explaining how that actually works:

http://stackoverflow.com/questions/2600783/how-does-the-vim-...


Yes, that vi editing mode of bash is very useful. IIRC it was there in ksh too. Some people might not know that you can use both the insert/append mode and the command mode of vi(m) in that vi editing mode of bash, to edit previously entered commands. You just switch between the two modes the same way as you do in the real vi(m), with ESC to go from insert/append mode to command mode, and i or a to go from command mode to insert/append mode.

Also, after you have entered a previous command, to edit it, or any previous command in the history, the first thing you have to do is press ESC (to go to command mode) and then k, thereby bring the last entered command into view for editing.

When editing a command, but in command mode (not insert/append mode), you can also press v to open the command in vi(m) in a temporary file, then have the full screen editor at your disposal to edit the command (useful for long scripts including for/while loops or long pipelines), and then when you save and exit (:wq or :x followed by Enter), the newly edited command gets executed by bash.

Also, this editing technique is useful not only for correcting typos in commands, but for modifying a previous correct command, to change some words in it and then re-issue it.


I also use that all the time, and pronounce it as "sudo", but with more excitement.


sudo !! is my favorite thing. I named my cat sudo bangbang.


I don't see the advantage over pressing up, C-a and typing sudo


Historically: it predates the ability to do interactive line-editing.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: