lnav
lnav copied to clipboard
FR: reverse search command and hotkey
There are many other programs which support /
as the hotkey for searching and also support ?
for reverse searching. Obviously reverse searching is very useful, so it would be most intuitive if this was implemented and bound to ?
.
The question then becomes which hotkey to use for displaying help. Maybe Control + h
, since h
and H
are already taken?
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
Searching is live and happens in the background in lnav, which is different from the way other tools work that will block looking for hits. So, I don't think a backwards search is particularly useful. One thing I can change is to start another searcher that goes backwards from the top-line instead of just the one searcher that goes forward.
I am new to lnav and love it, only thing I wish it had in addition is reverse search. I use less
and vim
daily and make use of ?
all the time to jump backward to a search term in a file and /
to jump forward to a different search term. It's a workflow I'm used to and like a lot.
In lnav, I have learned that to do similar, I can jump forward with /
and then Shift
+ n
to accomplish going to the first previous hit of a search term. It's only one extra step so it works for me, but ?
would be instant and much nicer.
Example:
- In
less
, open a log file,G
to jump to the end,?ERROR
to jump to the last error logged. In lnav, the equivalent isG
,/ERROR
,Shift
+n
In isolation, this seems like not a big deal, but when I'm deep into a multi-hour debugging session and I need constantly to jump forward and backward in the file while changing the search term, having it be instant would be really nice.
@aspiers @melwitt I'm working on a change to trigger a jump to the first hit above the top line by pressing CTRL-J instead of <Enter>. In other words, after entering the search string, pressing <Enter> would jump to the first hit below the top line and pressing CTRL+J would jump the the first hist above the top line. I've also modified the preview to show the first hit above the top line instead of only the lines below the top line.
How does that sound?
I'll push the change in a couple days for you to try out.
I like the idea a lot! Although Ctrl+J is not an entirely intuitive choice of binding to me, because in many classic UNIX environments it's considered equivalent or nearly equivalent to Enter. IMHO Shift+Enter would be a much more intuitive choice because that is already commonly used for reversing search direction: for example, in both Chrome and Firefox if you search for a phrase via Control+F then hitting Enter moves to the next match, and Shift+Enter moves to the previous match.
Love the idea! +1 to what @aspiers said.
Thank you @tstack for working on this!
I've pushed the change and also added a :prompt
command so that you can rebind the ?
key (or whatever you want) to open the search prompt in the mode where it will jump up instead of down. The following command will rebind ?
to do this:
:config /ui/keymap-defs/default/x3f/command :prompt --alt search ?
(I added the :prompt
command to satisfy the request in #547)
I would've liked to use Shift+Enter instead of CTRL+J, but I don't think terminals all agree on what to send to the process when Shift+Enter is pressed. On my Mac, iTerm.app and Terminal.app send a carriage return for Enter and Shift+Enter.
@tstack commented on October 1, 2020 7:33 AM:
I've pushed the change and also added a
:prompt
command so that you can rebind the?
key (or whatever you want) to open the search prompt in the mode where it will jump up instead of down. The following command will rebind?
to do this::config /ui/keymap-defs/default/x3f/command :prompt --alt search ?
(I added the
:prompt
command to satisfy the request in #547)
Sounds nice!
I would've liked to use Shift+Enter instead of CTRL+J, but I don't think terminals all agree on what to send to the process when Shift+Enter is pressed. On my Mac, iTerm.app and Terminal.app send a carriage return for Enter and Shift+Enter.
Urgh, good point. I found these which should help somewhat for Mac users who really want Shift+Enter to work in their terminals:
- https://stackoverflow.com/questions/5388562/cant-map-s-cr-in-vim
- https://stackoverflow.com/questions/16359878/vim-how-to-map-shift-enter
However I understand that that doesn't really help in terms of choosing a default binding, so it probably makes to stick with CTRL+J as the default and make it configurable as you have done. Thanks a lot!
BTW how about also including this example rebinding in the docs along with others which are all likely to be popular? It would be a big help for users who want to rebind keys.
While searching backwards using binding suggested earlier, moving with n and N still goes forward/backward (in respect to position in file). Shouldn't they go backward/forward when searching backward?