Hacker News new | ask | show | jobs
by brettproctor 3313 days ago
Anyone here will probably enjoy checking out commandlinefu: http://commandlinefu.com

Especially looking down the list of all time greats: http://www.commandlinefu.com/commands/browse/sort-by-votes

2 comments

Thanks for the second link. I go there every few weeks but never thought to check out the top voted.

...In hindsight, of course...

The gem I just plucked out is something I've been curious about for a while but never looked up:

    CTRL-X e
    
    The shell will take what you've written on the command 
    line thus far and paste it into the editor specified by 
    $EDITOR [then run it when saved]
Similar to `fc` except you don't need to run the command before invoking the editor
Also works in vi mode (I think the default for bash is emacs mode - for editing commands, that is):

Run:

set -o vi

once after you log in (for ksh / bash and compatible shells only, maybe, not sure about csh), or (better) put that line in your .bashrc or similar startup file, so it runs each time you log in. (I used to use "ksh -o vi" earlier, before I knew about "set -o vi" or before it existed, but in that case, it has to be the last line in your startup file, otherwise the other lines below will not run until you exit that (sub)shell.)

Then, when typing a command at the command line, just press ESC then v ; it does the same as what you said.

You can also do ESC :q! (in the editor, if it is vi) to quit without running the command you just edited, or save the command to another file for editing later at leisure, then quit without running it right now.

In fact, "set -o vi" also enables limited editing in vi mode right on the command-line, after you press ESC - you can use the command-mode commands of vi (h, l, b, w, f, F, and more) to move around, change characters or words, can also overwrite or append or insert text, etc.

You can even use / and ? and n and N to search backward and forwards in the commmand history to find (by substring) a previous command, to edit it. Once you find the right commmand, just press v.

Great for productivity.

Awesome, thanks for the tip!
Welcome :)
Is there an equivalent to that for urls typed on the command line to be opened using whatever $BROWSER
start http://example.com/

works in Windows.

xdg-open [1] is for Linux.

https://linux.die.net/man/1/xdg-open

Oh wow! Thanks. For macs you can use

  open http://example.com/
Good to know, thanks :)
Bringing this on a small tangent, but I've never been comfortable with 'sudo !!' and I can't really articulate why aside from wanting to be as explicit as possible.

<up>, <ctrl-a>, type sudo and enter is nearly as quick and much more explicit for me.

If you are a long time "vi" user, put `set -o vi` in "~/.bashrc": the sequence to issue the previous command with sudo prepended will be <esc>0isudo<enter>, probably something that is in your muscle memory already.
While I'm a very long time vi user, and I'm completely at home with the keybindings, I could never get into using "set -o vi". For me it falls into the uncanny valley of being quite like vi while not actually being vi.
Both bash and zsh have the option histverify that will expand the !! like so:

> echo "Test"

> sudo !!<enter>

> sudo echo "Test"

You can then modify the command if you want to.

My zsh is set up (don't know if this is a default or not; I'm using Oh My Zsh) to not run the command if you use any history expansions in a command, instead it'll give you a new prompt line with the substitutions already filled in. That way you can check that the command is correct before running it.
Also <ctrl-p> instead of <up> for extra touchtype points.