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

Open
#20,980 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 13 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
vim
Domain
tooling

Research direction

Start with netrw's NetrwGetFile path and the filetype detect call described in the issue, then inspect how LoadFTPlugin handles b:undo_ftplugin. Reproduce the steps with the Vim9 test file and :Explore. Done means entering the file does not retrigger FileType or remove user settings such as b:match_skip, omnifunc, formatexpr, and foldexpr.

Written by the indexing model from the issue text.

Description

bug
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

https://github.com/user-attachments/assets/b84e813d-32fc-4e87-b471-51d08b408238

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

Dominant language
Vim Script
Stars
41.4k
Forks
6.2k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vim/vim

All issues in vim/vim

Similar issues

More DevTools issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.