Skip to content

netrw always re-trigger FileType on enters file event loaded buffer #20980

Description

@Arkissa

Steps to reproduce

  1. Write this to a test file.
vim9script

packadd netrw

augroup Test
        autocmd!
        autocmd VimEnter * call Test()
augroup END

def Test()
        set filetype=vim
        b:match_skip = 10
        echo 'done'
enddef
  1. Run:
vim --clean -S test test
  1. :echo b:match_skip returns 10.

  2. Run:

:Explore<CR>
  1. Choose the test file and press <CR> to enter the test file.

  2. echo b:match_skip returns:

E121: Undefined variable: b:match_skip
Screencast.From.2026-08-09.15-21-29.mp4

Because when netrw enters a file, it uses filetype detect to initialize native or remote files. filetype detect will trigger FileType * again, which calls LoadFTPlugin(). It will execute b:undo_ftplugin.

...
  " Moved the filetype detect here from NetrwGetFile() because remote files
  " were having their filetype detect-generated settings overwritten by
  " NetrwOptionRestore.
  if &ft != "netrw"
    filetype detect
  endif
...
def LoadFTPlugin()
  if exists("b:undo_ftplugin")
    # We assume b:undo_ftplugin is using legacy script syntax
    legacy exe b:undo_ftplugin
    unlet! b:undo_ftplugin b:did_ftplugin
  endif

  var s = expand("<amatch>")
  if s != ""
    if &cpo =~# "S" && exists("b:did_ftplugin")
      # In compatible mode options are reset to the global values, need to
      # set the local values also when a plugin was already used.
      unlet b:did_ftplugin
    endif

    # When there is a dot it is used to separate filetype names. Thus for
    # "aaa.bbb" load "aaa" and then "bbb".
    for name in split(s, '\.')
      exe 'runtime! ftplugin/' .. name .. '.vim ftplugin/' .. name .. '_*.vim ftplugin/' .. name .. '/*.vim'
    endfor
  endif
enddef

If commands in b:undo_ftplugin are the same as the variables or options initialized by users, they will be undone and initialized back to the values from the built-in ftplugin file. This breaks user updates.

This is especially problematic in LSP scenarios. It often conflicts with some settings in built-in ftplugin files, such as omnifunc, formatexpr, and foldexpr.

btw NerdTree not have this problem, because it use :edit to enters file.

Expected behaviour

netrw won't be use filetype detect to re-treggier autocmd FileType

Version of Vim

:version VIM - Vi IMproved 9.2 (2026 Feb 14, compiled Aug 1 2026 21:48:38) Included patches: 1-892

Environment

Linux debian 7.1.3+deb14-amd64 #1 SMP PREEMPT_DYNAMIC Debian 7.1.3-1 (2026-07-04) x86_64 GNU/Linux

Logs and stack traces

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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