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

如何阻止Neovim自动将闭合大括号左移一个缩进?

解决Neovim中C++闭合大括号自动左移的问题

你需要调整Neovim内置的C/C++缩进规则来实现这种风格,核心是修改cinoptions参数,具体配置如下:

配置方式

根据你的配置文件类型选择对应的代码添加到配置中:

如果你用Vimscript(init.vim)

" 关闭可能干扰的自动缩进选项
set nocindent
set nosmartindent
set indentexpr=
" 修改cinoptions,让闭合大括号保持与代码块内相同缩进
let &cinoptions = &cinoptions . 'L0'

如果你用Lua(init.lua)

-- 关闭可能干扰的自动缩进选项
vim.opt.cindent = false
vim.opt.smartindent = false
vim.opt.indentexpr = ''
-- 添加L0参数,让闭合大括号的缩进级别与代码块内一致
vim.opt.cinoptions:append('L0')

说明

cinoptions中的L参数控制闭合大括号的缩进行为,L0表示闭合大括号的缩进级别和代码块内的行完全相同,不会自动左移。关闭cindent、smartindent和清空indentexpr是为了避免其他内置缩进规则干扰这个配置。

配置完成后,你输入的代码就会保持你想要的风格:

int main(){
    return 0;
    }

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 08:57:19