Add select next match in Notepad++ (like Ctrl + D in Sublime Text)

I am searching for a way to use the following functionality in the open source Notepad++.

In SublimeText if you press Ctrl + D (mac: cmd + D I think) this happens:

  • If there is no selection then the cursor position is expanded to select that word.
  • Otherwise the next occurance of that word is also selected (without the need to open a search-popup).

You then have a multi-selection of words you can change, and you have actually seen each of these places (as opposed to a select-all).

Is there any way this can be done in Notepad++ (maybe with the help of Autohotkey)?

Optional: In Sublime you can also undo each of these Ctrl + D's with Ctrl + U and skip an occurance with Ctrl + K.


Solution 1:

I found this thread on the Notepad++ Community page:

https://notepad-plus-plus.org/community/topic/11360/multi-selection-and-multi-edit

They are using the python script plugin to create this functionality with the following script:

# this script implements the enhanced multi cursor edit functionality

def default_positions():
    return 0, editor.getLength()

def get_pos_of_bookmarks():
    npp_bookmark_marker_id_number = 24
    npp_bookmark_marker_mask = 1 << npp_bookmark_marker_id_number
    _start_position, _end_position = default_positions()

    line_nbr = editor.markerNext(_start_position, npp_bookmark_marker_mask)
    if line_nbr != -1:
        _start_position = editor.positionFromLine(line_nbr)
        line_nbr = editor.markerNext(line_nbr + 1, npp_bookmark_marker_mask)
        if line_nbr != -1:
            _end_position = editor.getLineEndPosition(line_nbr)
    return _start_position, _end_position

def get_pos_of_visible_lines():
    first_visible_line = editor.getFirstVisibleLine()
    _start_position = editor.positionFromLine(first_visible_line)
    lines_visible = editor.linesOnScreen()
    last_visible_line = editor.docLineFromVisible(first_visible_line+lines_visible)
    _end_position = editor.getLineEndPosition(last_visible_line)
    return _start_position, _end_position

def get_pos_of_selections():
    _start_position, _end_position = default_positions()
    if editor.getSelections() == 2:
        _start_position = editor.getSelectionNStart(0)
        _end_position = editor.getSelectionNEnd(1)
    return _start_position, _end_position


area_dict = {'a':default_positions,
             'b':get_pos_of_bookmarks,
             's':get_pos_of_selections,
             'v':get_pos_of_visible_lines}

editor.beginUndoAction()

def Main():
    _text = editor.getTextRange(editor.getSelectionNStart(0), editor.getSelectionNEnd(0))
    if len(_text) != 0:

        _current_position = editor.getCurrentPos()
        _current_line = editor.lineFromPosition(_current_position)
        _current_word_start_pos = editor.getLineSelStartPosition(_current_line)
        _current_word_end_pos = editor.getLineSelEndPosition(_current_line)

        find_flag = 2 # 0=DEFAULT, 2=WHOLEWORD 4=MATCHCASE 6=WHOLEWORD | MATCHCASE
        mode_options = ' 0=replace,  1=before,  2=afterwards\n'
        area_options = ' a=all, b=bookmarks, s=selected, v=visible'
        expected_results = [x+y for x in ['0','1','2'] for y in ['a','b','s','v']]

        result = notepad.prompt(mode_options + area_options, 'Choose the desired option', '0a')
        while result not in expected_results: 
            if result is None:
                return
            result = notepad.prompt(mode_options + area_options, 'Choose the desired option', '0a')

        chosen_mode, chosen_area = result
        area_start_position, area_end_position = area_dict[chosen_area]()

        if chosen_mode == '0': # replace whole string version
            editor.setEmptySelection(_current_position)       
            position_tuple = editor.findText(find_flag, area_start_position, area_end_position, _text)

            while position_tuple is not None:
                if _current_position not in position_tuple:
                    editor.addSelection(*position_tuple)
                position_tuple = editor.findText(find_flag, position_tuple[1], area_end_position, _text)


        elif chosen_mode == '1': # insert before selected string version
            editor.setEmptySelection(_current_word_start_pos)
            position_tuple = editor.findText(find_flag, area_start_position, area_end_position, _text)

            while position_tuple is not None: 
                startpos, endpos = position_tuple
                if startpos != _current_position and endpos != _current_position:
                    editor.addSelection(startpos, startpos)
                else:
                    _current_word_start_pos, _current_word_end_pos = startpos, startpos
                position_tuple = editor.findText(find_flag, endpos, area_end_position, _text)


        elif chosen_mode == '2': # insert after selected string version
            editor.setEmptySelection(_current_word_end_pos)
            position_tuple = editor.findText(find_flag, area_start_position, area_end_position, _text)

            while position_tuple is not None: 
                startpos, endpos = position_tuple
                if startpos != _current_position and endpos != _current_position:
                    editor.addSelection(endpos, endpos)
                else:
                    _current_word_start_pos, _current_word_end_pos = endpos, endpos
                position_tuple = editor.findText(find_flag, endpos, area_end_position, _text)


        # now add the current selection
        editor.addSelection(_current_word_start_pos, _current_word_end_pos)

Main()
editor.endUndoAction()

Solution 2:

You can just hit F3 to continue searching.