Live data from Hacker News

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

blog.hofstede.it

221–230 of 287 posts

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

#221
On scripts that might handle filenames with spaces, I include:

    IFS='   ''
    '
Hint: the spaces between the first two apostrophes are actually one .

This does not affect the already written script (you don't need to press Tab instead of space to separate commands and arguments in the script itself), but by making and be the “internal field separators” will allow globbing with less quoting worries while still allowing for `files=$(ls)` constructs.

Example:

    IFS='   ''
    '
    echo hello >/tmp/"some_unique_prefix in tmp"
    cat /tmp/some_unique_prefix*
    fn="My CV.txt"
    echo "I'm alive" >/tmp/$fn
    cat /tmp/$fn
Of course this will still fail if there happens to be a filename with in it.

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

#222
post #143
post #8

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

...which is why I recently went to about:keyboard and removed that hotkey. I love that page. That, and Ctrl-N. No more forest of blank browser windows when using a terminal emulator in a web page! (Firefox only)

Ctrl+W is undoable.

Ctrl+Shift+T will undo your recent tab closures in reverse order. The tabs maintain their history as well.

I am very surprised at how many people in here don’t seem to know that. I learned about Ctrl+Shift+T before I learned about Ctrl+W. I was using the middle mouse button on a tab to close tabs before then.

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

#223
post #143

Earlier quoted context omitted.

...which is why I recently went to about:keyboard and removed that hotkey. I love that page. That, and Ctrl-N. No more forest of blank browser windows when using a terminal emulator in a web page! (Firefox only)

Ctrl+W is undoable. Ctrl+Shift+T will undo your recent tab closures in reverse order. The tabs maintain their history as well. I am very surprised at how many people in here don’t seem to know that. I learned about Ctrl+Shift+T before I learned about Ctrl+W. I was using the middle mouse button on a tab to close tabs before then.

I know. I used to use it fairly often when Ctrl-W still did something. It helps, but (1) it doesn't work if you closed the last tab and thus the whole window, you'd need to restore recently closed windows instead; and (2) it is still more disruptive and potentially state-losing than preventing an unwanted close in the first place. Tab history retention isn't perfect.

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

#224
post #66

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.

For the most simple case of a single job, I use the job number (`[1]` in the example) with %-notation for the background jobs in kill (which is typically a shell builtin):

    $ cat
    ^Z[1] + Stopped                    cat
    $ kill %1

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

#225

Earlier quoted context omitted.

What does it provide over mycmd1 #| mycmd2

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

Ah duh, cheers

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

#226

One thing I find life-changing is to remap the up arrow so that it does not iterates through all commands, but only those starting with the characters I have already written. So e.g. I can type `tar -`, then the up arrow, and get the tar parameters that worked last time. In zsh this is configured with bindkey "^[OA" up-line-or-beginning-search # Up bindkey "^[OB" down-line-or-beginning-search # Down

This is the default `fish` shell behavior. Type anything, up/down keys to iterate through full commands that containing the term; alt + up/down to iterate through args containing the term.

Fish is underrated

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

#227

Using the terminal becomes much more cozy and comfortable after I activate vim-mode. A mistake 3 words earlier? No problem: 3bcw and I'm good to go. Want to delete the whole thing? Even easier: cc I can even use 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.

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.

agreed, i use neovim as a terminal multiplexer because vi-mode is really bad. I wrote a blog post on how i solved the issue for myself https://loosh.ch/blog/neovidenal

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

#228

One thing I find life-changing is to remap the up arrow so that it does not iterates through all commands, but only those starting with the characters I have already written. So e.g. I can type `tar -`, then the up arrow, and get the tar parameters that worked last time. In zsh this is configured with bindkey "^[OA" up-line-or-beginning-search # Up bindkey "^[OB" down-line-or-beginning-search # Down

This is the default `fish` shell behavior. Type anything, up/down keys to iterate through full commands that containing the term; alt + up/down to iterate through args containing the term.

[deleted]

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

#229

Using the terminal becomes much more cozy and comfortable after I activate vim-mode. A mistake 3 words earlier? No problem: 3bcw and I'm good to go. Want to delete the whole thing? Even easier: cc I can even use 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.

i went even further and use neovim as my multiplexer

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

#230
post #8

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

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

I still maintain this is why macOS is the best OS for terminal work -- all the common keybindings for GUI tools use a different modifier key, so e.g. ⌘C and ⌘W work the same in your terminal as they do in your browser.

(Lots of the readline/emacs-style editing keybindings work everywhere in macos as well -- ^A, ^E, ^K, ^Y, but not ^U for some reason)

Post reply on HN