How to specify codepage when searching via Find in Files



  • Most of the files I work with use the OEM 737 (Greek) character set (Encoding / Code page) and using the AutoCodepage plugin has been very useful in defining a default code page for particular file extensions. Find All in “Current” or “All Opened Documents” works fine in that NPP correctly matches Greek strings as well as properly displaying matched Greek text in the results window. However this not the case in “Find in Files” where there seems to be no way to request that matches be based on a particular codepage. Nor have I found a default definition where this could be set. Am I missing something?



  • Unfortunately I don’t know how to overcome this problem since I neither know how my AutoCodepage plugin (or a plugin in general) could influence the “Search in Files” feature nor how to set character encoding in the “Search Results” window.

    I recommend to file an issue at the Notepad++ GitHub repository. You can read this FAQ Desk entry to learn how to do that. Before doing so, be sure that there doesn’t already exist a similar issue.

    Alternatively you could use the following workaround. To make it work properly some prerequisites have to be true:

    • The workaround relies on using the FindStr console tool to perform a search operation based on OEM encoding. Thus, if you want to search in documents encoded with codepage OEM 737 (Greek) you should work on a Greek Windows system where the console encoding is set to OEM 737 (the default I guess).
    • Notepad++ has to be configured to run in single-instance mode.
    • You need to be a member of the ADMINISTRATORS Windows user group or at least to be able to provide credentials of an administrative user account when User Account Control dialogs pop up.
    • You need to install the NppExec plugin.

    Introduction

    Let’s start with the last point. The NppExec plugin is obtainable via old Plugin Manager as well as via new Plugins Admin. With this plugin you are able to write simple scripts to automate Notepad++ and to start external programs/commands. This workaround requires two of such scripts and an external batch script.

    The batch script I prefer to store in a directory belonging to the plugin. The path to this directory depends on your version of Notepad++. In the script below I use a path that is valid for Notepad++ up to version v7.5.9, it’s <Npp-installation-dir>\plugins\NppExec\OpenFileAtLine where <Npp-installation-dir> is replaced with the variable $(NPP_DIRECTORY). You can use this path in newer versions of Notepad++ too, but I recommend to use $(SYS.PROGRAMDATA)\Notepad++\plugins\NppExec\OpenFileAtLine from version 7.6.2 upwards.

    Furthermore I want to discourage you to use Notepad++ v7.6 or v7.6.1. If you want to use a version newer than v7.5.9 use at least v7.6.2.

    Step by Step

    1. Install the NppExec plugin via Plugin Manager (in Notepad++ v7.5.9 or older) or via _ Plugins Admin_ (in Notepad++ v7.6.2 or newer).
    2. Open a new file in Notepad++ and copy the following code to it:

    @echo off & setlocal
    
    for /f "tokens=1-3 delims=:" %%a in ("%~2") do (
      start "" "%~1\notepad++.exe" -n%%c "%%a:%%b"
    )
    

    3. Save the file to a directory of your harddisk where you have write permissions and name it OpenFileAtLine.cmd.
    4. Create the directory <Npp-installation-dir>\plugins\NppExec\OpenFileAtLine (Notepad++ v7.5.9 and older) or %ProgramData%\Notepad++\plugins\NppExec\OpenFileAtLine (Notepad++ v7.6.2 and newer) and move the file OpenFileAtLine.cmd created above to this directory. This may require administrator rights.
    5. Click on (menu) Plugins -> NppExec -> Execute. The Execute... dialog opens.
    6. Copy the following code to the input field entitled with Command(s):.

    npp_console keep
    npp_console disable
    npe_console -- m-
    npe_console -- o1
    npe_console -- i0
    
    npe_console -- v+
    
    cmd /c "for /f "tokens=1-3 delims=:" %a in ('findstr /ins /c:"$(CURRENT_WORD)" "$(CLIPBOARD_TEXT)" 2^> NUL') do @echo %a:%b:%c"
    
    npp_sendmsg WM_COMMAND IDM_VIEW_SWITCHTO_OTHER_VIEW
    sci_sendmsg SCI_DOCUMENTEND
    sci_sendmsg SCI_GETCURRENTPOS
    sci_sendmsg SCI_LINEFROMPOSITION $(MSG_RESULT)
    set $(LineIdx) = $(MSG_RESULT)
    sel_settext $(OUTPUT)
    sci_sendmsg SCI_GOTOLINE $(LineIdx)
    
    npe_console -- v-
    
    npe_console -- o0
    npe_console -- i0
    npp_console enable
    
    npp_switch "$(FULL_CURRENT_PATH)"
    

    7. Click on Save.... In the small dialog box opening now, in the combobox type SearchAsOEM and click the Savebutton.
    8. When back in the Execute... dialog, copy the following code to the input field entitled with Command(s):. Please note that you maybe have to change the path to the OpenFileAtLine.cmd command. It should point to the file of step 4.

    npp_console keep
    npp_console disable
    npe_console -- m-
    npe_console -- v-
    npe_console -- o1
    npe_console -- i0
    
    set $(CurSelection) = $(CURRENT_WORD)
    npp_sendmsg WM_COMMAND IDM_VIEW_SWITCHTO_OTHER_VIEW
    cmd /c ""$(NPP_DIRECTORY)\plugins\NppExec\OpenFileAtLine\OpenFileAtLine.cmd" "$(NPP_DIRECTORY)" "$(CurSelection)""
    
    npe_console -- o0
    npe_console -- i0
    npp_console enable
    
    npp_switch "$(FULL_CURRENT_PATH)"
    
    unset CurSelection
    

    9. Click on Save.... In the small dialog box opening now, in the combobox type OpenFileAtLine and click the Savebutton.
    10. Click the OK button to close the Execute... dialog.
    11. Click on (menu) Plugins -> NppExec -> Advanced Options.
    12. In the group box Menu items tick the option Place to the Macros submenu.
    13. In the group box Menu item go to the combobox Associated script and select SearchAsOEM.
    14. In the input field Item name type Search as OEM (note the blanks!) and click the button Add/Modify.
    15. In the group box Menu item go to the combobox Associated script and select OpenFileAtLine.
    16. In the input field Item name type Open File at Line (note the blanks!) and click the button Add/Modify.
    17. Click the button OK to close the dialog. You will be hinted that a restart is required in order to apply your changes. Do that now.

    First try

    After restarting Notepad++ you are able to use the following workflow:

    1. Notepad++ opened an empty file named new 1 for you. Open an additional empty file and click on (menu) View -> Move/Clone Current Document -> Move to Other View. Now you should have two empty views side by side in your Notepad++ window.
    2. Go to the right view and type the path of the directory where the documents you want to search in are stored and press ENTER at least one times to start a new line.
    3. Mark the path to your documents directory and press CTRL+C to copy it to the clipboard.
    4. Switch to the left view and type the search term you want to search for. Place the cursor somewhere inside the word or select all words in case you have typed more than one word.
    5. Click (menu) Macro -> Search as OEM.
    6. In the right view the paths to the files are added where your search term has been found in, together with the line number, separated with colons.
    7. In the right view’s line numbers margin click on the line number of a line with a search result. This will select the whole line.
    8. Click on (menu) Macro -> Open File at Line.
    9. In the left view the selected file should be opened. The line where your search term has been found is scrolled into sight and marked as the current line.

    Improvements

    You could add two entries to the context menu of Notepad++ which do the same like the clicks to Search as OEM and Open File at Line. This can speed up your workflow.

    To do that click on (menu) Settings -> Edit Popup ContextMenu and add the following lines to the file that will be opened:

    <Item PluginEntryName="NppExec" PluginCommandItemName="Search as OEM" ItemNameAs="A-Menu-Item-Name-Of-Your-Choice" />
    <Item PluginEntryName="NppExec" PluginCommandItemName="Open File at Line" ItemNameAs="A-Menu-Item-Name-Of-Your-Choice" />
    

    Save the file and restart Notepad++. After that you can right click on selected text and will find your menu items in the context menu.



  • Addendum:

    The above workaround searches case insensitive. To search case sensitive replace findstr /ins in the script under step 6. with findstr /ns.



  • Correction:

    I made a mistake. Under First try -> step 2. I wrote you should type the path to the directory where the documents you want to search in are stored. Indeed you have to type the path including a search mask, e.g. *.txt.

    Additionally it would be better in the script under Step by Step -> step 6. to write findstr /in for a case insensitive and findstr /n for a case sensitive search. The option s would perform a recursive search in all subdirectories which can take a long time.

    Sorry for the inconvenience.



  • I really appreciate this huge effort.
    I am off to a trip so will try your workaround probably by the end of next week. Needless to say your post doubled this thread’s view count!
    I will keep you posted.
    Gig thanks.



  • I first upgraded from NPP 7.5.9 to 7.6.2 which was planned anyway.

    Then I almost made your workaround work!

    The right pane got filled with the file list + line numbers as expected but NppExec OpenFileAtLine did not seem to react. However, by directly running the command below from a Windows Command prompt (whilst replacing the variable part) it did work fine opening a new editing tab in NPP and placing the cursor at the proper line…

    c:\ProgramData\Notepad++\plugins\NppExec\OpenFileAtLine\OpenFileAtLine.cmd “c:\Program Files (x86)\Notepad++” “f:\xpac\xm\myfile.prg:2109”

    In any case I now know what your workaround does and it is good to know that it exists. What I was looking for however is a “Find results” output with highlighted matches where you could open the corresponding file from.

    I will try your suggestion of addressing Notepad++ GitHub repository and if unsuccessful I may have to come back and perhaps start with your workaround and proceed from there.

    Many thanks for your time and the thoroughness of your reply.



  • @Con-Sargo

    To get the content of the whole line where your search term has been found to be printed after the file name/line number tuple, replace line 9 of the script under Step by Step point 6. with the following code:

    cmd /c "for /f "tokens=1-3* delims=:" %a in ('findstr /in /c:"$(CURRENT_WORD)" "$(CLIPBOARD_TEXT)" 2^> NUL') do @echo %a:%b:%c:%d"
    

    To ensure that OpenFileAtLine.cmdworks properly, be sure that a complete line with a search result is selected (like explained in point 7. under First Try). The information which file to open and to which line to scroll is passed over to Notepad++ via the selected text in the file list view.

    What I was looking for however is a “Find results” output with highlighted matches where you could open the corresponding file from.

    As I’ve said, it’s a workaround which is not that comfortable like having a real search result window but it can be helpful for you now. If and when the required changes to work with a real search result window are included into the Notepad++ code base is unknown and can take a long time.



  • Thanks for that tip too. I will try it.


Log in to reply