Search code examples
sublimetext3sublimetext

Where does sublime store the last cursor position of a previosuly opened file?


I would like to export a list of last opened files (tabs) and the last cursor position for each file, in Sublime editor. I can easily parse the session file called Session.sublime_session located in the installation sub-path: <install path>/Data/Local/ (in Windows), to get the file names. But looking in that file, there are no obvious line numbers to be found. Or did I miss something?

Where is the last cursor position stored?


UPDATE

I now see that it is stored in the JSON field called selection, and is counted by number of characters into the buffer.

...
"selection":
    [
        [
            4353,
            4353
        ]
    ],
...

So the question now become, how can I calculate the line number from this?

Perhaps by writing a regex that counts EOL's (\ns) after reading in X number of bytes. (What if were using different EOL's or usinf UTF-8 vs ASCII?)


Solution

  • With the great help from the answers in this Unix SE question, I managed to patch together something that works pretty good. But it depends on both jq (for windows) and (Cygwin) Bash.

    jq-win64.exe -r '.windows[]|.groups[].sheets[]| "\(.file):\(.settings.selection[0][0])"' Session.sublime_session |sort | sed 's/^\/./\/cygdrive\L&\E/'
    

    I then used this as a basis to extract the number of EOL's and used that to determine the exact cursor line number for each file.

    Hint: use head -c <number> <file> | wc -l.