I only saw three possible ways of solving this problem; the first two have been rejected; maybe the third will work.
Change settings: so far as I have been able to see, the settings can be made close to your requirement, but will not exactly meet your needs. I do not believe settings will work for you.
Use a shortcut as a workaround: this can automate opening one file and closing all others, but will not allow you to use double-clicking the file from Windows Explorer to initiate the process.
Use a scripting plugin to hook actions into the file-open event inside Notepad++. Maybe you won't reject this one, though it takes the most work for you.
Each method is described in greater detail after, with the older ones pushed lower as I've edited the answer.
THREE: Use a scripting plugin
Using the PythonScript plugin (or the LuaScript plugin, or possibly the jN Notepad++ Plugin), it is possible to attach a hook ("register a callback") to the event that's triggered after a file is opened in Notepad++ (in PythonScript, this is called the
NOTIFICATION.FILEOPENED event, on the Notepad object). That function needs to close all files but the recently-opened file.
Actually, there's not much more to it than that. It will only be a few lines of code, even with the wrapper, so I'll write it for you.
"""in response to https://superuser.com/questions/1412875"""
from Npp import *
"""this is the function that will be called when a new file is opened"""
"""Registers the OnOpen callback"""
unregister_OnOpen_callback() # starts by making sure there are none registered
"""Unregisters the OnOpen callback - used to make sure that running the script twice doesn't register two instances of the same callback"""
if __name__ == '__main__': register_OnOpen_callback()
- Install PythonScript (using Plugins Admin in v7.6.3 or later, or using Plugin Manager plugin in v7.5.9 or earlier), restart Notepad++
- Plugins > Python Script > New Script. Name it "closeAllButRecent.py" in the suggested default directory
- Paste the code above into the file, and save.
- Plugins > Python Script > Configuration..., change from Initialization:
- Plugins > Python Script > Scripts, then ctrl+click on
- Add the following lines to the end of the
startup.py, and save.
- Restart Notepad++
From now on, it should close everything except the open file.
Oh, you'll also want Settings > preferences > backup > remember current session to be turned OFF, otherwise when you first open, there might be a second file open (though, after that, if you open a file, it will automatically close any that were opened).
if you want to undo this, just comment out the two lines from
startup.py and restart Notepad++; after that, Notepad++ will allow you to edit multiple files again.
TWO: Use a shortcut as a workaround
Inspired by @Toto's comment under another answer, you can assign a shortcut to multiple actions, if you use a macro.
- Close all instances of Notepad++, and reload one single instance of Notepad++ (this makes sure that any pending settings-changes have taken hold)
%AppData%\Notepad++\shortcuts.xml, and in the
<macros> section, add:
<Macro name="Close All then Open New" Ctrl="no" Alt="no" Shift="no" Key="0">
<Action type="2" message="0" wParam="41004" lParam="0" sParam="" />
<Action type="2" message="0" wParam="41002" lParam="0" sParam="" />
- Save. Exit Notepad++. Reload notepad++.
- Running that macro (via Macros > Close All then Open New) will close all the open files, then pop up the File > Open dialog
- If you so desire, add a keyboard shortcut with Macro > Modify Shortcuts, selecting
Close All then Open New, and pick a shortcut that you can remember.
This will not influence Windows Explorer RClick > Open With Notepad++, or Notepad++'s File > Open. But it will give you a way to automate the process in one menu command or keystroke.
Thanks @Toto for the germ of the idea.
ONE: Can't be done by a setting (ORIGINAL RESPONSE with edits)
If you disable Settings > Preferences > Backup > ☐ Remember current session for next launch, then Notepad++ won't try to remember what files were previously open, and will just start with whatever you launch Notepad++ with -- either a blank file if you just hit your shortcut, or whatever file you're editing if you're launching from Windows Explorer or similar
update: your clarification did help me understand what you want. If you change your shortcut (and any registry entries) to include the
-nosession -notabbar options, then I believe it will behave the way you want: it won't remember previously-open files when you first run Notepad++ (ie, enforcing the disabled
☐ Remember current session for next launch), and it won't have the tabbar, which (I believe) limits you to one open file at a time. I've never used this mode.
You need to be careful, because there's an
asNotepad.xml file (see here and here for details, searching for
asNotepad.xml in each page) which, if found, will run Notepad++ assuming
-multiInst -nosession -notabbar options always enabled -- the
-multiInst allows there to be more than one copy of Notepad++ running (similar to Windows being able to open more than one
notepad.exe simultaneously). Since you're worried about memory, you don't want multiple Notepad++ instances open simultaneously. The other problem with multi-instance mdoe is that if you change settings in one of the open instances of Notepad++, they won't propagate to the others... and the final settings saved to disk will be the settings for whichever instance happens to be closed last.
update 2: I just ran some experiments.
-notabbar isn't sufficient: if you open a second file with
-notabbar active, it will still have both files open, as can be seen from Notepad++'s Window menu, which will still list both files being open. So now my answer becomes:
Sorry, no, you cannot have it automatically close the previously-open file when you open a new file while Notepad++ is currently running.