Saturday, March 17, 2012

Adding "Search at point" function to Notepad++

Notepad++ with the Python Script plugin is a great code editor for windows.

After adding the following two files to your Notepad++ scripts folder you can make it even better with what I'm quickly finding to be an indispensable feature:

By using the Alt+left and Alt+right keys you can move to the previous and next occurrences of the symbol under the cursor. e.g. you can move quickly between all usages of a variable or function name in a file.

# goto_next_occurrence.py
"""Move cursor to next occurrence of the current word in the file, wrap around if possible."""

from Npp import editor, FINDOPTION
symbol = editor.getCurrentWord()
editor.wordRight()
editor.searchAnchor()
pos = editor.searchNext(FINDOPTION.MATCHCASE+FINDOPTION.WHOLEWORD+FINDOPTION.WORDSTART, symbol)
editor.scrollCaret()
# goto_prev_occurrence.py
"""Move cursor to previous occurrence of the current word in the file, wrap around if possible."""

from Npp import editor, FINDOPTION
symbol = editor.getCurrentWord()
editor.wordLeft()
editor.searchAnchor()
pos = editor.searchPrev(FINDOPTION.MATCHCASE+FINDOPTION.WHOLEWORD+FINDOPTION.WORDSTART, symbol)
editor.scrollCaret()

Then add them to the menu:

And bind them to keys, Alt-right and Alt-left work well for me:

Back in Linux-land, there are a bunch of ways to add similar functionality to Emacs, I like this one.

No comments: