2

My machine is slow, so whenever I open new file(by double click) I would like to have the previously opened tab(s) auto closed.

In Notepad++ 7.5.7, I am opening a folder as a workspace, and every time I open a new file, I need to right click and select close all but this, is there a way to do that automatically from settings?

enter image description here

In other words, when you open a new file, the others gets closed automatically, only one opened file(tab) at a time.

I tried limiting recent file history to one, but it is not the key.

Edit

I think the idea is to limit the number of opened tabs to 1, but how to do that?

  • I don't think opening 2 files is that much slow. What's the spec of your PC? – phuclv Mar 12 at 6:32
  • Mine is i5-4Gb, but I am switching from VS Code, I can not afford any lagging. With SQL Server instances running among others(Eclipse containers : tomcat), everything is slow :). – SourceCode Mar 12 at 15:44
1

I only saw three possible ways of solving this problem; the first two have been rejected; maybe the third will work.

  1. 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.

  2. 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.

  3. 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.

# encoding=utf-8
"""in response to https://superuser.com/questions/1412875"""
from Npp import *

def su1412882_OnOpen_callback(kwargs):
    """this is the function that will be called when a new file is opened"""
    notepad.activateBufferID(kwargs['bufferID'])
    notepad.closeAllButCurrent()

def register_OnOpen_callback():
    """Registers the OnOpen callback"""
    unregister_OnOpen_callback()    # starts by making sure there are none registered
    notepad.callback(su1412882_OnOpen_callback, [NOTIFICATION.FILEOPENED])

def unregister_OnOpen_callback():
    """Unregisters the OnOpen callback - used to make sure that running the script twice doesn't register two instances of the same callback"""
    notepad.clearCallbacks(su1412882_OnOpen_callback)

if __name__ == '__main__': register_OnOpen_callback()

One time:

  1. Install PythonScript (using Plugins Admin in v7.6.3 or later, or using Plugin Manager plugin in v7.5.9 or earlier), restart Notepad++
  2. Plugins > Python Script > New Script. Name it "closeAllButRecent.py" in the suggested default directory
  3. Paste the code above into the file, and save.
  4. Plugins > Python Script > Configuration..., change from Initialization: LAZY to ATSTARTUP.
  5. Plugins > Python Script > Scripts, then ctrl+click on startup.
  6. Add the following lines to the end of the startup.py, and save.
import closeAllButRecent
closeAllButRecent.register_OnOpen_callback()
  1. 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.

  1. Close all instances of Notepad++, and reload one single instance of Notepad++ (this makes sure that any pending settings-changes have taken hold)
  2. Edit %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="" />
        </Macro>
  1. Save. Exit Notepad++. Reload notepad++.
  2. Running that macro (via Macros > Close All then Open New) will close all the open files, then pop up the File > Open dialog
  3. 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.

  • So, you're closing Npp then reopen it then open a file. That doesn't seem simpler than open a file then close all but active. – Toto Mar 10 at 19:17
  • @Toto, if you're talking about my first paragraph: that was written in response to the unedited version of the OP, which didn't mention automatically closing the previous file (and implied, to me, that the OP was just asking for not remembering previous files when notepad++ was first opened) – PeterCJ Mar 10 at 19:28
  • 1
    Thank you, for the smart and only possible idea, writing a plugin, I got the same idea then I installed Notepad++ _c# library, but I stopped there since I have no idea how to access the currently opened tabs, and also the event of opening new one. – SourceCode Mar 12 at 15:37
1

Menu: Settings > Shortcut Mapper

enter image description here

then choose the shortcut you want

  • Thank you, I can do that by simply right click on the tab, but I want something automaticaly by simply opening a new file. – SourceCode Mar 10 at 18:28
  • 1
    @SourceCode: I see, but I don't think it is possible to do two actions with a single shortcut.You may have more info at notepad-plus-plus.org/community – Toto Mar 10 at 19:11
  • No shortcut is needed here, I want to tell Notepad++ to close other opened tabs when I select a file(double click). opens only the active tab. – SourceCode Mar 11 at 14:07
  • Since @SourceCode's most recent comment eliminated my shortcut-workaround as a possibility, I've come up with my third and final alternative: registering an event for the file-open event, using PythonScript. This mimics the restrictions that SourceCode described, to the best of my ability to understand. This is shown as option THREE in my updated answer. Good luck. – PeterCJ Mar 12 at 2:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.