Skip to content

$MYVIMDIR unset when no ~/.vimrc (netrw creating '$MYVIMDIR' directories) #16609

Closed
@sandwm

Description

@sandwm

Steps to reproduce

  1. Make sure you don't have a .vimrc, e.g.: mv ~/.vimrc{,.bak}
  2. Run vim . (or just vim and go to 4.)
  3. See a new directory named '$MYVIMDIR' (see note on netrw.vim below)
  4. See $MYVIMDIR is not set: :echo expand("$MYVIMDIR")

When ~/.vimrc exists, no directory $MYVIMDIR is created and the environment variable is set to /home/swm/.vim/ (in my case; first directory in runtimepath).

Expected behaviour

$MYVIMDIR is set (regardless of a user .vimrc existing or not), as starting.txt documentation seems to imply:

The $MYVIMRC or $MYGVIMRC environment variable will be set to the first found
vimrc and/or gvimrc file while $MYVIMDIR is set to the users personal runtime
directory 'rtp' (typically the first entry in 'runtimepath').
Note: These environment variables resolve symbolic links, but 'rtp' does not.

starting.txt also says:

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

So with vim -u ... $MYVIMDIR is not set (as per documentation), but this also happens when running just vim and no user .vimrc exists. That's arguably a similar situation, but not documented as such.

In the source, $MYVIMDIR is set in src/option.c, vimrc_found().

I ask to either

  • set $MYVIMDIR even without .vimrc, or
  • make the documentation more clear.

Resulting netrw issue

When you don't have a .vimrc and look into any directory using netrw.vim, it will create a '$MYVIMDIR' directory in the current working directory (not necessarily the directory you look at).

That's how I stumbled over this: There were some of these directories lying around. On some users / hosts they were created when using vim, on others not. Turned out, the difference was the (non-)existence of a user .vimrc.

netrw.vim uses $MYVIMRC in runtime/pack/dist/opt/netrw/autoload/netrw.vim:

" ---------------------------------------------------------------------
"  s:NetrwHome: this function determines a "home" for saving bookmarks and history {{{2
function! s:NetrwHome()
  if has('nvim')
    let home = netrw#own#JoinPath(stdpath('state'), 'netrw')
  elseif exists("g:netrw_home")
    let home = expand(g:netrw_home)
  else
    let home = expand("$MYVIMDIR")->substitute("/$", "", "")
  endif
  [...]

I can create an issue at https://github.com/saccarosium/netrw.vim depending on whether you consider this a vim or a netrw bug.

Version of Vim

9.1.1065 (Huge version without GUI. Arch package)

Environment

OS: Arch Linux
Terminal: mate-terminal
$TERM: xterm-256color
Shell: bash

Logs and stack traces

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions