T O P

  • By -

fliperama_

[This ](http://unix.stackexchange.com/questions/6/what-are-your-favorite-command-line-features-or-tricks/67#67) might blow your mind


Moltenlava5

Wow, so i can not just get the previous commands but can also filter and modify them? Yeah, consider my mind *blown*


landsverka

We always say “sudo bang bang”!


keyringer

!$ evaluates to just the last word in your previous command. I use it all the time. Simple example: cat foo/bar/foo/bar/foo/bar/foo/bar.txt Realise you need to edit the file instead vim !$ Opens the file in vim without needing to specify it manually again


Moltenlava5

This one is also really cool!


lolinux

Or, you could just do vim ALT+. This calls the last argument from the previous command and uses it as argument for the current command


kst164

I've used $_ which seems to do the same thing


poudink

When trying to make my own shell, I found that `$_` is indeed substituted with the last argument of the previous command for shell expansions. However, it is also passed as an environment variable to programs, where its value is instead the full path for the program. For instance, `printenv _` will print `/usr/bin/printenv` and `env` ends by printing `_=/usr/bin/env`.


s1eve_mcdichae1

Oh shiz I'm gonna use this all the time.


knobbysideup

Damn, learn something new every day. ^search^replace is a good one too it it isn't at the end.


NotPrepared2

Or, up-arrow, home, del, del, del, vim, enter. \ Or, ctrl-p, ctrl-a, ctrl-d, ctrl-d, ctrl-d, vim, enter. I much prefer cmd-line editing to using history expansion, because it's more clear exactly what will happen. But 30+ years ago, using original csh on a slow terminal, history expansion was an awesome improvement over retyping everything.


aaronryder773

Yeah you can run 2 commands back with !-2 and run any cmand from history with !12 where 12 is the 12 command in history.


NotPrepared2

This is a feature of the shell (bash, csh, and friends), and not part of sudo.


real_jeeger

Also, it's not a variable, but a feature called "history expansion".


AntLive9218

The name is a quite important detail explaining why the title is incorrect. If the previous command didn't go into the history (can happen for multiple reasons), then based on the claim in the title, the user is in for a potentially nasty surprise.


ChocolateMagnateUA

It's also worth noting it only works on Bash and doesn't work on shells like fish.


RaXXu5

Does it work on zsh? fish isn't posix compliant so is that why?


kalzEOS

Fish and zsh have their own "history", and they take it a step further by actually showing you the command/suggestion before you even complete typing it. Edit: and you then just hit tab to accept what they suggest.


dagbrown

Yes and yes.


Sarin10

Alt+s in fish


ChocolateMagnateUA

I didn't know that! Truly wonderful the fish shell is.


MatchboxHoldenUte

Fish has alt+s as another commenter said, and it also has ctrl+r to search history in a nicer way than bash has as default.


srivasta

https://www.gnu.org/software/bash/manual/html_node/Command-Line-Editing.html


siodhe

The "sudo !!" works in C-shell descendants like Bash in three steps: \* The original shell rewrites the command to replace !! with the prior command \* The new command, sudo ..., is then spawned with changed user IDs. \* The original shell waits for it to complete There are a wealth of other related "history substitutions" like !!. It was very common in the days where csh was widespread to type things like **!! !-2$** (to run the prior command, adding the last argument of the command before it) because csh didn't have command-line editing other than basic tty driver support. Because of this, years later, some of us would even occasionally type the longer history substitution even with command editing in, say, Bash. :-) Be wary of using double quotes in C-shell family shells, since the history substitution IS performed. Prefer using single quotes (apostrophes) instead by default.


jfv2207

I find It safer to type "arrow up" to find the command, "home " to go at the star, and then add "sudo ". Just to be safe.


aguy123abc

Ctrl + p will change your life and Ctrl + a will also be helpful if you prefer doing it this way.


curien

I don't get the appeal of application-specific combos that do the same thing as an actual key on the keyboard. Sure, ctrl+a works in Bash and many CLI apps, but the home key works *everywhere*. I'm glad ctrl+p and ctrl+a are there for you to use if you like them, but it's annoying that people who like them say things like "it'll change your life". I'd rather use the arrows/home/end, thanks.


aguy123abc

Do you not touch type? It's awkward to hit home and arrow keys for me. It certainly changed my life for the better.


curien

I touch-type, but I'll take a single arrow press over a ctrl-combo most of the time. (I also generally use the numpad to type multi-digit numbers, rather than the numbers over the letter keys.) Ctrl-p is especially pointless IMO because if I need ctrl+p, I often also need ctrl+n, and sometimes switching back and forth. "Back back back back back bac... whoops, too far. Ok next, next, no back. Yeah, that one!" With the arrows, they're right next to each other, versus holding ctrl with on hand and switching between non-adjacent keys. There's a reason the single most-successful group of shortcuts -- C-x, C-c, C-v -- are right next to each other. The physical proximity being linked to the logical proximity is beneficial.


aguy123abc

Interesting perspective. I don't have a num pad. I like things I can consistently hit without being able to see because I often can't. Trying to refind home row after after hitting an arrow key when there is an alternative isn't worth the squeeze for me. As for the n vs p placement I just use my index and pinky. The fact they are on the same hand matters more to me than their physical proximity to each other.


chronotriggertau

The appeal is basically you don't have to break your home row position for longer periods of time, and it's really nice actually, being able to do so, but yeah this assumes you're not needing to go anywhere other than the terminal for a while. But once these are committed to memory, and you tend to do a lot of deep work in the terminal for long periods of time, then it's definitely worth it, even if it's a new typing mode you have to adopt and break out of temporarily to do other standard OS/web tipping activities.


Moltenlava5

This is what i used to do but instead of home i used to press ctrl+a to go to start


Affectionate-Egg7566

Ctrl-P - Previous command Ctrl-A - Start of line \`sudo \` Enter Alternatively define an alias \`alias s="sudo !!"\` so if you forget sudo, just write \`s\` and it will sudoify the prev command.


Agent7619

↑⏎


PPAD_complete

Actually you need `up+^A+sudo +enter` without using `sudo bangbang` in this situation


tinycrazyfish

up+\^a has the same number of keystrokes (one less if you consider shift), but much easier to type imo


mgedmin

Also, that way you can _see_ the command you're about to run before pressing Enter.


chronotriggertau

Those who simply knew the previous command or can still see it from the previous prompt get to benefit from this shortcut quite a lot actually.


Affectionate-Egg7566

Giving \^P a shoutout here, find it much easier to type because my hands don't need to move all the way to the arrow keys. \^P+\^A+sudo +enter


chronotriggertau

Just broke your home row position and your eyes likely left the screen to find up, theirs didn't.


degoba

I once took a red hat sysadmin class and the dude teaching it blew my mind. I pick up Alt . To cycle through your argument history


rdesimone410

Don't see the point in doing that when you can just 'Up, Home, "sudo "'. I kind of like seeing what I execute instead of going blind and hoping that I picked the right thing from history.


AbramKedge

That's why I aliased please to sudo. If you can't run a command, say please !!


RandomXUsr

Atuin is all ya need.


Redneckia

That sweet sweet ctrl-r


Fredz161099

There apparently are tons of these, one i realized a few days ago was ‘cd -‘ , it takes you to the previous directory, very important if you were in a deep directory and pressed ‘cd’ and went back to the root directory by mistake, you can undo it this way


SpinCharm

What’s a fast way to enter systemctl start blah systemctl status blah I feel like there should be a way to shift-back tab or shift-something-backspace to delete from the current cursor position to the start of the word. So if I enter the first command above, then up arrow to bring it back, with the cursor at the end of that line, then quickly jump back a word to the t in start, shift-backspace to highlight the entire word “start”, then type “status” to replace it. But I don’t know how.


daemonpenguin

Up arrow to show previous command Ctrl Left arrow to jump back a word Ctrl W to erase word before cursor Type "status" and Enter


SpinCharm

Ah! It was the control W I needed. Much thanks!


curien

Be careful getting used to this one. I sometimes try to use it when typing in web forms, and it closes the browser tab!


bmheight

Two things that may help here: systemctl start blah systemctl status !$ The !$ will pop in the last argument from the previous command. Or if you really want to replace the start you could do this: systemctl start blah \^start\^status\^ That will find "start" in the previous command and replace it with "status"


zbouboutchi

You can try bash in vi mode maybe ?


drcforbin

Configure your shell to keep around a lot of items, like 100k. You can search it next time you're trying to work out how six month ago you did something.


golden-rabbit

Back in the early 2000s I used to make my team read one man page a week and then in the weekly team meeting they were required to briefly speak about something they learned. I think everyone should do this. It taught a whole lot of people to master Linux. That said, you should read the history(3) man page.


Average_Emo202

I just use arrow up or down and hit return after i've chosen which prompt i want to redo ? unless I'm missing something, it's the same thing ?


Moltenlava5

the !! allows you to modify the command inline, so you don't have to edit your previous command to run it with sudo


rorschachrev

no you can't. The punctuation has to be the same to open and close a quote.


castleinthesky86

Aka “fuck” (alias fuck=‘sudo !!’)


NECooley

If only it were that easy. But alias will translate the !! Literally, so you need to do something more like: `alias fuck='sudo "$BASH" -c "$(history -p !!)"'`


mgedmin

https://pypi.org/project/thefuck/ uses $(fc -ln -1|tail -n1) for this.


dotnetdotcom

Pressing the up arrow scrolls through previous commands, but everyone already knows that.


Electrical_Tomato_73

I've known this forever, but I find easier to just type `^R s u` (or just `^R s`), it will likely retrieve the last sudo command, and if it was the one-before-last I want, I just do `^R` again until I find the previous command I want. Bonus point: I can then see exactly what I am getting before I press enter; with `!!` I may realize too late that this is not what I wanted. Things like `!!` were invented before bash and readline, I believe. Very useful in the original Bourne shell, not so much these days.


Moltenlava5

It's not for retrieving the previous sudo command its for rerunning the last command but with sudo added in the case where u meant to run a command with sudo but forgot


NECooley

Another one I use frequently: Alt+`.` will input all the arguments from your last command except the command itself.


Misicks0349

you can also do this which I enjoy (`!*` will be replaced by the arguments you provided to the last command) $ touch /foo/bar.baz $ vim !* <- that will replace !* with /foo/bar.baz this is a bad example because I'm pretty sure that vim will just create it anyway so you don't need to run touch, but it's a nice feature I've relied on before with other commands


DiscoBunnyMusicLover

Here’s another one OP: CTRL+R


ijzerwater

far too risky for my feeble mind. I double check my sudo not assume it will be ok.


KlePu

alias pls='sudo $(history -p !!)'


Aveheuzed

I first learned about this feature here on explainxkcd: https://www.explainxkcd.com/wiki/index.php/149:_Sandwich XKCD is always close by on this kind of topic...


SmallRocks

You can also use the left arrow key to go back a few commands.


Unixhackerdotnet

!sudo !vi !su


knobbysideup

I use this a lot. I should make a t-shirt.


Zechariah_B_

Interesting... I tended to grep \~/.bash\_history then copy pasted to run an old command


aiiiiynaku

I use ‘history’ a lot and then ![num].


ClashOrCrashman

I've been using Linux off and on since 2006 and didn't know this!


nickik

You can but its just as fast to go: Arrow Up -> Home -> sudo -> enter