- Edited
pin
Well yeah, that's an option. But that would be the easy option. I'd like to keep ksh and just learn how it works a little more.
pin
Well yeah, that's an option. But that would be the easy option. I'd like to keep ksh and just learn how it works a little more.
pfr What I'm trying to achieve here is this...
What's a sample history output look like in zsh?
EDIT: use fc -l 1 as history alone in ksh just prints the last 15 or so entries.
EDIT: add -n to the fc command to omit line numbers; still need to remove the initial blanks on some shells though.
Nevermind--I think this is what you want:
$ alias h='fc -l -n 1 | sed -Ee "s/^[[:blank:]]+//" | ... | tr -d "\n" | ...'
Best not to use absolute positions with cut -c.
rvp Nevermind--I think this is what you want:
$ alias h='history | awk "{ sub(/^ *[0-9]+ +/, \"\"); print }" | sort | uniq | ...'
Best not to use absolute positions with cut -c.
This doesn't cut the line numbers though.
rvp
That works. Except, because there are double and single quotes in the command, the only thing that doesn't work is cutting the new line with tr -d '\n'
alias h='history | awk "{ sub(/^[[:blank:]]*[0-9]+[[:blank:]]+/, \"\"); print }" | fzf | tr -d '\n' | xclip -selection c'
rvp both of those options work. Thanks!
rvp I like to see tr(1) as the sort of gem which reminds me why sed is evil.
JuvenalUrbino But sed is not evil, or it may be but, without sed a whole bunch of pkgsrc packages would be loaded with further patches.
JuvenalUrbino I like to see tr(1) as the sort of gem which reminds me why sed is evil.
sedevil? Nah... Write-only?--Oh yes!
tr is great because it works with binary files, but, I'm curious to know why you think sed is evil.
pin without sed a whole bunch of pkgsrc packages would be loaded with further patches.
Some seds (FreeBSD's, Linux's) can be used on binary files too: before @Jay pointed out in a post that NetBSD had a pkg_alternatives system just like Debian, I used to use sed -i to "fix" the shebang path in binary compressed python files:
$ sed -i '' -e '1s/env python$/&3.7/' ~/bin/youtube-dl
NetBSD's seddoesn't work with binariy files, so I use ed instead:
$ ed ~/bin/youtube-dl <<<$'1s/python$/&3.7/\nw\nq\n'
PS. The one thing I find irritating about sed is the lack of consistency in the handling of the -i flag everywhere. On FreeBSD, one uses sed -i '' ... to omit backup file(s). On NetBSD and Linux, it is sed -i'' ...--ie. without the intervening space.
Was ironic by the way, but enjoyed your replies nevertheless;
rvp PS. The one thing I find irritating about sed is the lack of consistency in the handling of the -i flag everywhere. On FreeBSD, one uses sed -i '' ... to omit backup file(s). On NetBSD and Linux, it is sed -i'' ...--ie. without the intervening space.
as for this, I can relate to it pretty much. The other day a friend of mine who's into economics was having a hard time while trying to perform some substitutions all over a .tex file, and was googling for utilities capable of automatizing the task; I volunteered to do the job and asked him to lend me his Macbook; forgetting macOS incorporates FreeBSD userspace utilities, I immediately threw a sed -i on the command line without result, since the command has got no in-place editing, and you need to redirect output to a file. GNU sed also uses '-r' for extended regexp as opposed to -E
alias h='fc -l 1 | awk "{ sub(/^[[:blank:]]*[0-9]+[[:blank:]]+/, \"\"); print }" | fzf | tr -d "\n" | xclip -selection c'
This is too cool, thanks!
Is there maybe a way to start the chosen command via enter? ctrl-c/ctrl-v works, but via enter would be more handy.
silvanus I prefer to include the xclip -selection -c at the end so I can paste the command elsewhere if needed (such as in a web browser or irc chat). You can probably get it to execute upon selection somehow (I'm not sure how) but that would become annoying if you only wanted to copy a long command without disrupting what you already have in your terminal.
pfr It looks it might be ''xclip -selection clipboard" (did not see -c) on OpenBSD, but without it it works anyway with copy&paste. I ended up using
alias h='fc -l 1 | awk "{ sub(/^[[:blank:]]*[0-9]+[[:blank:]]+/, \"\"); print }" | fzf --history=/home/USERNAME/.ksh_history | tr -d "\n"'
which is ok with copy&paste. After selecting a command and enter it writes at least the command to cli (but ends with a coredump). Tried to make it start with a shortcut (similar to ctrl-r) but no luck so far. As @rvp mentioned some xdotool magic might be the solution. Starting it with a shortcut and running the selected command on enter would be the optimal solution for me.
silvanus I can see where this would be handy in saving a few key strokes. Perhaps ill create a separate alias for this specific usage (if you figure it out let me know).
Also, is the --history=/home/USERNAME/.ksh_history part necessary? I get the same output with and without it.
And as @rvp mentioned sed works with bash & ksh so I'm now using this:
alias h="fc -l 1 | sed -Ee 's/^[[:blank:]]*[0-9]+[[:blank:]]+//' | fzf | tr -d \\n | xclip -selection c"
@rvp I might be getting awfully specific now, but is there any way to order the output from most recent commands first? The current command begins at the beginning of the histfile, whereas my most recent commands are at the end of the file..
pfr this more universal command
No--that uses the same wretched complicated regex as in the old awk version. Use fc -n to omit the line numbers so you can use the simpler sed command in my updated post.
pfr but is there any way to order the output to be from most recent commands first?
Add -r to the fc command.