Posted by zdw 18 hours ago
In zsh this is configured with
bindkey "^[OA" up-line-or-beginning-search # Up
bindkey "^[OB" down-line-or-beginning-search # Down"I typed 'cd di↑' and you're giving me 'pwd'??"
Completely transformed all of my workflows
> 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.
2. clone it on host_bar in /home/user/src/myproject
If you set filter_mode = "directory", you can recall project specific commands from host_foo for use on host_bar even though you're working on different machines and the search space won't be cluttered with project specific commands for other projects.
However what I do find useful is eternal history. It's doable with some .bashrc hacks, and slow because it's file based on every command, but:
- never delete history
- associate history with a session token
- set separate tokens in each screen, tmux, whatever session
- sort such that backward search (ctrl-R) hits current session history first, and the rest second
Like half my corporate brain is in a 11M history file at this point, going back years.
What I would love is to integrate this into the shell better so it's using sqlite or similar so it doesn't feel "sluggish." But even now the pain is worth the prize.
First, as for speed and responsiveness, if there is a degradation, it is imperceptible to me. I wouldn't have a clue that my interactive shell is slowing down because it is logging a command to ~/.persistent_history.
My persistent_history is 4MB and has been migrated from machine to machine as I've upgraded, it's never felt slow to edit with (neo)vim or search with system supplied grep.
Eli's way of doing it also includes the timestamps for all commands, so it's easy to trace back when I had run the command, and duplicates are suppressed. In fact my longest persistent_history goes back to 2019-07-04, so I've been using it for quite some time now.
But the larger point I wanted to make is that I wouldn't feel comfortable switching this, in my opinion, quite efficient setup to displace it with an sqlite database. That would require a special tool to drill through the history and search rendering simple unix utilities useless. As Eli suggested, if your history gets too big, simply rotate the file and carry on. I have the alias phgrep to grep ~/.persistent_history, but I can easily have another alias to grep ~/.persistent_history*.
[1]: https://eli.thegreenplace.net/2013/06/11/keeping-persistent-...
## arrow up
"\e[A":history-search-backward
## arrow down
"\e[B":history-search-forward "\e[A": history-search-backward
"\e[B": history-search-forward bind '"\e[A"':history-search-backward
bind '"\e[B"':history-search-forwardFor 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" 'One I came up and that I use all the time:
alias wl='wc -l'
I use it so much I sometimes forget it's not stock.One thing I do is configure my keyboard so that "modifier+{ijkl}" mimicks the inverted T arrows key cluster. So there's never a need for me to reach for the arrow keys. And {ijk} makes more sense than vi's {hjkl} and is faster/more logical/less key fingers travel. The nice thing is: as I do this at the keyboard level, this works in every single map. "modifier" in my case is "an easily reachable key in a natural hand position on which my left thumb is always resting" but YMMV.
I set that up years ago and it works in every app: it's gorgeous. Heck, I'm using it while editing this very message for example.
And of course it composes with SHIFT too: it's basically arrow keys, except at the fingers' natural positions.
A mistake 3 words earlier? No problem: <esc>3bcw and I'm good to go.
Want to delete the whole thing? Even easier: <esc>cc
I can even use <esc>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.
. . . 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.
I do prefer vi bindings at the same time though. Vi bindings and mouse support complement each other well, you don't have to choose one or the other, just use whichever feels most natural and convenient in that exact moment.
I think it's a question of context and familiarity. To a vim user, like me and, I assume, ahmedfromtunis, their examples do indeed seem simple and natural. Presumably, to an emacs user, the example you quote (if it's quoted literally—I don't use emacs and can't even tell) is just as natural, and assuming some comfort with emacs is presumably OK in a manual for the software!
How do you get familiar with the software, if the manual expects you to be an expert in it already?
edit: And of course, CTRL+R, the best time saver of all
I WANT to love it - and if I was only ever working on one, or a small number of systems that I was the only one working on I’d probably do it. I’m ALL about customizing my environment.
However ssh into various servers through the day (some of which are totally ephemeral), and having to code switch my brain back and forth between vim mode and emacs mode in the shell would just slow me down and be infuriating each time I connect to a new box.
A mistake 3 words earlier?
meta-bbbd (not as elegant, I admit)
delete the whole thing?
ctrl-ak (this is even quicker than vim, especially if capslock is mapped to ctrl)
the control-based emacs movements work system-wide on macos btw. I am using ctrl-p and ctrl-n to go up and down lines, ctrl-a and ctrl-e to go to beginning and end of lines while writing this comment in by browser (which has vimium extension)
Sometimes I wish vim just had full emacs bindings while in insert mode. But I don't like to mess with defaults too much.
I keep thinking I should give vim readline a try though, so maybe today. Thanks for the comment.
Doing control+o in insert mode temporarily places you into normal mode so that you can execute one normal-mode command, and then go back to insert mode again--no need to hit 'i' again.
So, instead of '<esc>cc', '<c-o>S'.
The one you suggest however requires 4 strokes (ctrl then o then shift then s), 4 keys (ctrl, o, shift, s) and 2 combinations.
The "cc" sequence deletes the line and switches automatically to insert mode. To forgo the switch, the sequence then becomes "dd".
<esc>S
Esc exits insert mode (of course) and capital S erases the line and puts you in insert mode at column 0 (just like in (n)vim, right?).
Like I said, maybe I configured that? But 'S' is standard vim-stuff... (I'm not able to double check my config at the moment).
[Edit: right after hitting submit I realized that my way is perhaps "arguably" simpler because I do have to hit shift to get capital S. So I'm also hitting three keys...]
I really need to get around to playing with it more. I just hope that especially now with genAI that it's not too late for learning it further.
<esc>3bcw
What is your keyboard layout? This looks like a crime against humanity on a regular qwerty kb.If I hit CTRL + ARROW_LEFT 3 times, I am done a lot faster I guess. But I am open to learn, do people really use that and achieve the goal significantly faster?
Your Problem with vim is you don't grok vi
https://stackoverflow.com/questions/1218390/what-is-your-mos...
Many people these days, including yours truly, have caps-lock mapped to ctrl if held or esc if tapped. That’s good ergonomics and worth considering for any tech-savvy person.
Instead of the 3b I would type bbb (because I agree with you that typing numerals is a pain).
So (caps lock)bbbcw isn’t bad. It’s better than it looks, because if you’re a vim user then it’s just so automatic. “cw” feels like one atomic thing, not two keypresses.
And importantly, it doesn’t involve any chords.
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.
He had in his path a script called `\#` that he used to comment out pipe elements like `mycmd1 | \# mycmd2 | mycmd3`. This was how the script was written:
```
#!/bin/sh
cat
``` #!/bin/sh
$*
that's my `~/bin/noglob` file, so when I call a zsh script from bash that uses `noglob`, it doesn't blow up. $ echo foo | tr fo FO | sed 's/FOO/BAR/'
BAR
$ echo foo | ${IFS# tr fo FO | } sed 's/FOO/BAR/'
foo
It's nice to have a way to both /* ... */ and // ...
in shell scripts though: foo \
| bar ${IFS Do the bar. Do it. } \
| baz
* in the best possible way, like it's awful - I hate I didn't think of that foo |
bar |
baz
You don't have to use backquotes, AND, it allows you to comment line by line, because there's no backslash messing with the parser.I also use a last `|\ncat` so you can delete any line and you don't have to worry about the last line being a bit different than the rest
I created a list of similar tricks in https://github.com/kidd/scripting-field-guide in case anyone wants to take a look
$ {
> echo foo \
> && echo bar \
> || echo baz ;
> }
foo
bar
<^P><^A>$<^F>IFS
${IFS# echo foo && echo bar || echo baz ; }
$ _
There's good and bad to both approaches. I like how I can use () and {} to bracket things and otherwise every line that end in \ is continued. I line-up on the left with the operator, you with indentation. When you use a # style comment, you have to look up and back and forward to see what the operator is you are continuing over to the next line: $ foo |
bar | # ?Do? *the* $bar$ && [do] {it!}
baz
Which only takes an extra neuron or so, but then history... <^P>
$ foo | bar | # ?Do? *the* $bar$ && [do] {it!}
bazmycmd1 #| mycmd2
This will output the stdout of mycmd1:
mycmd1 #| mycmd2 | mycmd3
This will output the stdout of mycmd3: mycmd1 | \# mycmd2 | mycmd3I'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
Now let's say the output looks wrong; e.g. we get nothing out. Weird, the previous command looked right, and it doesn't seem to be a problem with the filter we just put on the end. Maybe the filter we added part-way-through was discarding too much, so that the things we actually wanted weren't reaching the later stages; we didn't notice, because everything was being drowned-out by irrelevant stuff that that our latest filter has just gotten rid of.
Tricks like this `\#` let us turn off that earlier filter, without affecting anything else, so we can see if it was causing the problem as we suspect.
As for more general "why use CLI?", that's been debated for decades already; if you care to look it up :-)
Be careful working CTRL + W into muscle memory though, I've lost count of how many browser tabs I've closed by accident...
Yeah, pressing Ctrl-W accidentially is a pain sometimes ... but Ctrl-Shift-T in Firefox is a godsend.
Fun fact: despite having absolutely no menu entry for it, and I believe not even a command available with Ctrl+Shift+P, Vscode supports Ctrl+Shift+T to re-open a closed tab. Discovered out of pure muscle memory.
stty werase undef
bind '"\C-w": backward-kill-word'
source: https://superuser.com/questions/212446/binding-backward-kill...This hurts.
Also, for the shell, if you do C+w, you can "paste" it back using C+y. Assuming you have not removed that configuration.
That, and Ctrl-N. No more forest of blank browser windows when using a terminal emulator in a web page!
(Firefox only)
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
<Home, #>
$ #long_command
<Enter>
$ stuff_1 $ stuff_2
<Up arrow a few times>
$ #long_command
<home, del>
$ long_command
With zsh, I prefer to use alt-q which does this automatically (store the current line, display a new prompt, then, after the new command is sent, restore the stored line). It can also stack the paused commands, e.g.:
$ cp foo/bar dest/ <alt-q>
$ wcurl -o foo/bar "$URL" <alt-q>
$ mkdir foo <enter> <enter> <enter>
dcd() {
# If no argument is given, do nothing
[ -z "$1" ] && return
# Find the first matching directory under the current directory
local dir
dir=$(find . -type d -path "*$1*" -print -quit 2>/dev/null)
# If a directory was found, cd into it
[ -n "$dir" ] && cd "$dir"
}
I thought this would be way too slow for actual use, but I've come to love it. # Use F2 to edit the current command line:
autoload -U edit-command-line
zle -N edit-command-line
bindkey '^[OQ' edit-command-line # f2 is ^[OQ; to double check, run `xargs` and then press f2