Opening Files
-
Hello,
I’m wondering if its possible to force Notepad++ to open additional ffiles to the left of the files that are already open?
Typically, I have several files open in one instance of Notepad++ and each day I have to add the current days files to the instance but they always open at the very end on the right and then i drag each file all the way to the left. I prefer it starting with the current date on the far left of the files open since when i do a search these are the first results i will see then it continues in descending date order.
Is this possible?
Thank you in advance for any help with this.
-
I don’t know if there’s a way to change Notepad++'s behavior of always opening new files on the right, but here’s a script that will sort all open files by the time they were created or the last time they were modified.
The script is self-documenting. Note that it requires PythonScript 3.0.16 or higher, which can be downloaded here.
''' ====== SOURCE ====== Requires PythonScript 3.0.16 or higher (https://community.notepad-plus-plus.org/topic/23039/faq-how-to-install-and-run-a-script-in-pythonscript) Based on this question: https://community.notepad-plus-plus.org/topic/27054/opening-files ====== WARNING!!! ====== This script will take a VERY LONG TIME TO EXECUTE if there are a lot of files open. It takes a few seconds to execute if there are 13 files open, and would probably take several minutes to execute if you have a hundred files open. God forbid that you execute it when you have 1000 files open. ====== DESCRIPTION ====== Can reorder all open files (only in the current view if you have two views open) so that they are sorted by the last time they were modified (or created, user chooses). To sort open files, click Yes to the first of the two prompts that appear when calling this script. Can also restore the order of all open files to their original order before the first time this script was used. To restore the files to their original order, click No to the first prompt and Yes to the second prompt. ====== EXAMPLE ====== Suppose you have the following files open in this order from left to right: foo.txt (created 2022-07-25, modified 2023-08-10) bar.txt (created 2020-05-01, modified 2025-07-20) baz.txt (created 2024-02-01, modified 2024-05-03) Suppose that MOST_RECENT_ON_THE_LEFT is True and SORT_BY_WHAT is set to 'mod'. If the user clicks Yes to the first prompt, the order will now be: bar.txt (created 2020-05-01, modified 2025-07-20) baz.txt (created 2024-02-01, modified 2024-05-03) foo.txt (created 2022-07-25, modified 2023-08-10) If instead MOST_RECENT_ON_THE_LEFT is False and SORT_BY_WHAT is set to 'creation', and the user clicks Yes to the first prompt, the order will now be: bar.txt (created 2020-05-01, modified 2025-07-20) foo.txt (created 2022-07-25, modified 2023-08-10) baz.txt (created 2024-02-01, modified 2024-05-03) Finally, if the user clicks No on the first prompt and Yes on the second, the files will go back to (foo.txt, bar.txt, baz.txt) from left to right. ''' from Npp import notepad from pathlib import Path # change this to False if you prefer the most recently updated on the RIGHT MOST_RECENT_ON_THE_LEFT = True # choose one of these two to sort by one of these characteristics of each file open: # 'modification': last modification time # 'creation': time created SORT_BY_WHAT = 'modification' # if at least this many files are open, warn the user that the script could take a very long time to run THRESHOLD_WARN_MANY_FILES_OPEN = 75 def get_mod_or_creation_time(fname): path_stat = Path(fname).stat() if SORT_BY_WHAT == 'modification': return path_stat.st_mtime else: return path_stat.st_ctime # immutable constant, defined by Notepad++ in the nativeLang.xml file in your installation's app data directory VIEW_TAB_MOVE_TO_END_ID = 10006 current_view = notepad.getCurrentView() files_open = notepad.getFiles() # list of (name, bufferID, index, view) fname_originally_open = notepad.getCurrentFilename() fnames_in_current_view = [name for (name, bufID, idx, view) in sorted(files_open, key=lambda x: x[2]) if view == current_view] def put_tabs_in_order(fname_list): for fname in fname_list: notepad.activateFile(fname) notepad.menuCommand(VIEW_TAB_MOVE_TO_END_ID) def sort_files_by_modtime_MAIN(): if len(files_open) >= THRESHOLD_WARN_MANY_FILES_OPEN and notepad.messageBox( f'You have {len(files_open)} files open. Running this script could take a very long time. Do you still want to run it?', 'Run script even though it could be slow?', MESSAGEBOXFLAGS.YESNO, ) == MESSAGEBOXFLAGS.RESULTNO: return left_or_right = 'left' if MOST_RECENT_ON_THE_LEFT else 'right' result_1 = notepad.messageBox( f'Do you want to order all open files by {SORT_BY_WHAT} time (most recently updated on the {left_or_right})?', 'Order by modification time?', MESSAGEBOXFLAGS.YESNO) if result_1 == MESSAGEBOXFLAGS.RESULTYES: file_modtimes = [(fname, get_mod_or_creation_time(fname)) for (fname, bufID, index, view) in files_open if view == current_view] fnames_in_current_view_by_modtime = [x[0] for x in sorted(file_modtimes, key=lambda x: x[1], reverse=MOST_RECENT_ON_THE_LEFT)] put_tabs_in_order(fnames_in_current_view_by_modtime) else: result_2 = notepad.messageBox( 'Do you want to restore all open files to their original order (before the first time this script was run)?', 'Restore to original order?', MESSAGEBOXFLAGS.YESNO) if result_2 == MESSAGEBOXFLAGS.RESULTYES: put_tabs_in_order(fnames_in_original_order) notepad.activateFile(fname_originally_open) if __name__ == '__main__': try: has_run_sort_files_by_modtime # this will raise a NameError the first time we run the script # get list of files in their original order so we can restore it later except NameError: fnames_in_original_order = fnames_in_current_view has_run_sort_files_by_modtime = True sort_files_by_modtime_MAIN()
-
M Mark Olson referenced this topic on
-
Here’s my script entry, which does exactly this:
open additional ffiles to the left of the files that are already open?
I call the script
MoveJustOpenedTabToExtremeLeft.py
and instructions for use appear in the comments in the source code.# -*- coding: utf-8 -*- from __future__ import print_function # Python2 vestige! ######################################### # # MoveJustOpenedTabToExtremeLeft (MJOTTEL) # ######################################### # note: # This script was developed and tested under Python3 64-bit on unicode (non-ANSI) encoded data. # It may work as-is using Python2 and/or ANSI-encoded data and/or 32-bits, but that would be incidental. # references: # https://community.notepad-plus-plus.org/topic/27054/opening-files # for newbie info on PythonScripts, see https://community.notepad-plus-plus.org/topic/23039/faq-desk-how-to-install-and-run-a-script-in-pythonscript #------------------------------------------------------------------------------- # ensure this file is not executed directly: assert __name__ != '__main__', 'Cannot run this script directly; for testing, could execute this at console >>> prompt: from MoveJustOpenedTabToExtremeLeft import MOVE_JUST_OPENED_TAB_TO_EXTREME_LEFT' # to execute, use this in (e.g.) user startup.py: # from MoveJustOpenedTabToExtremeLeft import MOVE_JUST_OPENED_TAB_TO_EXTREME_LEFT # another execution option would be to copy and then paste that line into the PythonScript console >>> box # note: if running via startup.py, need to make sure that "Initialisation" for "Python Script Configuration" is set to "ATSTARTUP" and not "LAZY". #------------------------------------------------------------------------------- from Npp import * import threading #------------------------------------------------------------------------------- class MJOTTEL(object): def __init__(self): notepad.callback(self.fileopened_callback, [NOTIFICATION.FILEOPENED]) def fileopened_callback(self, args): #print('FILEOPENED:', args) IDM_VIEW_GOTO_START = 10005 threading.Timer(0.05, lambda : notepad.menuCommand(IDM_VIEW_GOTO_START)).start() #------------------------------------------------------------------------------- MOVE_JUST_OPENED_TAB_TO_EXTREME_LEFT = MJOTTEL()
Notes:
-
the script is rather simple-minded: it works for the simple case of opening 1 file at a time. If you attempt to open 2+ at the same time (e.g. drag and drop 2+ files from Explorer into Notepad++, use File > Open and specify 2+ files, etc.), the script causes only 1 of the files to move. If this is a big limitation for functionality, I could look into how to successfully achieve it.
-
the threading stuff seems like it wouldn’t be necessary, but it is. Probably what happens (I didn’t verify) is that the file-opened notification message comes before Notepad++ is entirely done with everything it does to open a file. Delaying (via the threading call) moving the tab allows control to return to Notepad++ to let it finish doing its thing, before the script moves the tab.
-
-
A Alan Kilborn referenced this topic on