Top
Best
New

Posted by zdw 21 hours ago

Shell Tricks That Make Life Easier (and Save Your Sanity)(blog.hofstede.it)
454 points | 219 commentspage 3
martinflack 5 hours ago|
I use `!!` quite a bit to repeat the output of the prior command as an argument.

    # it's in my PATH but can't remember where
    which myscript
    vi `!!`
void-star 8 hours ago||
set -o vi

<esc> puts you into vi mode at the cli prompt with all the semantics of the editor.

These carpal tunnel riddled hands can’t be bothered to reach for ctrl or alt let alone arrow keys.

cess11 6 hours ago|
If you aren't aware already, you can put 'setxkbmap -option ctrl:swapcaps' in one of your startup config files, like .bashrc or somesuch. That flips left CTRL and CAPS LOCK.
ruptwelve 9 hours ago||
Maybe not a shell trick per-se but I have been a very big fan of zoxide. It can jump around your common directories. If you have a ~/workspace/projects and you are anywhere and type `cd projects` it will take you to that directory. I never realized how much I got hooked onto it, until I used a system without it.
llarsson 3 hours ago|
Built in functionality that may offer something similar:

https://linuxhandbook.com/cdpath/

coopykins 8 hours ago||
My favourite QoL improvement to any shell I use is to improve the history function(Ctlr+R)I personally like https://github.com/cantino/mcfly
fzeindl 10 hours 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 )"
            #######################################################
a-french-anon 5 hours ago||
I'd suggest `pwd -P` to resolve symlinks too. (if you use DIR to call/source neighbouring scripts).
TacticalCoder 6 hours ago||
Wait... Most of my shell scripts have zero unused variables: I prefer to comment them if I may need them later on.

Why do you disable SC2034?

I don't think not having unused variables prevent me from doing things in my scripts!?

I understand if it's a preference but SC2034 is basically one of my biggest timesavers: in my case unused variables are typically a bug. Except, maybe, ANSI coloring variables at the top of the script.

fzeindl 4 hours ago||
I disable it only for the DIR variable which I might not use.
fp64 6 hours ago||
Here's my favorite tip: If you use bash, you can write bash on your prompt (duh). But this is one of the biggest reasons I stick with bash everywhere, as I am quite comfortable and experienced in bash and sometimes it's just easier to write things like `for i in *.mp3; do ffmpeg -i $i ...` etc. If it's re-usable, I write it to a bash script later.
integralid 4 hours ago|
That's vaccously true as you said isn't it? I write fish on my shell and then I can save it as a fish script. Worth noting that bash is much more portable and available by default, but if I'm going for portability I go straight to /bin/sh
fp64 3 hours ago||
Fair point, but for scripting I don't feel fish (or zsh) offer an advantage big enough to bother learning that language with their rather narrow scope. But bash it's good to anyways know, you don't really get around it either. Larger/more complex scripts I write in other languages (depending on domain I and other requirements I guess). It's also not that I daily write those scripts on my shell, so I also think that even if I learned fish or zsh, I would have to look up things again every time I need to write something again.
thibran 3 hours ago||
Its almost ironical that we still use the Terminal - and many use it like in the eighties using Bash - and seem to have forgotten that we should invent a better terminal & shell than doing all the workarounds to handle the quirks of the current systems.
mmh0000 3 hours ago|
Make a better system, and we'll consider using it.

A Terminal + Bash/ZSH is soooo sticky because they are VERY good at what they do once you learn the basics and quirks. And now with LLMs, CLIs are even better because LLMs talk in text and CLIs talk in text.

Microsoft tried with PowerShell to design a better system; it "technically" is better, but not "better enough" to justify the cost of switching (on Linux). The same is true of nushell; it is "better", but not better enough to justify switching for most people.

I believe we're at "peak input method" until someone invents Brain<->Computer interfaces.

thibran 2 hours ago||
I use the Terminal all the time and write my own CLI tools, but I'm feeling more and more the limits of the current system. With the years I have used almost all available shells (EShell was even my default for some time). Right now my favorite shell is Nushell, but still, it feels dated compare to what is possible on modern computers.

> Make a better system, and we'll consider using it. It's on my TODO list, but it will break with all conventions and tools (no TTY). My idea is to bring the chain-things-together idea to the 21st century using a keyboard first GUI.

chasil 13 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.

SoftTalker 5 hours ago||
I knew most of these but the $_ variable and "ESC + ." to reference or insert the last argument of the previous command. I can see getting some use out of that, so thanks for posting.
t312227 5 hours ago|
or - as an alternative to <esc> + ".":

for the last argument

* <alt> + "."

if you want the -<n>th argument:

* <alt> + "_" # n times :=)

* <alt> + "."

cheers a..z

commandersaki 11 hours 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-...

More comments...