[vim/vim] netrw always re-trigger FileType on enters file event loaded buffer (Issue #20980)

arkissa (Vim Github Repository) <[email protected]> Sun, 09 Aug 2026 00:58:16 -0700
Newsgroups gmane.editors.vim.devel
Message-ID <vim/vim/issues/[email protected]>
Arkissa created an issue (vim/vim#20980)

### Steps to reproduce

1. Write this to a test file.

```vimscript
vim9script

packadd netrw

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

def Test()
        set filetype=vim
        b:match_skip = 10
        echo 'done'
enddef
```

2. Run:

```sh
vim --clean -S test test
```

3. `:echo b:match_skip` returns `10`.

4. Run:

```vim
:Explore<CR>
```

5. Choose the test file and press `<CR>` to enter the test file.

6. `echo b:match_skip` returns:

```
E121: Undefined variable: b:match_skip
```

[https://github.com/user-attachments/assets/b84e813d-32fc-4e87-b471-51d08b408238](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`.

```vimscript
...
  " 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
...
```

```vimscript
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

```shell

```

-- 
Reply to this email directly or view it on GitHub:
https://github.com/vim/vim/issues/20980
You are receiving this because you are subscribed to this thread.

Message ID: <vim/vim/issues/[email protected]>

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups "vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/vim_dev/vim/vim/issues/20980%40github.com.