Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing Notepad++ when the last document is closed

Tags:

notepad++

I'm using notepad++ for couple of months now and gone through all the settings but can't find a way to make the npp to close when I close the last tab. It would always start a new empty document.

Any ideas how can I make the npp to close upon closing the last document?

like image 976
JunkMale Avatar asked Apr 27 '12 00:04

JunkMale


3 Answers

The latest update of Notepad++ includes the functionality of closing the application after closing the last tab.

To update Notepad++, go to ?>Update Notepad++ and follow the install wizard.

When update is complete, you will have the option within the Settings>Preferences menu to "Exit on close the last tab" (under Tab Bar input group).

like image 150
Sebastian Avatar answered Sep 28 '22 18:09

Sebastian


This is totally based on ufo's code. Only that it works when you close the last document be it new or not, and it doesn't freeze npp.

For the sake of brevity, here follows the steps again:

  1. Install the Python Script plug-in
  2. On Plugins > Python Script > Configuration change Initialisation mode from LAZY to ATSARTUP
  3. Open up ...\Notepad++\plugins\PythonScript\scripts\startup.py and place following code at the end of it.
  4. Save and restart Npp to load the script.

    from threading import Timer
    
    def shutdownNppOnLastFileClosed(args):
    
        def closeNpp():
            notepad.menuCommand(MENUCOMMAND.FILE_EXIT)
    
        files = notepad.getFiles()
        if len(files) == 2:
            t = Timer(0.1, closeNpp)
            t.start()
    
    notepad.callback(shutdownNppOnLastFileClosed, [NOTIFICATION.FILEBEFORECLOSE])
    
like image 40
alexbrina Avatar answered Sep 28 '22 16:09

alexbrina


If you're familiar with Python, you could try the Python Script plug-in for N++. You would set up a callback script for the document-closed-event. Inside it do some iteration through all opened docs, and when there's only 1 left with no text in it, then terminate N++.

Personally I mapped the keys "Alt + x" to "Exit" Notepad++, which is easier to grap then the usually working "Alt + F4".

/EDIT

I actually quite liked your idea, so I've quickly tried it myself. It took ~20 minutes to figure it out. Here's a complete solution:

  1. Install the plug-in Python Script (link above)
  2. Go to Plugins > Python > Configuration and change Initialisation mode from LAZY to ATSARTUP
  3. Open up "...\Notepad++\plugins\PythonScript\scripts\startup.py" and place following code at the end of it: Seems like the code tags don't work below a numbered list, so click me to see the code
def shutdownNppOnLastFileClosed(args):
    import os
    files = notepad.getFiles()
    # there are always at least 2 'buffers' open in N++
    if len(files) == 2:
        currentBufferID = notepad.getCurrentBufferID()
        for (filename, bufferID, index, view) in files:
            if os.path.exists(filename):
                break
            notepad.activateBufferID(bufferID) 
            if editor.getLength() > 0:
                break
            # TODO: just to be on the safe side - if we
            # reached here, we actually should also check
            # if the 2 left empty buffers are not unsaved,
            # but I couldn't find a way to do that.
        else:
            # following 'menuCommand' looks cleaner than
            # the 'sys.exit' but it currently deadlocks N++:
            #notepad.menuCommand(MENUCOMMAND.FILE_EXIT)
            sys.exit(0)
        notepad.activateBufferID(currentBufferID)
notepad.callback(shutdownNppOnLastFileClosed, [NOTIFICATION.FILECLOSED])
like image 40
ufo Avatar answered Sep 28 '22 18:09

ufo