You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Neovim的:w、:wq等指令忽略弹窗缓冲区?

问题描述

使用以下Vimscript代码创建弹窗缓冲区与窗口:

:function! PopupDemo()
    let message = 'Hello World'
    for i in range(10)
        let message = message . ' ' . message
    endfor

    let s:win_buf = bufadd('popup-demo')
    call bufload(s:win_buf)
    call setbufline(s:win_buf, 1, message)
    echo 'buf id: ' . s:win_buf

    let width = 40
    let height = 10
    " Set the new window as the current window.
    let enter = v:false
    " Calculate the centering coordinates.
    let win_x = &columns / 2 - width / 2
    let win_y = &lines / 2 - height / 2

    let s:win_id = nvim_open_win(s:win_buf, enter, {
                \'relative': 'editor',
                \'row': win_y,
                \'col': win_x,
                \'width': width,
                \'height': height,
                \'focusable': v:false,
                \'style': 'minimal',
                \'border': ['╔', '═','╗', '║', '╝', '═', '╚', '║'],
                \'noautocmd': v:false,
                \'bufpos': [0, 0]
                \})

    call nvim_win_set_option(s:win_id, 'wrap', v:true)
:endfunction

:function! PopupClose(timer)
    if s:win_id != 0
        call nvim_win_close(s:win_id, v:false)
    endif
    if s:win_buf != 0
        call nvim_buf_delete(s:win_buf, {'force': v:true})
    endif
:endfunction

执行PopupDemo()创建弹窗后,未调用PopupClose()就使用:wq指令保存退出Neovim时,会触发以下错误:

E37: No write since last change
E162: No write since last change for buffer "popup-demo"

需要让:w、:wq、:wqall等保存指令忽略该弹窗缓冲区,使用的Neovim版本为v0.7.2。


解决方案

针对Neovim v0.7.2,提供三种可行方案:

方案1:标记缓冲区为不可修改

在PopupDemo()函数中,设置完缓冲区内容后,将缓冲区标记为不可修改。Neovim会认为该缓冲区没有未保存的修改,退出时不会触发提示。

修改PopupDemo(),在call setbufline(s:win_buf, 1, message)之后添加:

call nvim_buf_set_option(s:win_buf, 'modifiable', v:false)

方案2:将缓冲区设为临时缓冲区

临时缓冲区(buftype=nofile)不关联实际文件,Neovim默认不会提示保存这类缓冲区。

修改PopupDemo(),在let s:win_buf = bufadd('popup-demo')之后添加:

call nvim_buf_set_option(s:win_buf, 'buftype', 'nofile')

方案3:自动在退出时清理弹窗

添加自动命令,在Neovim退出前自动调用PopupClose(),清理弹窗窗口和缓冲区,从根源避免保存提示。

在代码末尾添加:

augroup PopupAutoClose
    autocmd!
    autocmd VimPreLeave * call PopupClose(v:null)
augroup END

内容的提问来源于stack exchange,提问作者aszswaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 00:25:28