Live data from Hacker News

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

blog.hofstede.it

231–240 of 287 posts

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

#231

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 never understood why emacs mode became the default. "set -o vi" is the _first_ command I type in a new shell.

remap Caps Lock to Ctrl and see the light from home row

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

#232

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 can also be achieved with `.inputrc`: "\e[A": history-search-backward "\e[B": history-search-forward

Thank you !

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

#233
post #77

Earlier quoted context omitted.

Can you explain to me why either of these is useful? I've somehow gotten by never really needing to pipe any commands in the terminal, probably because I mostly do frontend dev and use the term for starting the server and running prodaccess

Pipelines are usually built up step by step: we run some vague, general thing (e.g. a `find` command); the output looks sort of right, but needs to be narrowed down or processed further, so we press Up to get the previous command back, and add a pipe to the end. We run that, then add something else; and so on. Now let's say the output looks wrong; e.g. we get nothing out. Weird, the previous command looked right, and…

no no, not asking why use CLI. If I was less lazy, I would use it more often

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

#234

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.

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.

For a bit about the language, read `3bcw` as move `b`ackward by `3` words and `c`hange the `w`ord under the cursor.

The general form of `b` is `[count]b` where

    [count] An optional number that may precede the command to multiply
            or iterate the command.  If no number is given, a count of one    
            is used, unless otherwise noted.  Note that in this manual the
            [count] is not mentioned in the description of the command,
            but only in the explanation.  This was done to make the
            commands easier to look up.  If the 'showcmd' option is on,
            the (partially) entered count is shown at the bottom of the
            window.  You can use  to erase the last digit (|N|).

    b       [count] words backward.  |exclusive| motion.
https://vimdoc.sourceforge.net/htmldoc/intro.html#[count]

https://vimdoc.sourceforge.net/htmldoc/motion.html#b

For `c` it’s

    ["x]c{motion} Delete {motion} text [into register x] and start
                  insert.  When  'cpoptions' includes the 'E' flag and
                  there is no text to delete (e.g., with "cTx" when the
                  cursor is just after an 'x'), an error occurs and
                  insert mode does not start (this is Vi compatible).
                  When  'cpoptions' does not include the 'E' flag, the
                  "c" command always starts insert mode, even if there
                  is no text to delete.

    {motion} A command that moves the cursor.  These are explained in
             |motion.txt|.  Examples:
                 w           to start of next word
                 b           to begin of current word
                 4j          four lines down
                 /The    to next occurrence of "The"
https://vimdoc.sourceforge.net/htmldoc/change.html#c

https://vimdoc.sourceforge.net/htmldoc/intro.html#{motion}

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

#235
This snippet for zsh still has some rough edges but works for the majority of cases. Automatically extends any global alias when space is pressed in zsh. For ex. I have `alias -G G='rg -s'`, so if I type `command | G` it will autoexpand it to `command | rg -s` and so on.

  globalias() {
    local raw word
    # raw last blank-separated token, exactly as typed
    raw=${LBUFFER##\* }
    # shell-parsed last word
    word=${${(z)LBUFFER}[-1]}
    # if user typed \alias, don't expand
    if [[ $raw == \\* ]]; then
        zle self-insert
        return
    fi
    if alias -- ${(q)word} &>/dev/null; then
        zle _expand_alias
        zle expand-word
    fi
    zle self-insert
}

zle -N globalias bindkey ' ' globalias

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

#236

If only somebody had a lifehack for making me remember all these awesome commands. If I do something the slow way it's usually because I don't do the operation enough to burn it into my memory, or I got burned by accidentally hitting something close but incorrect once and closed the tab or something.

I find spaced repetition Anki flash cards surpisingly effective for this kind of thing.

You’d think remembering tonnes of shortcuts and commands and flags would be alot of effort but it’s surprisingly low effort when using cloze deletions and phrased like:

- “… is to delete the last word” - “Ctrl w is to …”

If you’re not familiar with spaced repetition it’s worth checking out, especially if you have a holey memory like mine.

https://ncase.me/remember/

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

#237
post #87

Earlier quoted context omitted.

Once you start using CTRL+r, you may find that you never reach for up arrow again.

I'm familiar with ctrl-r, but I still very much like the up-arrow behavior described by that commenter.

What I love about the default Bash Crtl-C behaviour is that once a command has been located, the bash history is moved to the history of that command, until Enter is pressed.

  $ a
  bash: a: command not found
  $ b
  bash: b: command not found
  $ c
  bash: c: command not found
  $ d
  bash: d: command not found
  $  b 
  $ a
That's great if I don't remember which command I was experimenting with, but I do know other commands that I did around that time (usually a file that I edited with VIM).

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

#238
CTRL+L isn't the same as clear btw, it really maps back to `clear -x`.

CTRL+L clears the visible output but you can still scroll up in your buffer to see the rest, clear will clear that scroll up buffer too.

I've written about and demo'd this in https://nickjanetakis.com/blog/clear-vs-ctrl-l-in-your-shell.

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

#239
post #122

Earlier quoted context omitted.

And once you get tired of fzf and want something better, you reach for https://atuin.sh . Completely transformed all of my workflows

From the atuin.sh website > Sync your shell history to all of your machines I think of my shell history as very machine specific. Can you give some insights on how you benefit from history sync? If you use it.

I sync Atuin to my home server but I also configure it to be host specific by default.

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

#240
post #87

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

Once you start using CTRL+r, you may find that you never reach for up arrow again.

If you use multiple terminals it kinda sucks unless you do export PROMPT_COMMAND='history -a' in your.bashrc or something cause only the last closed terminal saves to history
Post reply on HN