Vim编辑VHDL时if-else-end if;语句缩进异常该如何修复?
VHDL if-else-end if 缩进异常修复方案
前置校验
- 首先确认配置无冲突,打开你的
.vimrc(Vim)或init.vim/init.lua(Neovim),检查是否存在强制覆盖VHDL缩进规则的配置:- 若存在
autocmd FileType vhdl setlocal indentexpr=、set cindent或强制指定缩进逻辑的语句,请全部删除 - 增加如下配置确保官方VHDL缩进规则正常加载:
augroup vhdl_indent autocmd! autocmd FileType vhdl setlocal indentexpr=GetVhdlIndent() autocmd FileType vhdl setlocal autoindent smartindent nocindent " 按需调整缩进宽度,保证shiftwidth与tabstop一致 autocmd FileType vhdl setlocal shiftwidth=4 tabstop=4 expandtab augroup END
- 若存在
修复官方缩进脚本已知Bug
Vim 8.2自带的vhdl.vim缩进脚本存在end if匹配后重复缩进的已知问题,直接修改脚本即可:
- 打开缩进脚本文件,路径为你环境中的
/usr/share/vim/vim82/indent/vhdl.vim,Neovim默认路径为/usr/share/nvim/runtime/indent/vhdl.vim - 搜索如下代码段:
if line =~ '^\s*end\>' let ind = ind - s:shiftwidth() endif - 修改为以下内容,避免
end if补全后二次缩进:if line =~ '^\s*end\>' let ind = ind - s:shiftwidth() " 修复end if补全后多缩进一层的问题 if line =~ '^\s*end\s\+if\>' || line =~ '^\s*end\s\+if\s*;' let ind = ind - s:shiftwidth() endif endif
排除第三方插件干扰
自动补全、自动补全关键字类插件是出现多余空行的常见原因:
- 若你使用
vim-endwise插件,添加配置禁用其对VHDL的自动适配:let g:endwise_no_mappings = {'vhdl': 1} - 若你使用
nvim-autopairs、ultisnips等自动补全/ snippet插件,添加规则排除VHDL文件类型的end关键字自动补全、自动插入换行逻辑即可。
验证生效
修改完成后重启编辑器,打开任意VHDL文件执行以下命令确认配置加载正常:
- 执行
:set indentexpr?,返回值应为GetVhdlIndent() - 执行
:set filetype?,返回值应为vhdl
重新测试if-else-end if的缩进逻辑即可恢复正常。
内容的提问来源于stack exchange,提问作者Wtswkz
相关产品推荐
相关产品推荐

