Live data from Hacker News

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

blog.hofstede.it

81–90 of 287 posts

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

#81
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

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

#82

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 $ #long_command $ stuff_1 $ stu…

In zsh you can bind "push-line-or-edit". In bash and all readline programs, you can approximate it with C-u followed by C-y (i.e. cut and paste). My history is still full of '#' and ':' (csh trauma) prefixed command-lines like you described though ...

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

#83

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

> life-changing

For further life-changing experience... add aliases to .bash_aliases

    alias gph='history | grep --colour -i '
    alias gpc='grep --colour -Hin '
    #if gnu time is installed
    alias timef='/usr/bin/time -f "tm %E , cpu %P , mem %M" '

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

#84
post #77

Earlier quoted context omitted.

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

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

I can imagine a pipeline where intermediate stages have been inserted to have some side effect, like debug logging all data passing through.

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

#85
post #3

My favourite shell trick is to comment my code: $ some_long_command -with -args -easily -forgotten # thatspecialthing ... Some weeks later .. $ CTRL-R .. 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 ..

I don’t keep history. Any commands I think will be useful, I save it in a script.

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

#86
post #78

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

Prepend your command with a space and now your command is not saved in the history.

That depends on the shell configuration.

On bash, you can achieve this by setting HISTCONTROL=ignorespace but that's not the default.

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

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

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

#88

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

Did this many years ago (but with bash) -- life changing is an apt way of saying it.

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

#89

Earlier quoted context omitted.

Huh. I don’t use vi-mode for more than jumping to the beginning or end of a line, which I like a lot.

You mean, like the “home” and “end” buttons?

Yeah but those are so far away, i have to hunt for them every time

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

#90

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.

The v has been such a lifesaver at times when having to execute/modify super complex commands!
Post reply on HN