Keyboard shortcut for next/previous incremental search result?
I find incremental search very useful and I almost always use it instead of the normal ‘Find’. However, I have a habit of doing an incremental search for certain text, then closing the incremental search bar, doing some writing, and then pressing F3/Shift-F3 (Next/Previous search result), expecting to go to the next/previous result based on what I entered in the incremental search bar; however, this actually just goes to the next/previous result from whatever the last regular ‘Find’ search I did was.
Is there any way to use a shortcut that goes to the next/previous result of my incremental search, without having to re-open the window? Re-opening the bar, going to the next result, then closing it back again, adds two extra steps (opening + closing the bar) which it would be nice to eliminate…
This has been an open issue since May 2016; see https://github.com/notepad-plus-plus/notepad-plus-plus/issues/1845
Oddly enough, I remember that issue was there since I added a comment to it earlier today. :-D
As a side note, why would you close the incremental-search window, only to have to reopen it? That sounds like two unnecessary steps to me. I always just leave it open; it is not that large that it takes up too much real estate…
Ah, thanks, didn’t come across that when I searched.
The reason I close it is because it is just a single keystroke, Esc, whereas leaving it open would require me to use the mouse to switch focus back to the file itself – unless there is a keyboard shortcut for swapping focus? I like to do as much as possible with just the keyboard, swapping back and forth between mouse and keyboard is very cumbersome for me.
Ah, that makes sense about the Esc key…
If you’re willing to set up and use the Pythonscript plugin, I’ve created a solution that will allow assignments of keyboard shortcuts to do the next/previous-incremental-search-result functionality directly from the current editor window.
Note that I didn’t do it for YOU…I thought about it after reading your post, and figured doing it would benefit ME! :-D
Here’s how to accomplish the goal:
- Create TWO new Pythonscripts, one named
IncrSearchWindowPressFindNext.pyand one named
- Bind these Pythonscripts to TWO new keyboard shortcuts via the shortcut mapper (I use
Shift+Ctrl+.for the “next” script and
Shift+Ctrl+,for the “prev” script–these keystrokes are self-reminding as they are a variant of the
>keys, the same as the text on the buttons in the Incremental Search window)
The TWO scripts should have the SAME contents and those contents are:
import ctypes from ctypes.wintypes import BOOL, HWND, LPARAM from inspect import currentframe, getframeinfo try: ISWPFNOP__incr_search_hwnd except NameError: ISWPFNOP__incr_search_hwnd = None def ISWPFNOP__main(): # (I)ncremental(S)earch(W)indow(P)ress(F)ind(N)ext(O)r(P)revious user32 = ctypes.windll.user32 SendMessage = user32.SendMessageW FindWindow = user32.FindWindowW FindWindowEx = user32.FindWindowExW GetWindowText = user32.GetWindowTextW GetWindowTextLength = user32.GetWindowTextLengthW EnumChildWindows = user32.EnumChildWindows GetParent = user32.GetParent GetClassName = user32.GetClassNameW WNDENUMPROC = ctypes.WINFUNCTYPE(BOOL, HWND,LPARAM) create_ub256 = ctypes.create_unicode_buffer(256) BM_CLICK = 0x00F5 if ISWPFNOP__incr_search_hwnd == None: # the goal with this section is to locate the Incremental Search window # this is technically overkill but it DOES define the Incremental Search window: incr_search_classname_and_text_seq_list = [ u'#32770', u'', u'Button', u'X', u'Static', u'Find :', u'Edit', None, # 'None' here means the text of control is variable u'Button', u'<', u'Button', u'>', u'Button', u'&Highlight all', u'Button', u'Match &case', u'Static', None, # 'None' here means the text of control is variable ] ISWPFNOP__main.seq_list_index = 0 def child_window_enumeration_function(hwnd, _): class_name = create_ub256[:GetClassName(hwnd, create_ub256, 256)] if (ISWPFNOP__main.seq_list_index % 2) == 0 and \ class_name == incr_search_classname_and_text_seq_list[ISWPFNOP__main.seq_list_index]: ISWPFNOP__main.seq_list_index += 1 advance_seq_index = False if (ISWPFNOP__main.seq_list_index % 2) == 1: text_length = GetWindowTextLength(hwnd) if text_length == 0: if incr_search_classname_and_text_seq_list[ISWPFNOP__main.seq_list_index] == u'' or \ incr_search_classname_and_text_seq_list[ISWPFNOP__main.seq_list_index] == None: advance_seq_index = True else: buff = ctypes.create_unicode_buffer(text_length + 1) GetWindowText(hwnd, buff, text_length + 1) if incr_search_classname_and_text_seq_list[ISWPFNOP__main.seq_list_index] == buff.value or \ incr_search_classname_and_text_seq_list[ISWPFNOP__main.seq_list_index] == None: advance_seq_index = True if advance_seq_index: ISWPFNOP__main.seq_list_index += 1 else: ISWPFNOP__main.seq_list_index = 0 continue_with_the_enumeration = True if ISWPFNOP__main.seq_list_index == len(incr_search_classname_and_text_seq_list): # we surely found the Incremental-Search window! (it is the parent of the current child) global ISWPFNOP__incr_search_hwnd; ISWPFNOP__incr_search_hwnd = GetParent(hwnd) continue_with_the_enumeration = False return continue_with_the_enumeration EnumChildWindows(FindWindow(u'Notepad++', None), WNDENUMPROC(child_window_enumeration_function), 0) if ISWPFNOP__incr_search_hwnd != None: # save current view and doc index (and do it so that restoring later works well if a doc is cloned) saved_view = notepad.getCurrentView() saved_document_index = notepad.getCurrentDocIndex(saved_view) # find out which python file we are so we can determine which button to press: currently_running_pythonscript = getframeinfo(currentframe()).filename caption_of_button_to_press = '>' if 'FindNext' in currently_running_pythonscript else '<' # FINALLY, press the desired button: next_or_prev_button_hwnd = FindWindowEx(ISWPFNOP__incr_search_hwnd, 0, u'Button', caption_of_button_to_press) SendMessage(next_or_prev_button_hwnd, BM_CLICK, 0, 0) # return input focus to editor tab (focus was with incr search window): notepad.activateIndex(saved_view, saved_document_index) ISWPFNOP__main()
If this (or ANY posting on the Notepad++ Community site) is useful, don’t reply with a “thanks”, simply up-vote ( click the
^ 0 varea on the right ).
- Create TWO new Pythonscripts, one named