I often find myself searching in my history by doing something like
history | grep searchterm
This returns a list of all the times that I used
searchterm in a command. However, I would often like to know the n previous commands and the m next commands that I entered before and after that command which contained
searchterm. Often the context in which I ran a command is very useful.
What I would like to be able to do is
> history-search-context searchterm n m
and get separated lists of commands that were entered n commands before and m commands after any history entry that contains
> history-search-context mycommand 2 1 687 cd some/directory 688 ls -a 689 mycommand -flag arg1 690 touch file ------------- 943 apt-get install something 944 pwd 945 mycommand -otherflag arg1 arg2 arg3 946 rm file
This would tell me that I used
mycommand twice in recorded history with some interesting flags and arguments, and within a window of [-2,+1] commands I had one time run it in
some/directory and had to create a file, and the other time, I installed
something, checked which directory I was in, and then deleted
Is there an existing way to do this? If not, how to accomplish this?