from Hacker News

Trick to find commands in the terminal quickly

by Toby1VC on 2/16/25, 7:54 PM with 77 comments

  • by ralgozino on 2/16/25, 8:35 PM

    Install "fzf" [0] and set it up to be used with control+r, there's no going back. You get as a bonus the chance to use fzf in a lot of other places :)

    I guess that more advance tool would be "atuin" [1], but it is too much for my use case.

    [0] https://github.com/junegunn/fzf [1] https://github.com/atuinsh/atuin

  • by TeMPOraL on 2/16/25, 9:34 PM

    Interesting.

    For me, C-r is sufficient (and/or M-r in Emacs, where C-r by default does reverse interactive search on the text in buffer; it's nice to have both at the same time, actually). However, I must have skipped some education about shell history and/or its default settings, because half the time I need it, the command I want isn't there to be found. I also observed the following kinds of behaviors:

    - Sometimes, shell history seems to be scoped (or reacting to) current working directory;

    - Sometimes, commands executed on remote machines end up being saved in local history;

    - When the shell gets killed (e.g. when Emacs crashes and takes down the shells open inside with it), or the system crashes, sometimes the history gets saved, and sometimes nothing remains from a session that spanned multiple days;

    - When I have multiple terminals open, it's a coin toss whether only one will have history saved or all of them, and then another toss as to whether histories will be CWD-sensitive or not.

    Is there a good primer/community consensus on how to configure shell so all history gets saved in sensible manner (including continuously, so it survives a crash)?

  • by IshKebab on 2/16/25, 8:44 PM

  • by ashenke on 2/16/25, 8:14 PM

    For these commands I want to rerun, but not often enough, I add a comment to the command like `yay - Sc # clear pacman and yay caches` so it's easier to search in the future
  • by psxuaw on 2/16/25, 8:17 PM

    I just add " ###" to the end of commands run frequently. It makes easier to identify important commands with atuin, fzf or plain bash/fish history search.
  • by jstanley on 2/16/25, 8:13 PM

    > Instead of icons you could use text which you can grep later easily.

    An obvious improvement! And if you're using text you don't even need to be watching out to recognise it, you can search with Ctrl-R.

  • by kevincox on 2/17/25, 3:16 PM

    The icon idea is cool. I do something similar but optimized for searching rather than identifying. I put a "tag" in front of my commands. Then I can type the command and press up.

    For example

        mc= sexec nix-shell -p jdk17 prismlauncher --run 'exec prismlauncher -l 1.19.4 -s mc.example'
    
    Will start the right version of Minecraft with the right version of Java. I just type mc<Up> to find it.

    I use this for commands that I know I will want to reuse, but not frequent enough to always be near the top of my history. In this case I could probably search for "prism" and it would be good enough but for things like specific ffmpeg incantations it is hard to remember a unique part, so the tagging lets me give my own name to the command.

  • by stuaxo on 2/17/25, 10:00 AM

    I wish history would differentiate between non commands errors and commands that ran without error.

    Most of the time I don't need the rubbish I typed along the way in my history.

  • by k3vinw on 2/17/25, 6:32 AM

    Visual cues are welcome and can be very nice when applied effectively. For example, Readline has support for color highlighting: https://wiki.archlinux.org/title/Readline

    I haven’t played with emojis in the terminal before. Wouldn’t this emoji trick depend on font support?

  • by bilekas on 2/16/25, 8:51 PM

    Ctrl+R for reverse search is okay but can get a bit tedious when looking for variations on the command.
  • by nandkeypull on 2/17/25, 5:09 AM

    Using C-r is a little too much navigation overhead for me personally. For things that I'll need long-term, I just use a pair of aliases to add a new alias to my .bashrc and source it (alias vib='vim ~/.bashrc' and alias .b='source ~/.bashrc'). I also have 'vit' and 'viv' aliases to do the same thing for my .tmux.conf and .vimrc.

    For short-term stuff, I use https://github.com/dp12/fastdiract to save frequently used commands and run them instantly with a two-key combo (f0-f9).

  • by Duanemclemore on 2/16/25, 8:21 PM

    Clever approach, but one extra step for me.

    I made a shell script "hg" which stands for "history | grep." So "hg .wine" brings up all commands in the bash command history buffer with the string .wine in them, say "1601 ls .wine" To run one of course you just enter ! and the number of the command. So like... !1601. Whole process is extremely ergonomic.

    Although - if anyone wants to write a shell extension that always runs the command output in a separate panel and keeps the parent panel (or tab) to just the commands entered that would be cool too.

  • by kazinator on 2/17/25, 7:15 AM

    In the TXR Lisp repl, I implemented a feature I hadn't seen anywhere: submit a command recalled from history, and then move onto the next command in history. I assigned it to Ctrl-X Enter.

    Turns out, Korn shell had this first: Ctrl-O. And newer Bash has it also.

    You can repeat sequences of multiple commands in your history without having to navigate back to each one individually. Just find the first one and submit one by one with Ctrl-O.

  • by Gualdrapo on 2/16/25, 8:16 PM

    Then I'd spend even more time looking for an icon I'd think represents best the command, and then looking for that icon in the emoji selector
  • by jasonpeacock on 2/16/25, 10:14 PM

    Or use the right tools, like atuin[1] and hoard[2], to manage your shell history and preserve frequent/favorite commands.

    [1] https://terminaltrove.com/atuin/

    [2] https://terminaltrove.com/hoard/

  • by ajmurmann on 2/17/25, 4:13 AM

    I always found it most useful to have up and down arrows search through history for what I've already typed. So to run my tests via Gradle, I usually just type ’.g‘ followed by up arrow.

    I think I set it up with ‘‘‘ "\e[A": history-search-backward "\e[B": history-search-forward ’’’ (Not near a computer right now to confirm)

    I find this somehow much faster than C-r

  • by 1oooqooq on 2/16/25, 9:43 PM

    sligthly ontopic of emoji commands on shell for dyslexics and/or visual people, here's one i use often https://github.com/gcb/emojihash.sh?tab=readme-ov-file
  • by pjmlp on 2/16/25, 8:37 PM

    Ctrl+R followed by key parts of it.
  • by VeejayRampay on 2/16/25, 10:36 PM

    I use television (https://github.com/alexpasmantier/television) with shell integration

    so basically I end up doing ctrl-r, then fuzzy find the command and run it

  • by adamredwoods on 2/16/25, 8:30 PM

    I love making little bash scripts to improve my workflow, and I think everyone should do it, but-- on their own.

    For example I have 'glast' to list the 5 previous git branches.

    Or I have 'phelp' that basically lists the available commands in a package file.

  • by wang_li on 2/17/25, 3:46 PM

    The tool this guy is looking for is called "make". You build a makefile with the appropriate targets then you type "make check", "make build", "make debug", "make tag".
  • by seiferteric on 2/16/25, 9:46 PM

    I made a tool some years ago to store commands in a dot files https://github.com/seiferteric/clamp
  • by kittikitti on 2/17/25, 6:11 PM

    I usually copy and paste my bash history to a LLM and ask questions. But I also use Ctrl (Cmd) + R for a exact keyword search.
  • by mooreds on 2/16/25, 9:44 PM

    The first thing I add to my shell RC file is 'set -o vi'

    Then I can use the vi/vim search keys

    - escape

    - '/'

    - <type words>

    - hit n or N to move back and forth

    Way way easier than using the up or down arrow.

  • by SoftTalker on 2/16/25, 9:51 PM

    If you have a command you run over and over, make an alias for it. That way if your shell history gets clobbered you still have it.
  • by roydivision on 2/17/25, 8:21 AM

    $ <some long command I will probably need later> # <some memorable name>

    Later, search for <some memorable name>

  • by mettamage on 2/16/25, 11:50 PM

    Emojis are underutilized in so many ways. THis is a fun way. Are there other ways? Sure. This is one of them.
  • by hakcermani on 2/16/25, 9:53 PM

    .. another cheap trick .. when juggling different projects i keep a separate history file for each under ~/.histories/ .. shameless plug to my gist .. bash script that launches gnome-terminal with a named history file ..

    https://gist.github.com/appsmatics/ff27e885460bd345eabe1c5f7...

  • by oriettaxx on 2/16/25, 11:56 PM

    genius!

    bravo! you can also search with grep, I did not know!

    ``` history | grep ICON ```

  • by ethan-j on 2/17/25, 3:27 AM

    zsh-autosuggestions is very useful!
  • by TZubiri on 2/16/25, 9:11 PM

    Ctrl shift R