On Mon, Jan 4, 2016 at 4:35 PM, Dennis Williamson < dennistwilliam...@gmail.com> wrote:
> > > On Mon, Jan 4, 2016 at 4:05 PM, Dennis Williamson < > dennistwilliam...@gmail.com> wrote: > >> >> >> On Mon, Jan 4, 2016 at 3:07 PM, Eduardo A. Bustamante López < >> dual...@gmail.com> wrote: >> >>> Take into account that many options have been provided (history -d, the >>> space >>> prefix, even editing .bash_history yourself). >>> >>> But you request a single key stroke to do this... why? >>> >>> If you enter a password by mistake in your shell, and it gets recorded, >>> then >>> you go and clean up. It's not hard to do. >>> >>> But since you request a simple-and-easy way of doing this, it seems like >>> you do >>> this a lot... which you shouldn't! :-) >>> >>> Now, it is up to you to convince Chet that it is so important to have a >>> simple >>> shortcut to do this. IMO, it isn't. >>> >>> -- >>> Eduardo Bustamante >>> https://dualbus.me/ >>> >>> >> >> Just bind your own keystroke to a function which uses history -d: >> >> histdel() { >> local last_command histline >> >> last_command=$(history 1) >> >> histline="${last_command% *}" >> >> history -d "$histline" # I wish history -d accepted negative >> offsets >> } >> >> bind -x '"\ez": histdel' >> >> Then Esc-z or Alt-z will delete the most recent history entry. You could >> choose another keystroke to bind. >> >> >> -- >> Visit serverfault.com to get your system administration questions >> answered. >> > > Actually, this is better: > > histdel() { > > ( # use a subshell to make extglob setting and function variables > local > > last_command=$(history 1) > > # strip modified-entry marker, it doesn't matter if we delete an > asterisk in the command since we're deleting it anyway > last_command=${last_command/\*/ } > shopt -s extglob > last_command=${last_command##*( )} # strip leading spaces > histline="${last_command%% *}" > > history -d "$histline" # I wish history -d accepted negative > offsets > > ) > } > > bind -x '"\ez": histdel' > > I'm using a subshell here. You can use the local keyword for variables and > save and restore the extglob setting if you prefer. > > -- > Visit serverfault.com to get your system administration questions > answered. > Petr Skočík pointed out to me in a private message that my subshell version only affects the history within the subshell. Here is a version that doesn't require setting extglob and is much shorter: histdel () { local histline commandline IFS=' *' read -r histline commandline <<< "$(history 1)" history -d "$histline" } -- Visit serverfault.com to get your system administration questions answered.