Shell Tricks That Make Life Easier (and Save Your Sanity)

140 pointsposted 11 hours ago
by zdw

65 Comments

teh

2 minutes ago

Another useful "Emergency exit" is CTRL+Z which stops the process and cannot be intercepted.

It's often faster than hitting CTRL+C and waiting for process cleanup, especially when many resources are used. Then you can do e.g. `kill -9 $(jobs -p)` to kill the stopped tasks.

fzeindl

7 minutes ago

My header on top of every script

            #!/usr/bin/env bash
            set -eEuo pipefail
            # shellcheck disable=SC2034
            DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
            #######################################################

ahmedfromtunis

2 hours ago

Using the terminal becomes much more cozy and comfortable after I activate vim-mode.

A mistake 3 words earlier? No problem: <esc>3bcw and I'm good to go.

Want to delete the whole thing? Even easier: <esc>cc

I can even use <esc>v to open the command inside a fully-fledged (neo)vim instance for more complex rework.

If you use (neo)vim already, this is the best way to go as there are no new shortcuts to learn and memorize.

penguin_booze

8 minutes ago

> <esc>cc

Doing control+o in insert mode temporarily places you into normal mode so that you can execute one normal-mode command, and then go back to insert mode again--no need to hit 'i' again.

So, instead of '<esc>cc', '<c-o>S'.

piekvorst

21 minutes ago

This reminds me of an excerpt from an old Emacs manual:

    . . . if you forget which commands deal with windows, just type @b[ESC-?]@t[window]@b[ESC].
This weird command is presented with such a benevolent innocence as if it's the simplest thing in the world.

I think the better advice for command-line editing would be to set up the mouse.

commandersaki

2 hours ago

I've been a (n)vim user for 20+ years now, but I hate vi-mode in the shell. However if I feel that I need to do a complex command, I just do ctrl-x+e to open up in neovim (with EDITOR=nvim set). I find it a good middle ground.

sudonem

an hour ago

Agree.

I WANT to love it - and if I was only ever working on one, or a small number of systems that I was the only one working on I’d probably do it. I’m ALL about customizing my environment.

However ssh into various servers through the day (some of which are totally ephemeral), and having to code switch my brain back and forth between vim mode and emacs mode in the shell would just slow me down and be infuriating each time I connect to a new box.

exceptione

an hour ago

   <esc>3bcw
What is your keyboard layout? This looks like a crime against humanity on a regular qwerty kb.

sva_

an hour ago

Instead of esc, type ctrl [

exceptione

34 minutes ago

Does it help a lot? You've still got a three to type which is a crime, plus some letters, only to move 3 words. My typing skills are not great, but that sounds like an awful lot of work(?)

If I hit CTRL + ARROW_LEFT 3 times, I am done a lot faster I guess. But I am open to learn, do people really use that and achieve the goal significantly faster?

gsinclair

22 minutes ago

I don’t love vi-mode, but I’ll address your comment.

Many people these days, including yours truly, have caps-lock mapped to ctrl if held or esc if tapped. That’s good ergonomics and worth considering for any tech-savvy person.

Instead of the 3b I would type bbb (because I agree with you that typing numerals is a pain).

So (caps lock)bbbcw isn’t bad. It’s better than it looks, because if you’re a vim user then it’s just so automatic. “cw” feels like one atomic thing, not two keypresses.

And importantly, it doesn’t involve any chords.

rzmmm

2 hours ago

Oh wow I didn't know about this, thank you. The underlying feature is called "readline vi-mode" for folks who want to search more about it.

tkocmathla

3 hours ago

I love this, from a comment on the article:

  He had in his path a script called `\#` that he used to comment out pipe elements like `mycmd1 | \# mycmd2 | mycmd3`. This was how the script was written:
 
  ```
  #!/bin/sh
  cat
  ```

rgrau

2 hours ago

A similar trick:

    #!/bin/sh
    $*
that's my `~/bin/noglob` file, so when I call a zsh script from bash that uses `noglob`, it doesn't blow up.

internet_points

2 hours ago

Yes! That one's going in my $PATH. Such a useful use of cat!

000ooo000

an hour ago

What does it provide over

mycmd1 #| mycmd2

chriswarbo

16 minutes ago

Theirs "turns off" one element of a pipeline; yours turns off everything after a certain point.

This will output the stdout of mycmd1:

    mycmd1 #| mycmd2 | mycmd3
This will output the stdout of mycmd3:

    mycmd1 | \# mycmd2 | mycmd3

fellerts

3 hours ago

CTRL + W usually deletes everything until the previous whitespace, so it would delete the whole '/var/log/nginx/' string in OP's example. Alt + backspace usually deletes until it encounters a non-alphanumeric character.

Be careful working CTRL + W into muscle memory though, I've lost count of how many browser tabs I've closed by accident...

hejira

3 hours ago

In my terminal it's the exact opposite – Alt-Backspace deletes to the previous space, whereas Ctrl-W deletes to the last non-alphanumeric (such as /). I'm using fish shell in an Alacritty terminal.

Yeah, pressing Ctrl-W accidentially is a pain sometimes ... but Ctrl-Shift-T in Firefox is a godsend.

Aerolfos

2 hours ago

> Yeah, pressing Ctrl-W accidentially is a pain sometimes ... but Ctrl-Shift-T in Firefox is a godsend.

Fun fact: despite having absolutely no menu entry for it, and I believe not even a command available with Ctrl+Shift+P, Vscode supports Ctrl+Shift+T to re-open a closed tab. Discovered out of pure muscle memory.

dgrunwald

14 minutes ago

It's a normal command called "View: Reopen Closed Editor".

gryfft

3 hours ago

Ctrl-Shift-T usually brings that tab right back at least

figmert

2 hours ago

> Be careful working CTRL + W into muscle memory though, I've lost count of how many browser tabs I've closed by accident...

This hurts.

Also, for the shell, if you do C+w, you can "paste" it back using C+y. Assuming you have not removed that configuration.

zahlman

3 hours ago

Not a fan of the LLM-flavoured headings, and the tips seem like a real mixed bag (and it'd be nice to give credit specifically to the readline library where appropriate as opposed to the shell), but there are definitely a few things in here I'll have to play around with.

One thing I dislike about brace expansions is that they don't play nicely with tab completion. I'd rather have easy ways to e.g. duplicate the last token (including escaped/quoted spaces), and delete a filename suffix. And, while I'm on that topic, expand variables and `~` immediately (instead of after pressing enter).

talkin

3 hours ago

> cd -: The classic channel-flipper. Perfect for toggling back and forth.

And not only cd. Gotta love 'git checkout -'

piekvorst

an hour ago

The '-' shortcut is weird. In 'git commit -F -', the '-' is actually /dev/stdin.

keybored

an hour ago

The `-` is specially handled (hard coded) for a few commands only when it comes to “the last branch” DWIM.

voidUpdate

3 hours ago

With ctrl+r, if you press it twice, it will autofill the search with whatever you last searched for. pressing it more will go back through the history. Been using that a lot recently when doing docker stuff. ctrl+r, type the container name, keep going until I get the compose build command. ctrl+r, ctrl+r, repeat until the log command. Then I can just mash ctrl+r to get the build and log commands. Ctrl+r is your friend. ctrl+r

arcanemachiner

2 hours ago

Make sure to add fzf + shell integration for maximum Ctrl+r goodness.

hikarudo

2 hours ago

One trick I use all the time:

You're typing a long command, then before running it you remember you have to do some stuff first. Instead of Ctrl-C to cancel it, you push it to history in a disabled form.

Prepend the line with # to comment it, run the commented line so it gets added to history, do whatever it is you remembered, then up arrow to retrieve the first command.

$ long_command

<Home, #>

$ #long_command

<Enter>

$ stuff_1 $ stuff_2

<Up arrow a few times>

$ #long_command

<home, del>

$ long_command

fragmede

2 hours ago

Fwiw, in Bash, alt-shift-3 will prepend the current command with # and start a new command.

j4cobgarby

38 minutes ago

More generally, it's alt-#. On an ISO (e.g. UK) keyboard layout, shift-3 isn't a hash.

amelius

2 hours ago

What confuses me is that Ctrl+Y "yank" means the opposite of what it means in Vim. Certainly does not help with keeping my sanity.

nasretdinov

an hour ago

I'd advise against using sudo !! though since it adds the command to history and then it's very easy to accidentally trigger, running some undesired command as root without any prior confirmation. IMO pressing up, Ctrl-A and typing "sudo " isn't much longer but saves you from running unknown commands as root by accident

000ooo000

an hour ago

I have a bash key binding, Ctrl+Y, that prepends sudo to the current command and submits it. I also don't use sudo-rs. No one has died yet.

kgwxd

an hour ago

Decades ago, i used a small dns host. I wanted to switch a personal site and they just couldn't get the final step of the transfer to work. A ton of "try now" emails spanning several weeks.

Then one day, I was trying to setup MySQL on a personal Linux machine, and it wouldn't let me use my "standard password" for the admin account. I knew I could just use a different one, but I really wanted to know what the problem was. Took a long time, and I don't remember how I figured it out, but I eventually tracked it to the password ending with '!!'.

It took a while to put it together, and I never confirmed with the dns host support it's what fixed the issue but, I changed my password there, tried the transfer again, and it worked without any help from support. I suspect my plaintext password played some part in a script used in the transfer process, and was outputting the previous command in place of the !! I wish I had asked them if that was it, but if it was, they would have to admit to having my plain text password, or lie about it.

exceptione

an hour ago

I didn't know the `ALT + .` trick to repeat the last argument, but what is even more neat (and not mentioned in the article) is that it cycles through your history. At least it does in my shell.

chasil

3 hours ago

A much larger base for ksh (as a pdksh descendent) is Android. OpenBSD is a tiny community in comparison, although Android has acquired code directly from OpenBSD, notably the C library.

The vi editing mode is always present in ksh, but is optional in dash. If present, the POSIX standard requires that "set -o vi" enable this mode, although other methods to enable it are not prohibited (such as inputrc for bash/readline), and as such is a "universal trick."

The article is relying on some Emacs mode, which is not POSIX.

$_ is not POSIX if I remember correctly.

History in vi mode is easier, just escape, then forward slash (or question mark) and the search term (regex?), then either "n" or "N" to search the direction or its reverse.

I've seen a lot of people who don't like vi mode, but its presence is the most deeply standardized.

prodigycorp

an hour ago

There's one thing you need to only think about once, and has the potential to save you a ton of time: profile your ZSH startup time!

Stuff like NVM or Oh My ZSH will add a few seconds to your shell startup time.

commandersaki

an hour ago

My favourite trick is either commenting out a whole command or placing a comment at the end of a command to make it easier to find in my persistent history (thanks eliben) [0], using the # character.

I tried this in zsh and it wasn't the default behaviour which immediately made me nope from the shell altogether, among all the other quirks. I've just been using bash for far too long to switch to something different.

[0] https://eli.thegreenplace.net/2013/06/11/keeping-persistent-...

Joker_vD

3 hours ago

> The “Works (Almost) Everywhere” Club

> The Backspace Replacements

Also known as "emacs editing mode". Funnily enough, what POSIX mandates is the support for "vi editing mode" which, to my knowledge, almost nobody ever uses. But it's there in most shells, and you can enable it with "set -o vi" in e.g. bash.

ZeroGravitas

3 hours ago

Vi mode is also available in Claude code and gemini-cli to give some recent examples, and a bunch of other places you might not expect it, as well the more obvious places where code is written.

Once you get used to it, it is painful to go back.

mr_mitm

2 hours ago

My biggest complaint about the fish shell is the lack of true vi mode. They attempt to emulate it and it works to some degree, but it's no comparison to readline's implementation.

worksonmine

an hour ago

And if you set `set editing-mode vi` in ~/.inputrc (readline configuration) you'll have it in even more places.

vdm

2 hours ago

Ctrl-r works well at searching character trigrams, which can include space. Trigrams without space work well with auto_resume=substring .

`| sudo tee file` when current user does not have permission to >file

piekvorst

33 minutes ago

Is it just me, or is it an LLM language? The article tries very hard to be correct but somehow lacks experience.

I've never used the majority of these tricks for decades, except for brace expansion, process substitutions, and complex redirections.

user

2 hours ago

[deleted]

keybored

an hour ago

> We’ve all been there.

Close tab.

I ought to migrate away from shell scripting and just keep the shell for interactive use. Unfortunately I have cursed myself by getting competent-ish with P. shell and Bash scripting. Meaning I end up creating maintenance headaches for my future self.

(Echoes of future self: ... so I asked an LLM to migrate my shell scripts to Rust and)

Anyway with the interactive shell stuff. Yeah the I guess Readline features are great. And beyond that I can use the shortcut to open the current line in an editor and get that last mile of interactivity when I want it. I don’t really think I need more than that?

I tried Vim mode in Bash but there didn’t seem to be a mode indicator anywhere. So dropped that.

Edit: I just tested in my Starship.rs terminal: `set -o vi`. Then I got mode indicators. Just with a little lag.

tetris11

3 hours ago

Never heard of instant truncate, nor `fc`, nor `Esc .`

Quite a few useful ones

ta8903

2 hours ago

Something that should be mentioned is starting a command with a space doesn't add it to your history in most shells, really useful for one-off commands that you don't want cluttering your history.

Also, increase your `$HISTSIZE` to more than you think you would need, there have been cases where it helped me find some obscure command I ran like 3 years before.

aa-jv

3 hours ago

My favourite shell trick is to comment my code:

  $ some_long_command -with -args -easily -forgotten # thatspecialthing
... Some weeks later ..

  $ CTRL-R<specialthing>
.. finds:

  $ some_long_command -with -args -easily -forgotten # thatspecialthing

Need to see all the special things you've done this week/whenever?

  $ history | grep "\#"
...

Makes for a definite return of sanity ..

senectus1

3 hours ago

omg >$ CTRL-R<specialthing>

I could kiss you.. this alone is amazing!

000ooo000

an hour ago

!?specialthing?

If you are feeling brave

fragmede

3 hours ago

http://atuin.sh adds a database to store history in and a custom app to use for lookup with added modes to help with searching.

aa-jv

3 hours ago

Yes indeed, it is very fun to discover this if you don't know it already, it expands your understanding of your shell life immensely, doesn't it?

amelius

an hour ago

What I hate is that if you start a command with a space it is not recorded in the history. This happens often when copy+pasting commands. I know you can turn it off but still ... this drives me mad.

faangguyindia

3 hours ago

I just open, agent in tui, and ask it to do what I want and make a plan, i read the plan edit it and run it.

Simple, no need to learn any commandline these days.

I used to use arch and all, and managed many big projects. I find little value in learning new tools anymore, just feed it docs and it generated working plan most of the time

Now I've moved to coding in Haskell, which i find suits me better than wasting my time with cli and exploring what options all these cli tools have.

chriswarbo

6 minutes ago

I'm confused; how is writing a shell command (using shortcuts like those in the article!) "wasting time", but describing what you want to an LLM, having it make a plan, reading the plan, editing it, and running it is somehow not a waste of time?

You also mention there being "little value", when your proposed approach costs literal money in form of API/token usage (when using hosted models).

> Now I've moved to coding in Haskell

You might like https://hackage.haskell.org/package/turtle or http://nellardo.com/lang/haskell/hash/

worksonmine

an hour ago

What is it like to be this proud of not learning the tools you use? Do you really think several paragraphs to an agent that may or may not be correct is the "easy" way compared to just checking the manual for the flag you want?

I will never understand people like you.

faangguyindia

24 minutes ago

Tools are means to end.

They don't matter much to me.