2 ms·
The following sets up a key binding to grab interesting lines from the history, throw them into a file, and pop it up in an editor: # put interesting lines f
by kmstout 6y ago
The following sets up a key binding to grab interesting lines from the history, throw them into a file, and pop it up in an editor:
# put interesting lines from history into a file, open an editor
function h2e() {
echo "#! /bin/bash" > "$3"
history | tail -n"$1" | egrep "$2" | sed -re 's/\s*[0-9]+\s+//' >> "$3"
"${EDITOR:-emacs}" "$3"
}
# prompt for h2e's arguments
function h2ei() {
read -p 'num. lines [1]: ' lines
read -p 'pattern [.]: ' pattern
read -p 'file [foo.sh]: ' fn
h2e "${lines:-1}" "${pattern:-.}" "${fn:-foo.sh}"
}
# fix the terminal settings for the duration of a command, revert afterward
function insane() {
save=$(stty -g)
stty sane
"$@"
stty $save
}
# bind F12 to our history grabber
bind -x '"\e[24~":insane h2ei'
It's been quite handy of late.
- nickthemagicman 6y agoSo you would run "h2ei 10 somepattern out.txt"? Which would put the last 10 lines matching some pattern to the txt file?
- kmstout 6y agoClose--it's those lines matching some pattern within the last 10 lines. Think of it as localize, then filter. I've written it the other way, too, but this seems more natural to me. (Also, a direct h2e invocation would take the arguments from the command line; h2ei will ask every time.) In practice, I will do something, realize that the thing I just did over the last several minutes could be useful--or the core of something more general--and then strike F12 to start h2ei. At the prompts, I'll generally give it a nice round number like 10 or 20 or 50 lines, leave the default pattern, and set the filename to be something evocative in ~/bin. An editor will immediately open on my new shell script, and I can trim it down and continue hacking on it from there. The key idea is that I get an easier path from doing something ad hoc to creating something more useful in the long run.