Trick to find commands in the terminal quickly
github.com
github.com
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
"Sync your shell history to all of your machines, wherever they are"
That sounds like a potential security issue though.
Regardless, I just run it local only on each of my machines, separately. It’s still really helpful!
Interestingly, the default fish shell also comes with something similar these days, although I still prefer the look of `fzf`.
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)?
I find that zsh is quite good at addressing some of the issues you mention.
# Ensure history is written after every command, allowing it to persist across sessions
shopt -s histappend # Append to history instead of overwriting
PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
# Set the history size (adjust as needed)
HISTSIZE=50000 # Number of commands kept in memory
HISTFILESIZE=1000000 # Number of commands kept in the history file
# Add timestamps to history (format: YYYY-MM-DD HH:MM:SS)
HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
# Ignore duplicate and space-prefixed commands
HISTCONTROL=ignoredups:ignorespace
# Save multi-line commands as a single entry
shopt -s cmdhist
# Allow history expansion with Ctrl + R
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'
Also, if you are on shells you worry about, use gnu screen (or tmux if you prefer) imho. It will give you a second layer of history to be able to dig through.```export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"```
-a = append to history file -c = 'clear' aka write the history file -r = reload
Every time you hit 'enter' this will add the command to the history file, and will reload your bash prompt history. That means that every terminal window will share a common history file and you don't need to close the TERM to 'save' the session to history.
This is ESPECIALLY helpful on remote (SSH) connections. Downside: You 'Up Arrow' history gets altered by every term window you have running. So you can't just blindly `up` to repeat a command.
It takes a little to get used to, but VERY handy.
This allows EV
No matter my config it would always glitch. Atuin is backed by a global sqlite db: problem solved. And unlimited history.
I don't use sync though, commands sent to 3rd party servers are a no-go.
# sometag
the --actual --command
"Bashtags" I call them, despite not actually being in bash these days.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.
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.
Most of the time I don't need the rubbish I typed along the way in my history.
Error code 22 is EINVAL, "invalid argument". If most of the CLI frameworks agreed to use it on invalid commands, shells could diferrentiate user errors and program errors and keep histories clean.
Adding this to POSIX Utility Conventions and GNU Coding Standards would be a great kickstart.
https://pubs.opengroup.org/onlinepubs/9699919799.2018edition...
I haven’t played with emojis in the terminal before. Wouldn’t this emoji trick depend on font support?
Like vi in reverse, n goes further back, but N goes forward.
The "set -o vi" option is a POSIX optional feature, and should work in any compliant shell.
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).
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.
Not a Mercurial user, I take it? (hg is the standard command for Mercurial.)
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.
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
Also got to watch out for Ctrl+d on qwerty kb layouts where s and d keys live side by side.
so basically I end up doing ctrl-r, then fuzzy find the command and run it
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.
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.
"vi - Allow shell command line editing using the built-in vi editor. Enabling vi mode shall disable any other command line editing mode provided as an implementation extension."
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V...
Later, search for <some memorable name>
https://gist.github.com/appsmatics/ff27e885460bd345eabe1c5f7...
bravo! you can also search with grep, I did not know!
``` history | grep ICON ```
CTRL+r
vs
CTRL+R (so Ctrl shift R)
I've tried, and see no difference: what should it be?
So ctrl shift R goes from the latest command backwards that fit your search term.
Oh I see, ctrl R does the same thing interesting.