Found the issue due to souce the menu this cause encoding=utf-8 and fileencoding=cp936 in _vimrc but in txt files it shows encoding=utf-8 fileencoding=utf-8
The way to fix is reset fenc=utf-8 after source $VIMRUNTIME/delmenu.vim
It worked

"condition setting {{{
if has("multi_byte")
" UTF-8 
set encoding=utf-8
set termencoding=utf-8
set formatoptions+=mM
set fileencodings=utf8,ucs-bom,gbk,cp936,gb2312,gb18030
if v:lang =~? '^\(zh\)\|\(ja\)\|\(ko\)'
set ambiwidth=double
if has("win64")
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
source  D:\Vim\function.vim
source  D:\Vim\PlugSetting.vim
"set menu "
set langmenu=zh_CN.UTF-8
set helplang=cn
language messages zh_CN.utf-8
set fenc=utf-8   " **<-- defined again here will keep enc and fenc all utf-8**


  • Two kinds of characters in my TXT file, English and Chinese
    Both charcters are shows correct in gvim.
    Search and Substitute are correctly with mumanlly using :g/\w\{4\}大 and s/\w{4\}大
  • Error when using autocmd to execute the searching
    au BufReadPost *.txt execute ' g/\w\{4\}大/ '
    the seraching pattern Chinese character seems with wrong encoding shows as <b4><f3> open the txt file shows Pattern not found:\w\{4\}<b4><f3>

Why same command :g/\w\{4\}大 have different encoding by manually typing and by executing in autocmd ?

I am writing a autocmd to dected TXT file and do some search here's an simple case


the autocmd in my _vimrc is au BufReadPost *.txt execute ' g/\w\{4\}大/ ' then when I open a txt file it will shows the encoding error the final character not shows correctly, it becomes<b4><f3>, here's the screen shot.

enter image description here

But, when i directly run the command in the txt file, it do worked: enter image description here

This really fused me, i try and serach any way to fix this problem finally found the this character encoding in autocmd is encoding=cp936, so i set a seperate encoding for the autocmd au BufReadPost *.txt execute ' set enc=cp936|g/\w\{4\}大/ ' this will shows the correclt global search but! it will make the txt file wrong I can see the global search cmd shows the correct character , but the context in TXT shows not correctly.

enter image description here enter image description here

My vim version 9.0, MS-Windows 64-bit GUI-console with OLE support. My encoding in _vimrc is:

set encoding=utf-8
set termencoding=utf-8
set fileencodings=utf8,ucs-bom,gbk,cp936,gb2312,gb18030

Why cause this problem, really confused

I am not sure how to shows the Chinese correctly both in file and when running au BufReadPost *.txt execute 'g/\w\{4\}大/'

  • you should never change the encoding option of your running Vim. That is strongely discouraged. Nov 1, 2023 at 7:08
  • Hi,Chirs, I dont change the encoding but it really shows wrong character when runing autocmd
    – M_Sea
    Nov 1, 2023 at 7:22
  • Using :set encoding=cp936 you are changing encoding. This makes all literal text stored inside Vims memory potentially invalid. Nov 1, 2023 at 7:31
  • 1
    What is the encoding of your _vimrc file? Nov 1, 2023 at 8:00
  • 1
    Glad you found a solution, though I doubt it should be the solution. Better save your _vimrc in utf-8 encoding and -- for safety -- put a scriptencoding utf-8 at the start of it. (Actually I don't know if setting the file encoding inside _vimrc has -- or should have -- any effect.) Nov 2, 2023 at 7:07

1 Answer 1


The g/\w{4\}大 is an incomplete command (you lack the corresponding action)

If you would like initiate a search I would add the following line to your _vimrc instead:

au BufReadPost *.txt call setreg('/', '\w\{4\}大')
  • 1
    that is not true. By default, if no other action is given, :g will print the matching lines Nov 1, 2023 at 9:17
  • I try this cmd au BufReadPost *.txt call setreg('/', '\w\{4\}大') but no effect in my *txt file
    – M_Sea
    Nov 1, 2023 at 9:19
  • 1
    perhaps you should tell us, what you actually want to achieve instead? Why do you think you need such a BufReadPost autocommand? Nov 1, 2023 at 9:21
  • Hi,@ChristianBrabandt,all my files are open with gvim ,only txt file I want to do the serach and replace with some patterns, this is why I use an autocmd BufReadPost *.txt ,but all chinese characters running in autocmd shows incorrectly.
    – M_Sea
    Nov 1, 2023 at 9:33
  • 1
    for search and replace :g is the wrong command anyhow. Why do you want to perform a search and replace on all text files when loading your files. This will potentially corrupt your files without you even noticing. Please state in your question what exact problem you are facing. Nov 1, 2023 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.