10

Is it possible to remember what files are opened in a session of gedit, by gedit itself or its plug-ins, just similar to what the session manager of Firefox does?

So that next time restarting gedit, I can reopen and continue to work on the files opened in the last session.

12

There's also a Session Saver plugin to save and restore working sessions.

1
  • Unfortunately it's buggy on F22 with GEdit 3.16.4 : / – Zelphir Kaltstahl Oct 1 '16 at 0:27
5

This is an old question, but it is the first result in google when searching for a plugin to re-open previously opened files. It seems that no one gave a very specific answer, though.

gedit-restore-tabs is a nice plugin that was written for G-Edit 3.12 but works in later versions. I used it in the past, but couldn't remember where I had gotten it. I found it again by going to this page on the gnome.org wiki and searching through the lists linked there.

4
  • Does it work for you? I'm running Xubuntu with gedit 3.14.3 and though I can install it, it doesn't seem to work at all. – Eyal Feb 24 '16 at 18:37
  • It's working perfectly in gedit 3.10.4 on Ubuntu 15.10. I had it working in gedit 3.13 but my motherboard died and I rebuilt the computer and did a fresh install of Ubuntu and just went with the default gedit that installed with it. – whitebeard Feb 24 '16 at 20:39
  • It has a bug in 3.18 but still usable. – Tgr Jan 9 '17 at 3:44
  • Doesn't work at all in gedit 3.10.4 on Ubuntu 14.04 (at least with XFCE). It recreated the number of tabs correctly, but failed to open files they contained (and the first tab had the loading icon on it, running forever). – Przemek D Jun 28 '18 at 8:27
3

Just hit "File" on Gedit's menu. A number of files used previously will appear in the drop-down menu numbered 1, 2, 3...

The Help menu states: "The application records the paths and filenames of the five most recent files that you edited and displays the files as menu items on the File menu. You can also click on the {down arrow} icon on the toolbar to display the list of recent files."

3
  • 1
    I'm using gedit 3.14.3, and I don't have a "File" menu anymore. Is this functionality missing from the latest versions of gedit? – Eyal Apr 30 '15 at 13:58
  • This is not an answer it describes exactly what has stopped happening in my installation of gedit. How do you restore this functionality to gedit? – Thompson Dawes Apr 30 '17 at 22:15
  • 2
    This was a perfectly valid answer in 2011 when it was provided. Gedit has moved on since then and the user interface is completely different. It now has a "clean" look which may or may not work for you. It certainly didn't work for me, so I too have moved on - to Pluma - which has a more traditional mode of operation. – CentaurusA May 1 '17 at 16:27
1

In version 3.18.3 (or may be earlier) you may use plugin "Quick open". When it is enabled you may see recently opened files by using "File -> Quick Open".

1

A little late to the party but, I wrote a python program to reopen all my applications on reboot. This includes gedit and the last five opened files in their own tabs.

Here's the snippet from the program:


#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''

REQUIRES:

   sudo apt install xdotool
'''
    
from __future__ import print_function           # Must be first import
import os
import time

OTHERS_TIME = 3                                 # Firefox, etc. to load up
SERVER_TIME = 1.5                               # gnome-terminal-server time
BASHRC_TIME = 1.5                               # Seconds to load ~/.bashrc
WINDOW_TIME = .5                                # Seconds fpr window to appear

def launch_command(ext_name):
    ''' Launch external command in background and return PID to parent.
        Use for programs requiring more than .2 seconds to run.
    '''

    all_pids = get_pids(ext_name)       # Snapshot current PID list
    all_wins = get_wins(all_pids)       # Snapshot of windows open
    new_pids = all_pids
    new_wins = all_wins
    sleep_count = 0                     # Counter to prevent infinite loops

    os.popen(ext_name)                  # Run command in background

    while new_pids == all_pids:         # Loop until new PID is assigned
        new_pids = get_pids(ext_name)   # Snapshot current PID list
        if sleep_count > 0:             # Don't sleep first time through loop
            time.sleep(.005)            # sleep 5 milliseconds
        sleep_count += 1
        if sleep_count == 1000:         # 10 second time-out
            print('launch_ext_command() ERROR: max sleep count reached')
            print('External command name:',ext_name)
            return 0, 0

    pid_list = list(set(new_pids) - set(all_pids))
    if not len(pid_list) == 1:
        print('launch_command() ERROR: A new PID could not be found')
        return 0, 0

    time.sleep(WINDOW_TIME)             # Give time for window to appear
    new_wins = get_wins(all_pids)       # Snapshot of windows open
    win_list = list(set(new_wins) - set(all_wins))
    if not len(win_list) == 1:
        #print('launch_command() ERROR: New Window ID could not be found')
        #suppress error message because we aren't using window ID at all
        return int(pid_list[0]), 0

    # Return PID of program we just launched in background
    return int(pid_list[0]), int(win_list[0])


def get_pids(ext_name):
    ''' Return list of PIDs for program name and arguments
        Whitespace output is compressed to single space
    '''
    all_lines = []
    # Just grep up to first space in command line. It was failing on !
    prog_name = ext_name.split(' ',1)[0]
    all_lines = os.popen("ps aux | grep -v grep | grep " + \
                        "'" + prog_name + "'").read().strip().splitlines
    PID = []
    for l in all_lines():
        l = ' '.join(l.split())         # Compress whitespace into single space
        PID.append(int(l.split(' ', 2)[1]))

    return PID


def get_wins(all_pids):
    ''' Return list of all windows open under PID list
        Currently unnecessary because we work on active window '''
    windows = []
    for pid in all_pids:
        all_lines = os.popen('xdotool search --pid ' + str(pid)). \
                             read().strip().splitlines
        for l in all_lines():
            windows.append(int(l))

    return windows



def gedit():

    last_modified_files = gedit_recent_files()
    command = 'gedit '
    for f in last_modified_files:
        # Add each file name to parameter list passed to `gedit`
        command += '"' + f + '" '

    # Open gedit with last five modfied files. '&' = run in background
    command=command+' &'
    active_pid, active_win = launch_command(command)
    if active_pid == 0:
        print("ERROR launching", command, \
        "Aborting 'alienstart' script")
        exit()


def gedit_recent_files():
    ''' Get list of gedit 5 most recent files:
    
grep --no-group-separator -B5 'group>gedit' ~/.local/share/recently-used.xbel | sed -n 1~6p | sed 's#  <bookmark href="file:///#/#g' | sed 's/"//g'

/home/rick/python/mmm added=2020-05-02T15:34:55Z modified=2020-11-19T00:43:45Z visited=2020-05-02T15:34:56Z>
/home/rick/python/mserve added=2020-07-26T16:36:09Z modified=2020-11-28T01:57:19Z visited=2020-07-26T16:36:09Z>

    '''
    command = "grep --no-group-separator -B5 'group>gedit' " + \
              "~/.local/share/recently-used.xbel | " + \
              "sed -n 1~6p | sed 's#  <bookmark href=" + '"' + \
              "file:///#/#g' | " + "sed 's/" + '"' + "//g'"

    recent_files = []
    times = []
    all_lines = os.popen(command).read().strip().splitlines
    uniquifier = 1                  # gedit can give all open files same time
    for l in all_lines():
        fname = l.split(' added=', 1)[0]
        trailing = l.split(' added=', 1)[1]
        modified = trailing.split(' modified=', 1)[1]
        modified = modified.split('Z', 1)[0]
        # TODO: 2038
        d = time.strptime(modified, '%Y-%m-%dT%H:%M:%S')
        epoch = time.mktime(d)
        epoch = int(epoch)
        recent_files.append(fname)

        try:
            times.index(epoch)
            # gedit has given multiple files the same modification time
            epoch += uniquifier
            uniquifier += 1
        except:
            pass                    # Not a duplicate time
        times.append(epoch)

    N=5
    top_files = []
    if N > len(times):
        # Less than 5 most recent files in list
        N = len(times)
        if N == 0:
            # No most recent files in list
            return top_files            # return empty list

    # Store list in tmp to retrieve index
    tmp=list(times)
    # Sort list so that largest elements are on the far right
    times.sort()

    #print ('5 most recent from lists and indices')
    for i in range(1, N+1):
        top_files.append(recent_files[tmp.index(times[-i])])

    return top_files


if __name__ == "__main__":

    gedit()

The larger REAL program is more complicated because it opens gnome-terminal, changes directory, renames tabs and move windows to one of three monitors. Sometimes the larger program launches programs in the background, sometimes in the foreground. It polls to ensure one program is running (or finished running) before proceeding to the next program.

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.