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?
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).
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:
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])
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:
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With