Skip to content

Commit c9df1fb

Browse files
saccarosiumchrisbra
authored andcommitted
patch 9.1.0327: No support for using $XDG_CONFIG_HOME
Problem: No support for using $XDG_CONFIG_HOME Solution: optionally source $XDG_CONFIG_HOME/vim/vimrc (Luca Saccarola) fixes: #2034 closes: #14182 Signed-off-by: Luca Saccarola <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent f9f5424 commit c9df1fb

File tree

13 files changed

+359
-12
lines changed

13 files changed

+359
-12
lines changed

runtime/doc/starting.txt

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*starting.txt* For Vim version 9.1. Last change: 2024 Mar 13
1+
*starting.txt* For Vim version 9.1. Last change: 2024 Apr 14
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -811,7 +811,8 @@ accordingly. Vim proceeds in this order:
811811
name. Also see |vimrc-intro|.
812812

813813
Places for your personal initializations:
814-
Unix $HOME/.vimrc or $HOME/.vim/vimrc
814+
Unix $HOME/.vimrc, $HOME/.vim/vimrc
815+
or $XDG_CONFIG_HOME/vim/vimrc
815816
MS-Windows $HOME/_vimrc, $HOME/vimfiles/vimrc
816817
or $VIM/_vimrc
817818
Amiga s:.vimrc, home:.vimrc, home:vimfiles:vimrc
@@ -853,15 +854,16 @@ accordingly. Vim proceeds in this order:
853854
I The environment variable VIMINIT (see also |compatible-default|) (*)
854855
The value of $VIMINIT is used as an Ex command line.
855856
II The user vimrc file(s):
856-
"$HOME/.vimrc" (for Unix) (*)
857-
"$HOME/.vim/vimrc" (for Unix) (*)
858-
"s:.vimrc" (for Amiga) (*)
859-
"home:.vimrc" (for Amiga) (*)
860-
"home:vimfiles:vimrc" (for Amiga) (*)
861-
"$VIM/.vimrc" (for Amiga) (*)
862-
"$HOME/_vimrc" (for Win32) (*)
863-
"$HOME/vimfiles/vimrc" (for Win32) (*)
864-
"$VIM/_vimrc" (for Win32) (*)
857+
"$HOME/.vimrc" (for Unix) (*)
858+
"$HOME/.vim/vimrc" (for Unix) (*)
859+
"$HOME/.config/vim/vimrc" (for Unix) (*)
860+
"s:.vimrc" (for Amiga) (*)
861+
"home:.vimrc" (for Amiga) (*)
862+
"home:vimfiles:vimrc" (for Amiga) (*)
863+
"$VIM/.vimrc" (for Amiga) (*)
864+
"$HOME/_vimrc" (for Win32) (*)
865+
"$HOME/vimfiles/vimrc" (for Win32) (*)
866+
"$VIM/_vimrc" (for Win32) (*)
865867
"$HOME/config/settings/vim/vimrc" (for Haiku) (*)
866868

867869
Note: For Unix and Amiga, when ".vimrc" does not exist,
@@ -1085,6 +1087,44 @@ defaults.vim from your .vimrc, first unlet skip_defaults_vim, as in the
10851087
example above.
10861088

10871089

1090+
*xdg-base-dir* *$XDG_CONFIG_HOME*
1091+
XDG Base Directory Specification ~
1092+
1093+
The XDG Base Directory Specification aims to define a standard location for
1094+
configuration files used by applications. This is mainly done to prevent
1095+
the legacy behavior of dumping everything into the users home directory.
1096+
The specification can be found online at
1097+
https://specifications.freedesktop.org/basedir-spec/latest/
1098+
1099+
The location of this standard configuration directory is configurable by the
1100+
user, using environment variable but should also give fallback in case those
1101+
variables weren't set.
1102+
1103+
This is a not an exhaustive list of those directories:
1104+
Environment var default location Description ~
1105+
`$XDG_CACHE_HOME` $HOME/.cache Ephemiral data files
1106+
`$XDG_CONFIG_HOME` $HOME/.config Configuration files
1107+
`$XDG_DATA_HOME` $HOME/.local/share Persistent data files
1108+
`$XDG_STATE_HOME` $HOME/.local/state State data files
1109+
1110+
Vim will only care of the `$XDG_CONFIG_HOME` directory, the others are not
1111+
(yet) used for its various configuration and state files.
1112+
1113+
*xdg-vimrc*
1114+
Vim, on Unix systems, will look at `$XDG_CONFIG_HOME/vim/vimrc` for its
1115+
configuration (see |vimrc|) but it will source it only if no other
1116+
initialization file is found in `$HOME` or `$HOME/.vim` (thus making this
1117+
feature backward compatible). However, if you want to migrate to use
1118+
`$XDG_CONFIG_HOME/vim/` directory, you will have to move away your `~/.vimrc`
1119+
and `~/.vim/vimrc` file.
1120+
1121+
*xdg-runtime*
1122+
When the |xdg-vimrc| is used the |'runtimepath'| will be modified accordingly
1123+
to respect the |xdg-base-dir|: >
1124+
1125+
"$XDG_CONFIG_HOME/vim,$VIMRUNTIME,/after,$XDG_CONFIG_HOME/vim/after"
1126+
<
1127+
10881128
Avoiding trojan horses ~
10891129
*trojan-horse*
10901130
While reading the "vimrc" or the "exrc" file in the current directory, some

runtime/doc/tags

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ $VIM starting.txt /*$VIM*
1212
$VIM-use version5.txt /*$VIM-use*
1313
$VIMRUNTIME starting.txt /*$VIMRUNTIME*
1414
$VIM_POSIX vi_diff.txt /*$VIM_POSIX*
15+
$XDG_CONFIG_HOME starting.txt /*$XDG_CONFIG_HOME*
1516
$quote eval.txt /*$quote*
1617
% motion.txt /*%*
1718
%:. cmdline.txt /*%:.*
@@ -11371,6 +11372,9 @@ x11-clientserver remote.txt /*x11-clientserver*
1137111372
x11-cut-buffer gui_x11.txt /*x11-cut-buffer*
1137211373
x11-selection gui_x11.txt /*x11-selection*
1137311374
xattr editing.txt /*xattr*
11375+
xdg-base-dir starting.txt /*xdg-base-dir*
11376+
xdg-runtime starting.txt /*xdg-runtime*
11377+
xdg-vimrc starting.txt /*xdg-vimrc*
1137411378
xf86conf.vim syntax.txt /*xf86conf.vim*
1137511379
xfontset mbyte.txt /*xfontset*
1137611380
xfree-xterm syntax.txt /*xfree-xterm*

runtime/doc/version9.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41543,6 +41543,8 @@ and is a work in progress.
4154341543

4154441544
Support for Wayland UI.
4154541545

41546+
Support for the XDG Desktop Specification |xdg-base-dir|
41547+
4154641548
Vim9 script
4154741549
-----------
4154841550
Add support for internal builtin functions with vim9 objects, see

src/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3276,6 +3276,10 @@ source_startup_scripts(mparm_T *parmp)
32763276
&& do_source((char_u *)USR_VIMRC_FILE2, TRUE,
32773277
DOSO_VIMRC, NULL) == FAIL
32783278
#endif
3279+
#ifdef XDG_VIMRC_FILE
3280+
&& do_source((char_u *)XDG_VIMRC_FILE, TRUE,
3281+
DOSO_VIMRC, NULL) == FAIL
3282+
#endif
32793283
#ifdef USR_VIMRC_FILE3
32803284
&& do_source((char_u *)USR_VIMRC_FILE3, TRUE,
32813285
DOSO_VIMRC, NULL) == FAIL

src/option.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,66 @@ set_init_clean_rtp(void)
364364
}
365365
#endif
366366

367+
#ifdef UNIX
368+
/*
369+
* Change 'runtimepath' and 'packdir' to '$XDG_CONFIG_HOME/vim' if the only
370+
* vimrc found is located in '$XDG_CONFIG_HOME/vim/vimrc'.
371+
* In case the '$XDG_CONFIG_HOME' variable is not set, '$HOME/.config' is used
372+
* as a fallback as is defined in the XDG base dir specification:
373+
* <https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html>
374+
*/
375+
static void
376+
set_init_xdg_rtp(void)
377+
{
378+
int opt_idx;
379+
int has_xdg_env = TRUE;
380+
int should_free_xdg_dir = FALSE;
381+
char_u *vimrc1 = NULL;
382+
char_u *vimrc2 = NULL;
383+
char_u *xdg_dir = NULL;
384+
char_u *xdg_rtp = NULL;
385+
char_u *vimrc_xdg = NULL;
386+
387+
vimrc1 = expand_env_save((char_u *)USR_VIMRC_FILE);
388+
vimrc2 = expand_env_save((char_u *)USR_VIMRC_FILE2);
389+
390+
xdg_dir = mch_getenv("XDG_CONFIG_HOME");
391+
if (!xdg_dir)
392+
{
393+
xdg_dir = expand_env_save((char_u *)"~/.config");
394+
should_free_xdg_dir = TRUE;
395+
has_xdg_env = FALSE;
396+
}
397+
vimrc_xdg = concat_fnames(xdg_dir, (char_u *)"vim/vimrc", TRUE);
398+
399+
if (file_is_readable(vimrc1) || file_is_readable(vimrc2) ||
400+
!file_is_readable(vimrc_xdg))
401+
goto theend;
402+
403+
xdg_rtp = has_xdg_env ? (char_u *)XDG_RUNTIMEPATH
404+
: (char_u *)XDG_RUNTIMEPATH_FB;
405+
406+
if ((opt_idx = findoption((char_u *)"runtimepath")) < 0)
407+
goto theend;
408+
409+
options[opt_idx].def_val[VI_DEFAULT] = xdg_rtp;
410+
p_rtp = xdg_rtp;
411+
412+
if ((opt_idx = findoption((char_u *)"packpath")) < 0)
413+
goto theend;
414+
415+
options[opt_idx].def_val[VI_DEFAULT] = xdg_rtp;
416+
p_pp = xdg_rtp;
417+
418+
theend:
419+
vim_free(vimrc1);
420+
vim_free(vimrc2);
421+
vim_free(vimrc_xdg);
422+
if (should_free_xdg_dir)
423+
vim_free(xdg_dir);
424+
}
425+
#endif
426+
367427
/*
368428
* Expand environment variables and things like "~" for the defaults.
369429
* If option_expand() returns non-NULL the variable is expanded. This can
@@ -588,6 +648,7 @@ set_init_1(int clean_arg)
588648
set_options_default(0);
589649

590650
#ifdef UNIX
651+
set_init_xdg_rtp();
591652
set_init_restricted_mode();
592653
#endif
593654

src/os_unix.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ typedef struct dsc$descriptor DESC;
236236
# ifdef VMS
237237
# define USR_VIMRC_FILE "sys$login:.vimrc"
238238
# else
239-
# define USR_VIMRC_FILE "$HOME/.vimrc"
239+
# define USR_VIMRC_FILE "~/.vimrc"
240240
# endif
241241
#endif
242242

@@ -249,6 +249,12 @@ typedef struct dsc$descriptor DESC;
249249
# endif
250250
#endif
251251

252+
#ifndef XDG_VIMRC_FILE
253+
# define XDG_VIMRC_FILE mch_getenv("XDG_CONFIG_HOME") \
254+
? (char_u *)"$XDG_CONFIG_HOME/vim/vimrc" \
255+
: (char_u *)"~/.config/vim/vimrc"
256+
#endif
257+
252258
#if !defined(USR_VIMRC_FILE3) && defined(VMS)
253259
# define USR_VIMRC_FILE3 "sys$login:_vimrc"
254260
#endif
@@ -349,13 +355,19 @@ typedef struct dsc$descriptor DESC;
349355
# ifdef RUNTIME_GLOBAL
350356
# ifdef RUNTIME_GLOBAL_AFTER
351357
# define DFLT_RUNTIMEPATH "~/.vim," RUNTIME_GLOBAL ",$VIMRUNTIME," RUNTIME_GLOBAL_AFTER ",~/.vim/after"
358+
# define XDG_RUNTIMEPATH "$XDG_CONFIG_HOME/vim," RUNTIME_GLOBAL ",$VIMRUNTIME," RUNTIME_GLOBAL_AFTER "/after,$XDG_CONFIG_HOME/vim/after"
359+
# define XDG_RUNTIMEPATH_FB "~/.config/vim," RUNTIME_GLOBAL ",$VIMRUNTIME," RUNTIME_GLOBAL_AFTER "/after,~/.config/vim/after"
352360
# define CLEAN_RUNTIMEPATH RUNTIME_GLOBAL ",$VIMRUNTIME," RUNTIME_GLOBAL_AFTER
353361
# else
354362
# define DFLT_RUNTIMEPATH "~/.vim," RUNTIME_GLOBAL ",$VIMRUNTIME," RUNTIME_GLOBAL "/after,~/.vim/after"
363+
# define XDG_RUNTIMEPATH "$XDG_CONFIG_HOME/vim," RUNTIME_GLOBAL ",$VIMRUNTIME," RUNTIME_GLOBAL "/after,$XDG_CONFIG_HOME/vim/after"
364+
# define XDG_RUNTIMEPATH_FB "~/.config/vim," RUNTIME_GLOBAL ",$VIMRUNTIME," RUNTIME_GLOBAL "/after,~/.config/vim/after"
355365
# define CLEAN_RUNTIMEPATH RUNTIME_GLOBAL ",$VIMRUNTIME," RUNTIME_GLOBAL "/after"
356366
# endif
357367
# else
358368
# define DFLT_RUNTIMEPATH "~/.vim,$VIM/vimfiles,$VIMRUNTIME,$VIM/vimfiles/after,~/.vim/after"
369+
# define XDG_RUNTIMEPATH "$XDG_CONFIG_HOME/vim,$VIM/vimfiles,$VIMRUNTIME,$VIM/vimfiles/after,$XDG_CONFIG_HOME/vim/after"
370+
# define XDG_RUNTIMEPATH_FB "~/.config/vim,$VIM/vimfiles,$VIMRUNTIME,$VIM/vimfiles/after,~/.config/vim/after"
359371
# define CLEAN_RUNTIMEPATH "$VIM/vimfiles,$VIMRUNTIME,$VIM/vimfiles/after"
360372
# endif
361373
# endif

src/testdir/Make_all.mak

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ NEW_TESTS = \
331331
test_wnext \
332332
test_wordcount \
333333
test_writefile \
334+
test_xdg \
334335
test_xxd \
335336
test_alot_latin \
336337
test_alot_utf8 \
@@ -564,6 +565,7 @@ NEW_TESTS_RES = \
564565
test_winfixbuf.res \
565566
test_wordcount.res \
566567
test_writefile.res \
568+
test_xdg.res \
567569
test_xxd.res \
568570
test_alot_latin.res \
569571
test_alot_utf8.res \

src/testdir/dumps/Test_xdg_1.dump

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
|~+0#4040ff13#ffffff0| @73
2+
|~| @73
3+
|~| @73
4+
|~| @73
5+
|~| @73
6+
|~| @73
7+
|~| @73
8+
|~| @73
9+
|~| @73
10+
|~| @73
11+
|~| @73
12+
|~| @73
13+
|~| @73
14+
|~| @73
15+
|~| @73
16+
|~| @73
17+
|~| @73
18+
|r+0#0000000&|c|_|o|n|e| @16|o|n|e| @48
19+
|r|c| @20|.|v|i|m|r|c| @45
20+
|P+0#00e0003&|r|e|s@1| |E|N|T|E|R| |o|r| |t|y|p|e| |c|o|m@1|a|n|d| |t|o| |c|o|n|t|i|n|u|e> +0#0000000&@35

src/testdir/dumps/Test_xdg_2.dump

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
|~+0#4040ff13#ffffff0| @73
2+
|~| @73
3+
|~| @73
4+
|~| @73
5+
|~| @73
6+
|~| @73
7+
|~| @73
8+
|~| @73
9+
|~| @73
10+
|~| @73
11+
|~| @73
12+
|~| @73
13+
|~| @73
14+
|~| @73
15+
|~| @73
16+
|~| @73
17+
|~| @73
18+
|r+0#0000000&|c|_|t|w|o| @16|t|w|o| @48
19+
|r|c| @20|.|v|i|m|/|v|i|m|r|c| @41
20+
|P+0#00e0003&|r|e|s@1| |E|N|T|E|R| |o|r| |t|y|p|e| |c|o|m@1|a|n|d| |t|o| |c|o|n|t|i|n|u|e> +0#0000000&@35

src/testdir/dumps/Test_xdg_3.dump

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
|~+0#4040ff13#ffffff0| @73
2+
|~| @73
3+
|~| @73
4+
|~| @73
5+
|~| @73
6+
|~| @73
7+
|~| @73
8+
|~| @73
9+
|~| @73
10+
|~| @73
11+
|~| @73
12+
|~| @73
13+
|~| @73
14+
|~| @73
15+
|~| @73
16+
|~| @73
17+
|~| @73
18+
|r+0#0000000&|c|_|t|h|r|e@1| @14|t|h|r|e@1| @46
19+
|r|c| @20|.|c|o|n|f|i|g|/|v|i|m|/|v|i|m|r|c| @34
20+
|P+0#00e0003&|r|e|s@1| |E|N|T|E|R| |o|r| |t|y|p|e| |c|o|m@1|a|n|d| |t|o| |c|o|n|t|i|n|u|e> +0#0000000&@35

0 commit comments

Comments
 (0)