Sumatra PDF is a PDF, ePub, MOBI, CHM, XPS, DjVu, CBZ, CBR reader for Windows

Reopen Closed Tab

Is it possible to implement a “reopen” or “undo closed tab” option, similar to the Ctrl+Shift+T function in browsers such as Firefox and Chrome?

(Maybe I’m the only one who would find this helpful, but if you are looking at many of the same PDFs often, e.g., while working on a project, it would be quite appreciated.)

Reasonable request, and not the first time requested, stock answer is why can’t you simply Alt+F+1 or up to 9 and 0


Sorry for late response - glad to hear I’m not the only one thinking about it (apologies for not finding the previous requests).

I suppose “Alt+F+number” is one solution if “Remember open files” is checked, though I find that setting slightly annoying for two reasons:

  1. having all my PDFs open to the last place (say page 24) if I’m opening up the file for the first time in a while is generally not what I want, and

  2. having all of my tabs reopen next time Sumatra is launched (i.e., if I close a window with ten tabs because I’m done with the project, it’s slightly annoying to close all ten to start fresh. Additionally, if you access files on a networked drive, and later open up the program when you no longer have access to it [work v. home], then the program has to freeze and try to access to disconnected drive before giving up, which takes time).

Because of those quirks, I disable “Remember open files”. Maybe if those two functions (remembering recently opened files in the File menu, and remembering open tabs/location) were separated, then the Alt+F+# would be a more workable solution.

Obviously, this is just a minor issue. Just my 2¢.

@ayd

Try in advanced settings RestoreSession = false and on the standard options uncheck remember per document but do check remember opened files

That should give you the list without your “annoyances”

Yup - that seems to work. Thanks!

(Official reopen closed tab shortcut would still be convenient, but this is definitely a usable workaround)

I opened +/-15 files in various time. I will be open more one, and i close unnecessary.
Last opened and last closed - It is not the same;
I accidentally close one book… oh! i don’t remember what of book I close… I don’t remember both name and author… . And I don’t remember when the file is get opened.
And more badly ctrl+W is keyboard shortcut for Close file, and ctrl+1/2… for change view.

i think it is very need feature restore last closed file, or list both last open (it already exists) and last closed

I do agree it can be frustrating to loose the current session of tabs for many reasons and the proximity of W is unfortunate if frequently using nearby keys.

The issue of saving last file closed is still an open issue as shown above https://github.com/sumatrapdfreader/sumatrapdf/issues/788

My best suggestion to consider is to frequently store your current tabs collection especially just before any accidental closure then save again only once you are happy with the next collection.

I outline how that can be achieved (note only in more recent pre-release) in my Alternative method of posting Open files from *MULTIPLE* tabs in Acrobat (or other app)

By combining the concept of save current tabs using ISO date you can restore the current status later. Here at 20 minutes past, I can return to the tabs as they were at 18 minutes past the hour or 19 minutes past. Obviously this list can build up very quickly so it may also be necessary to use an automated means to cull all the old files once no longer required.

1 Like

I use Git ) for save session)

While using multiple windows it could happen the “main instance” gets closed before a new single pdf window.
So it would be nice if each instance gets a extra “last used files” or something similar.

Would also be possible with something like “open last closed window”.

Currently there is only the one session history even when two windows are opened
It would be complex (though not impossible) to keep two sets of history for the one session.

You can use separate copies to have two separate sessions but they clearly will not share a common history, so only useful if you have two distinct separate needs such as Reading main files and alongside other reference material. or one for leisure and one business etc.

Actually whilst testing the two sets of tab states are held in the settings so it may be possible for a script to save the two sets of tab states but I think it would require some time to ensure it worked for all usages.

Am I correct in understanding there is no simple way to save the list of files currently opened in Sumatra, close the app, restart, and re-open the list of files?

I guess a work-around is to write an AutoIT/AutoHotKey script to loop through the list of open files, hit CTRL+D, copy/parse to grab the full pathname, and another script to re-open the whole thing, but I wanted to check before bothering.

Thank you.

There WAS no way even with autoxx to save currently open ffilenames of tabs (they were stored in memory only) but you can look at the method suggested above to store current session data (in recent pre-release version) the long history filenames have two tab spaces but the currently open ones have four tab characters so you can use cmd “find”. That should be easy ? in AHK but console should work just as well although vbs would be quieter.

This is my current script so wont work for most users but you can get the gist

set target=%addins%\..\SumatraPDF.exe

FIND "				FilePath =" "%addins%\..\SumatraPDF-settings.txt" > %temp%\pdflist.txt
echo. >>%temp%\pdflist.txt

FOR /F "tokens=3,2,1 delims=/" %%i in ("%date%") do set IsoDate=%%k-%%j-%%i
FOR /F "tokens=2,1 delims=: " %%m in ("%time%") do set IsoTime=-%%m-%%n-
echo :[Filenames] >"%IsoDate%%IsoTime%SumatraPDFSavedSession.cmd"
FOR /F "tokens=3* skip=2 delims= " %%G IN (%temp%\pdflist.txt) DO echo start "" "%target%" -reuse-instance "%%G %%H" >>"%IsoDate%%IsoTime%SumatraPDFSavedSession.cmd"

Thanks for the script.

Here’s a small script in Python to save the list of files currently open in SumatraPDF:

import re
from datetime import datetime

INPUTFILE = r"C:\Users\joe\AppData\Local\SumatraPDF\SumatraPDF-settings.txt"
OUTPUTFILE = datetime.now().strftime("Sumatra.open.files_%Y-%m-%d_%H.%M.%S.txt")

with open(INPUTFILE,encoding="utf-8") as reader:
	content = reader.read()

#Grab everything from "SessionData"
subset= content[content.index("SessionData"):]

with open(OUTPUTFILE, 'w') as writer:
	files = re.findall('FilePath = (.+)', subset)
	for file in files:
		writer.write(f"{file}\n")

As an easy way to reopen the files after Sumatra was closed, I modified the script per your example to output data to a Windows batch file, but it looks I’m missing something: Windows does launch SumatraPDF, but 1) doesn’t actually open the file, and 2) is stuck on the first line and won’t proceed.

“C:\Program Files\SumatraPDF\SumatraPDF.exe” -reuse-instance “C:\1.pdf”
“C:\Program Files\SumatraPDF\SumatraPDF.exe” -reuse-instance “C:\2.pdf”
etc.

I notice a “%%H” parameter in your post that isn’t defined. What is it?

Thank you.

import re
from datetime import datetime

APP = "C:\Program Files\SumatraPDF\SumatraPDF.exe"
INPUTFILE = r"C:\Users\joe\AppData\Local\SumatraPDF\SumatraPDF-settings.txt"
OUTPUTFILE = datetime.now().strftime("Sumatra.open.files_%Y-%m-%d_%H.%M.%S.cmd")

#Read input file
with open(INPUTFILE,encoding="utf-8") as reader:
	content = reader.read()

subset= content[content.index("SessionData"):]

with open(OUTPUTFILE, 'w') as writer:
	#^ and $ are stripped!
	files = re.findall('FilePath = (.+)', subset)
	for file in files:
		writer.write(f'"{APP}" -reuse-instance "{file}"\n')

is an odd batch file “for …” construct as the pair actually means “%%G …* to end” i.e. all parts of filepath name with spaces. thus can in a bad case be "filename "

What may cause problems in conversion is that a batchfile runs one line at a time

To avoid that stalling each batch file line should begin

start " " "command line ... 1.pdf"
start " " "command line ... 2.pdf"
...

which spawns separate command lines one after the other and then promptly closes the list

Thanks. I’ll read up on how Windows opens documents from a batch file, because using the above, it only opens the first few in the list and ignores the rest.

image

That is odd, It sounds like your shell method is passing one long truncated line (with \newline feeds?) rather than a cmd file with true separate cmd lines\r\n.
It should work line by line without error when the written file.cmd is run from a cmd prompt ??

I first open a cmd window, then run the script. The lines are not truncated. Everything lives on a single line.

It works if I copy/paste/run each line manually.

As a work-around, I’ll just use a second Python script to launch each line.

Thank you.


Edit: I think I found what it is: Filenames must not contain accented characters (“é”, “à”, etc.), or if they do, they must be in ASCII not ANSI/UTF8 or cmd won’t find them because it does no conversion on the fly.

Also, if it fails loading a file on one line because it didn’t find it/couldn’t load because of the accents, it looks like the rest won’t be loaded

For some reason, commands run fine when running each line manually. So the issue occurs specifically when ran from a cmd file.


Edit: Here’s the Python script to read the list of files, and launch SumatraPDF:

import os
import sys
import subprocess

APP = "C:\Program Files\SumatraPDF\SumatraPDF.exe"

arguments = len(sys.argv) - 1
if arguments != 1:
	print ("Must pass file with list of files to open.")
	sys.exit()

with open(sys.argv[1]) as reader:
	for line in reader:
		command = f'"{APP}" -reuse-instance "{line}"'
		print(subprocess.run(f"{command}", shell=True))