Skip to content

Commit 4e7249a

Browse files
committed
patch 9.1.0718: hard to know the users personal Vim Runtime Directory
Problem: hard to guess the Vim Runtime Directory Solution: Set the $MYVIMDIR environment variable to the users personal runtime directory (e.g. ~/.vim on Linux) closes: #15576 Signed-off-by: Christian Brabandt <[email protected]>
1 parent f7b8609 commit 4e7249a

5 files changed

Lines changed: 79 additions & 9 deletions

File tree

runtime/doc/starting.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*starting.txt* For Vim version 9.1. Last change: 2024 Aug 03
1+
*starting.txt* For Vim version 9.1. Last change: 2024 Sep 05
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -828,8 +828,8 @@ accordingly. Vim proceeds in this order:
828828
easy to copy it to another system.
829829

830830
If Vim was started with "-u filename", the file "filename" is used.
831-
All following initializations until 4. are skipped. $MYVIMRC is not
832-
set.
831+
All following initializations until 4. are skipped. $MYVIMRC and
832+
$MYVIMDIR are not set.
833833
"vim -u NORC" can be used to skip these initializations without
834834
reading a file. "vim -u NONE" also skips loading plugins. |-u|
835835

@@ -847,11 +847,13 @@ accordingly. Vim proceeds in this order:
847847
'compatible' is only done later. Add a ":set nocp" command if you
848848
like. For the Macintosh the $VIMRUNTIME/macmap.vim is read.
849849

850-
*VIMINIT* *.vimrc* *_vimrc* *EXINIT* *.exrc* *_exrc* *$MYVIMRC*
850+
*VIMINIT* *.vimrc* *_vimrc* *EXINIT* *.exrc* *_exrc* *$MYVIMRC* *$MYVIMDIR*
851851
c. Five places are searched for initializations. The first that exists
852852
is used, the others are ignored. The $MYVIMRC environment variable is
853853
set to the file that was first found, unless $MYVIMRC was already set
854-
and when using VIMINIT.
854+
and when using VIMINIT. The $MYVIMDIR environment variable is
855+
set to the personal 'rtp' directory, however it is not verified
856+
that the directory actually exists.
855857
I The environment variable VIMINIT (see also |compatible-default|) (*)
856858
The value of $VIMINIT is used as an Ex command line.
857859
II The user vimrc file(s):
@@ -971,7 +973,8 @@ accordingly. Vim proceeds in this order:
971973
The |VimEnter| autocommands are executed.
972974

973975
The $MYVIMRC or $MYGVIMRC file will be set to the first found vimrc and/or
974-
gvimrc file.
976+
gvimrc file while $MYVIMDIR is set to the users personal runtime directory
977+
'rtp' (typically the first entry in 'runtimepath').
975978

976979

977980
Some hints on using initializations ~

runtime/doc/tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ $HOME options.txt /*$HOME*
77
$HOME-use version5.txt /*$HOME-use*
88
$HOME-windows options.txt /*$HOME-windows*
99
$MYGVIMRC gui.txt /*$MYGVIMRC*
10+
$MYVIMDIR starting.txt /*$MYVIMDIR*
1011
$MYVIMRC starting.txt /*$MYVIMRC*
1112
$VIM starting.txt /*$VIM*
1213
$VIM-use version5.txt /*$VIM-use*

src/option.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8243,6 +8243,55 @@ vimrc_found(char_u *fname, char_u *envname)
82438243
if (p != NULL)
82448244
{
82458245
vim_setenv(envname, p);
8246+
if (vim_getenv((char_u *)"MYVIMDIR", &dofree) == NULL)
8247+
{
8248+
size_t usedlen = 0;
8249+
int len = 0;
8250+
char_u *fbuf = NULL;
8251+
8252+
if (STRNCMP(gettail(fname), ".vimrc", 6) == 0)
8253+
{
8254+
len = STRLEN(p) - 2;
8255+
p[len] = '/';
8256+
}
8257+
else if (STRNCMP(gettail(fname), ".gvimrc", 7) == 0)
8258+
{
8259+
len = STRLEN(p);
8260+
char_u *buf = alloc(len);
8261+
if (buf != NULL)
8262+
{
8263+
mch_memmove(buf, fname, len - 7);
8264+
mch_memmove(buf + len - 7, (char_u *)".vim/", 5);
8265+
len -= 2; // decrement len, so that we can set len+1 = NUL below
8266+
vim_free(p);
8267+
p = buf;
8268+
}
8269+
}
8270+
#ifdef MSWIN
8271+
else if (STRNCMP(gettail(fname), "_vimrc", 6) == 0)
8272+
{
8273+
len = STRLEN(p) + 4; // remove _vimrc, add vimfiles/
8274+
char_u *buf = alloc(len);
8275+
if (buf != NULL)
8276+
{
8277+
mch_memmove(buf, fname, len - 10);
8278+
mch_memmove(buf + len - 10, (char_u *)"vimfiles\\", 9);
8279+
len -= 2; // decrement len, so that we can set len+1 = NUL below
8280+
vim_free(p);
8281+
p = buf;
8282+
}
8283+
}
8284+
#endif
8285+
else
8286+
(void)modify_fname((char_u *)":h", FALSE, &usedlen, &p, &fbuf, &len);
8287+
8288+
if (p != NULL)
8289+
{
8290+
// keep the directory separator
8291+
p[len + 1] = NUL;
8292+
vim_setenv((char_u *)"MYVIMDIR", p);
8293+
}
8294+
}
82468295
vim_free(p);
82478296
}
82488297
}

src/testdir/test_xdg.vim

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ source shared.vim
55

66
func s:get_rcs()
77
let rcs = {
8-
\ 'file1': { 'path': '~/.vimrc' },
9-
\ 'file2': { 'path': '~/.vim/vimrc' },
10-
\ 'xdg': { 'path': exists('$XDG_CONFIG_HOME') ? '$XDG_CONFIG_HOME' : "~/.config" },
8+
\ 'file1': { 'path': '~/.vimrc', 'dir': expand('~/.vim/') },
9+
\ 'file2': { 'path': '~/.vim/vimrc', 'dir': expand('~/.vim/') },
10+
\ 'xdg': { 'path': exists('$XDG_CONFIG_HOME') ? '$XDG_CONFIG_HOME' : "~/.config",
11+
\ 'dir': exists('$XDG_CONFIG_HOME') ? expand("$XDG_CONFIG_HOME/vim") : '~/.config/vim/'},
1112
\}
1213
for v in values(rcs)
1314
let v.exists = filereadable(expand(v.path))
@@ -20,18 +21,24 @@ func Test_xdg_rc_detection()
2021
let rc = s:get_rcs()
2122
let before =<< trim CODE
2223
call writefile([expand('$MYVIMRC')], "XMY_VIMRC")
24+
call writefile([expand('$MYVIMRCDIR')], "XMY_VIMDIR")
2325
quit!
2426
CODE
2527
call RunVim(before, [], "")
2628
let my_rc = readfile("XMY_VIMRC")
29+
let my_rcdir = readfile("XMY_VIMDIR")
2730
if rc.file1.exists
2831
call assert_equal(rc.file1.path, my_rc)
32+
call assert_equal(rc.file1.dir, my_rcdir)
2933
elseif !rc.file1.exists && rc.file2.exists
3034
call assert_equal(rc.file2.path, my_rc)
35+
call assert_equal(rc.file2.dir, my_rcdir)
3136
elseif !rc.file1.exists && !rc.file2.exists && rc.xdg.exists
3237
call assert_equal(rc.xdg.path, my_rc)
38+
call assert_equal(rc.xdg.dir, my_rcdir)
3339
endif
3440
call delete("XMY_VIMRC")
41+
call delete("XMY_VIMDIR")
3542
endfunc
3643

3744
func Test_xdg_runtime_files()
@@ -78,6 +85,7 @@ func Test_xdg_runtime_files()
7885
" Test for ~/.vimrc
7986
let lines =<< trim END
8087
call assert_match('XfakeHOME/\.vimrc', $MYVIMRC)
88+
call assert_match('XfakeHOME/.vim/', $MYVIMDIR)
8189
call filter(g:, {idx, _ -> idx =~ '^rc'})
8290
call assert_equal(#{rc_one: 'one', rc: '.vimrc'}, g:)
8391
call assert_match('XfakeHOME/\.vim/view', &viewdir)
@@ -93,6 +101,7 @@ func Test_xdg_runtime_files()
93101
" Test for ~/.vim/vimrc
94102
let lines =<< trim END
95103
call assert_match('XfakeHOME/\.vim/vimrc', $MYVIMRC)
104+
call assert_match('XfakeHOME/\.vim/', $MYVIMDIR)
96105
call filter(g:, {idx, _ -> idx =~ '^rc'})
97106
call assert_equal(#{rc_two: 'two', rc: '.vim/vimrc'}, g:)
98107
call assert_match('XfakeHOME/\.vim/view', &viewdir)
@@ -112,6 +121,7 @@ func Test_xdg_runtime_files()
112121
let lines =<< trim END
113122
let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
114123
call assert_match('XfakeHOME/\.config/vim/vimrc', $MYVIMRC, msg)
124+
call assert_match('XfakeHOME/\.config/vim/', $MYVIMDIR, msg)
115125
call filter(g:, {idx, _ -> idx =~ '^rc'})
116126
call assert_equal(#{rc_three: 'three', rc: '.config/vim/vimrc'}, g:)
117127
call assert_match('XfakeHOME/\.config/vim/view', &viewdir)
@@ -129,6 +139,7 @@ func Test_xdg_runtime_files()
129139
let lines =<< trim END
130140
let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
131141
call assert_match('XfakeHOME/xdg/vim/vimrc', $MYVIMRC, msg)
142+
call assert_match('XfakeHOME/xdg/vim/', $MYVIMDIR, msg)
132143
call filter(g:, {idx, _ -> idx =~ '^rc'})
133144
call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/vimrc'}, g:)
134145
call assert_match('XfakeHOME/xdg/vim/view, &viewdir)
@@ -225,6 +236,7 @@ func Test_zzz_xdg_runtime_files()
225236
call test_ignore_error('E285')
226237
gui -f
227238
call assert_match('Xhome/\.gvimrc', $MYGVIMRC)
239+
call assert_match('Xhome/\.vim/', $MYVIMDIR)
228240
call filter(g:, {idx, _ -> idx =~ '^rc'})
229241
call assert_equal(#{rc_one: 'one', rc: '.gvimrc'}, g:)
230242
call writefile(v:errors, 'Xresult')
@@ -242,6 +254,7 @@ func Test_zzz_xdg_runtime_files()
242254
call test_ignore_error('E285')
243255
gui -f
244256
call assert_match('Xhome/\.vim/gvimrc', $MYGVIMRC)
257+
call assert_match('Xhome/\.vim/', $MYVIMDIR)
245258
call filter(g:, {idx, _ -> idx =~ '^rc'})
246259
call assert_equal(#{rc_two: 'two', rc: '.vim/gvimrc'}, g:)
247260
call writefile(v:errors, 'Xresult')
@@ -260,6 +273,7 @@ func Test_zzz_xdg_runtime_files()
260273
gui -f
261274
let msg = $'HOME="{$HOME}", ~="{expand("~")}"'
262275
call assert_match('Xhome/\.config/vim/gvimrc', $MYGVIMRC, msg)
276+
call assert_match('Xhome/\.config/vim/', $MYVIMDIR, msg)
263277
call filter(g:, {idx, _ -> idx =~ '^rc'})
264278
call assert_equal(#{rc_three: 'three', rc: '.config/vim/gvimrc'}, g:)
265279
call writefile(v:errors, 'Xresult')
@@ -279,6 +293,7 @@ func Test_zzz_xdg_runtime_files()
279293
gui -f
280294
let msg = $'HOME="{$HOME}", XDG_CONFIG_HOME="{$XDG_CONFIG_HOME}"'
281295
call assert_match('Xhome/xdg/vim/gvimrc', $MYGVIMRC, msg)
296+
call assert_match('Xhome/xdg/vim/', $MYVIMDIR, msg)
282297
call filter(g:, {idx, _ -> idx =~ '^rc'})
283298
call assert_equal(#{rc_four: 'four', rc: 'xdg/vim/gvimrc'}, g:)
284299
call writefile(v:errors, 'Xresult')

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
718,
707709
/**/
708710
717,
709711
/**/

0 commit comments

Comments
 (0)