Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions runtime/defaults.vim
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,6 @@ if has('langmap') && exists('+langremap')
" compatible).
set nolangremap
endif

" XDG Base Directory support
source $VIMRUNTIME/xdg.vim
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can simply be :ru xdg.vim, but I don't want this to be part of defaults.vim

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$VIMRUNTIME is more precise, as we know where the file is.

4 changes: 2 additions & 2 deletions runtime/doc/starting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1125,8 +1125,8 @@ This is not an exhaustive list of those directories:
`$XDG_DATA_HOME` $HOME/.local/share Persistent data files
`$XDG_STATE_HOME` $HOME/.local/state State data files

Vim will only use the `$XDG_CONFIG_HOME` directory, the others are not
(yet) used for its various configuration and state files.
Vim itself will only use the `$XDG_CONFIG_HOME` directory, some of the others
are supported in $VIMRUNTIME/xdg.vim, see the script for details.

*xdg-vimrc*
Vim, on Unix systems, will look at `$XDG_CONFIG_HOME/vim/vimrc` for its
Expand Down
21 changes: 21 additions & 0 deletions runtime/xdg.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
" XDG Base Directory support

if empty($XDG_CONFIG_HOME) | let $XDG_CONFIG_HOME = $HOME."/.config" | endif
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for just chiming in, but I don't think Vim should define these variables as environment variables, as they bleed into processes Vim spawns. Other programs Vim might spawn are expected handle the absence of these environment variables themselves and set their configuration accordingly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I also noticed this. That's one of the reasons I completely re-wrote the script.

Copy link
Contributor Author

@bam80 bam80 Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defining these standard $XDG_ variables here this way should be safe, as if they are not set, XDG mandates default values for them that any other program spawned from Vim should not change. Setting them to the default values is perfectly OK, though.

So defining them here also gives additional convenience for any other vim scripts that goes after.
Thus, I can just rely on $XDG_DATA_HOME in my minpac setup and do not bother to handle the fallback there (as we can handle it once and for all here).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @janvhs 's point is that some programs behave differently if the environment variables are set at all (for example, a program that supports XDG but has it's own defaults that are not XDG might honor the environment but otherwise default to ~/.myprog or something). That makes it not-so-safe.

if empty($XDG_DATA_HOME) | let $XDG_DATA_HOME = $HOME."/.local/share" | endif
if empty($XDG_STATE_HOME) | let $XDG_STATE_HOME = $HOME."/.local/state" | endif

set packpath^=$XDG_DATA_HOME/vim
set packpath+=$XDG_DATA_HOME/vim/after

if isdirectory(expand($XDG_CONFIG_HOME."/vim"))
set viminfofile=$XDG_STATE_HOME/vim/viminfo | call mkdir($XDG_STATE_HOME."/vim", 'p', 0700)

" These options are not essential for XDG, but you might want to set them:
" set backupdir=$XDG_STATE_HOME/vim/backup// | call mkdir(&backupdir, 'p', 0700)
" set directory=$XDG_STATE_HOME/vim/swap | call mkdir(&directory, 'p', 0700)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could someone outline format of the swap file? Does it always contain a full copy of the original?

" set viewdir=$XDG_STATE_HOME/vim/view | call mkdir(&viewdir, 'p', 0700)
" set undodir=$XDG_STATE_HOME/vim/undo | call mkdir(&undodir, 'p', 0700)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

viewdir and undo files I would also consider DATA files

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • By default, directory=.,~/tmp,/var/tmp,/tmp - seems purely temporary. No need to override.
  • For XDG, viewdir is set by Vim already: viewdir=~/.config/vim/view, which suggests it goes to backup anyway. So I don't see the necessity to override it here (even if I would consider it STATE personally).
  • undodir=. is the default, and 'undofile' option is off. That doesn't contradicts with XDG scheme. At least - these files never stayed on my way. No reason to override IMO.

So among all of these write-sensitive options, only viminfofile is clearly out of place for me. That is why I changed it only.
Considering amount of uncertainty, I would prefer progress in small steps, and this is the first step. Will see how it goes.


" let g:netrw_home = $XDG_DATA_HOME."/vim"
" call mkdir($XDG_DATA_HOME."/vim/spell", 'p', 0700)
endif
Loading