Intrepid has asked for the wisdom of the Perl Monks concerning the following question:

This is going to seem like a very esoteric line of inquiry, I suspect, but I will put it out here for the fine monks and nuns to give me feedback.

I use a computer with Windows 10 and with Cygwin installed (as I often mention on PMo). I have this computer and 5 other computers to configure for file locations under my home dir for the vim editor, meaning places to keep backup files and to keep swap files. All those 5 systems run Debian or some derivative distro of Debian Gnu/Linux. So things as they stand will be relatively predictable when I get around to configuring them. Yes, I could do this "by hand" on each system, but automating such a repetitive chore is exactly one of the compelling reasons Perl exists.

Here are the outputs of my script using File::XDG at this stage of writing it (see the code below):

According to File::XDG on Cygwin, the following directories would be used for "vim":

Config for app vim is   C:/Users/somia/config/vim
Data for app   vim is   C:/Users/somia/AppData/share/vim
Cache for app  vim is   C:/Users/somia/cache/vim

According to File::XDG on MSWin32, the following directories would be used for "vim":

Config for app vim is   C:/Users/somia/AppData/Local/.config/vim
Data for app   vim is   C:/Users/somia/AppData/Local/.local/share/vim
Cache for app  vim is   C:/Users/somia/AppData/Local/.cache/vim

According to File::XDG on Linux, the following directories would be used for "vim":

Config for app vim is   /home/somian/.config/vim
Data for app   vim is   /home/somian/.local/share/vim
Cache for app  vim is   /home/somian/.cache/vim
Pretty unpredictable, huh! Note in particular the distinction between the Cygwin results and the MSWin results. I could use CygwinPerl for this, or Strawberry. In the end, maybe it's basically an esthetic choice. Which looks better to you? I'm leaning towards the choice made by Strawberry (Win32) perl.

The Code

EDIT

The original code I posted - unfinished, placed in READMORE tags:

#!/usr/bin/env perl use strict; use v5.18; use utf8; use warnings; =head1 SYNOPSIS perl Emplace-XDG-dirs =cut use Carp qw(carp croak); use Try::Tiny; use File::XDG 1.00; use File::Path qw(mkpath); use File::Basename; my ($appName, $XDGUser, $XDGData, $XDGCache); $appName = 'vim'; my $xdgEmp = File::XDG->new( name => $appName , api => 1 ); $XDGUser = $xdgEmp->config_home; $XDGData = $xdgEmp->data_home; $XDGCache = $xdgEmp->cache_home; say <<EOFILE; According to File::XDG the following directories would be used for "vi +m": EOFILE say sprintf "Config for app $appName is\t%s", $XDGUser; say sprintf "Data for app $appName is\t%s", $XDGData; say sprintf "Cache for app $appName is\t%s", $XDGCache; print q[-] x 88; print $/;

The (probably) final code

#!/usr/bin/env perl # Last modified: Tue May 27 2025 03:02:52 PM -04:00 [EDT] use strict; use v5.18; use utf8; use warnings; =head1 SYNOPSIS perl Emplace-XDG-dirs =cut use File::XDG 1.00; use File::Spec; use File::Path qw(mkpath rmtree); use subs qw/tellMe/; my ($XDGUser, $XDGData, $XDGCache); my $appName = 'vim'; my $xdgEmp = File::XDG->new( name => $appName , api => 1 ); $XDGUser = $xdgEmp->config_home; $XDGData = $xdgEmp->data_home; $XDGCache = $xdgEmp->cache_home; my @branches = (File::Spec->catdir($XDGData => 'backups'), File::Spec->catdir($XDGData => 'swapfiles')); say "We could make these dirs for you:"; say join qq[\n]=>@branches, ''; if (tellMe("making those dirs for $appName")) { mkpath (@branches, {verbose => 'true', mode => 0775}); } else { say "No? OK, aborting now"; } =head2 Vim settings Put in our .vimrc config file: set backup set backupcopy=auto set backupdir= ... (dir created by script) set directory= ... (dir created by script) =cut sub tellMe { my $gummy = $_[0]; my $ans = "Y"; printf "Do you want proceed with: %s? [Y/n]\n", $gummy; chomp ($ans = <STDIN>); if ($ans =~/Y|y/) { return 1; } elsif ($ans eq '') { return 1; } else { return 0; } } # vim: ft=perl et sw=4 ts=4 :

Thanks for your interest!

May 27, 2025 at 19:04 UTC

A just machine to make big decisions
Programmed by fellows (and gals) with compassion and vision
We'll be clean when their work is done
We'll be eternally free yes, and eternally young
Donald Fagen —> I.G.Y.
(Slightly modified for inclusiveness)

Replies are listed 'Best First'.
Re: File::XDG on varying platforms
by NERDVANA (Priest) on May 27, 2025 at 14:24 UTC
    Doesn't this depend on vim? e.g. if you have Cygwin's vim, then it would look in the paths reported by cygwin perl, right? And if you have a native Win32 MinGW build of vim, then it would look where Strawberry is indicating?

      That's a perceptive question, NERDVANA. I didn't detail that I use both kinds of vim on this computer: the Cygwin build for the console/term, and the Win32 build (GVim) for a gui vim. And fortunately both vims look for the personalization config file in $HOME/.vimrc. So I only have to maintain one .vimrc file, because I break some rules by setting $HOME in Cygwin (.bashrc) to $USERPROFILE =~s{\\} {/}g (pseudocode) (C:/Users/somia). That lets me have just one home directory (not ~ equals /home/somia for Cygwin), and I don't have to struggle with trying to remember which & where when I'm looking for files. So the single .vimrc file is at C:/Users/somia/.vimrc.

      I could attempt to edit .vimrc with perl code in this script (note the revised script under "EDIT" in my post), but that just seems like too much work and worry (about messing up a precious config file because I'm not expert enough as a Perl coder - yet).

      We got a little bit off-topic here, but I'm glad you asked, Cheers.

      May 27, 2025 at 22:15 UTC

        Interesting. So vim doesn't respect the %APPDATA% on windows anyway. If I were using windows, I think I'd prefer my config files for things like vim to live inside C:\Users\<your user profile>\AppData\Roaming (which I believe is the default for %APPDATA%), so that in cases where I was logging into a domain it would carry across to wherever else I logged in. You might consider adding symlinks around so that this happens. Or just let all the cygwin tools dump files into the root of your user directory if you like...

        But also, I think you want the home directory on cygwin to be /cygdrive/c/Users/somia so that cygwin tools don't get confused by "C:".

        Had similar issues with vim/gvim from Cygwin, WSL and Windows GVim. Cannot exactly remember the solution I found, but I think it was a Cygwin symlink from one expected location to the other.

        Luckily I went out of [Windows] business, so I cannot tell.

        Greetings,
        🐻

        $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
Re: File::XDG on varying platforms
by stevieb (Canon) on May 27, 2025 at 08:52 UTC

    I'm confused. You ask what looks better, but all I see are directory paths. Why not just use File::HomeDir to get the user's home directory regardless of OS, and create your directory structure there?

    use File::HomeDir; my $home_dir = File::HomeDir->my_home; ... do stuff in home dir

    Forget about checking for OS. Just get the home directory of the user regardless of Operating System, create your own sub directory, and dump your content there.

    ...unless I'm missing something obvious here...