The swapfile full path is available through
swapname() which takes a buffer name. The current buffer name is available with
bufname() with no arguments. So this prints the swapfile path of the current buffer
If you want just the filename you'd use
fnamemodify() along with the filename modifier
:t (for "tail"). And if you want to remove the
.swp file extension add
:r (for "root") to that. Putting it all together this will print just the root of the swapfile:
echo fnamemodify(swapname(bufname()), ":t:r")
So, following your example, you could then create a string containing a path, the swapname and a new extension like so:
let metafile = $HOME . '/.cache/TreasureChest/ . fnamemodify(swapname(bufname()), ":t:r") . '.txt'
(I'm just storing it in a variable to illustrate things here. You can manipulate it however you need to.)
Note that I'm assuming a configuration with
'directory' set and ending with double slash (see
:h 'directory') because doing this encodes the edited file's path in the filename itself and thus the swapfile name alone will be unique. If you instead are configured to store swap files in the same directory as the edited file then you may get filename collisions. In that case you may want to use the full swapfile path as a sub-directory of your metadata file storage directory which means you wouldn't want to strip the path:
let metafile = $HOME . '/.cache/TreasureChest . fnamemodify(swapname(bufname()), ":r") . '.txt'
Of course, if you're using this scheme you don't even need
swapname() you can just manipulate the the edited file path directly with, for instance,
An alternative to using sub-directories would be to do a similar encoding to that of Vim, i.e. when
'directory' is set and ends with
// Vim takes the full path of the edited file and substitutes percent sign for each forward slash. (The swapfile with this name is then stored in a temporary directory.)
So, using the aforementioned
expand(), that would look something like...
let metafile = $HOME . '/.cache/TreasureChest/ .
\ substitute(expand('%:p'), '/', '%', 'g') . '.txt'