我想知道如何在 Neovim 启动时将自动缩进设置为四个空格,因为我使用空格进行缩进。
提前致谢。
我不太了解 Neovim,但是(从我读到的there)我猜它在这个主题上与 Vim 兼容。所以下面的解释适用于纯 Vim。
您正在寻找的选项是
'expandtab'
。不过,为了清楚起见,我在进入此选项之前解释了缩进宽度。
宽度由多个选项控制。这里的“缩进”是指在插入模式下按 <Tab>
(或按
<BS>
、退格键,可撤消现有缩进),或自动增加缩进级别(取决于语言)。
:help tabstop
:help softtabstop
:help shiftwidth
整数选项
'tabstop'
\t
)(不是您直接感兴趣的内容,但请参见下文)。整数选项
'softtabstop'
'tabstop'
的值(或更准确地说,禁用“软制表位”功能),特殊值 -1 表示复制
'shiftwidth'
的值。整数选项
'shiftwidth'
<<
、
>>
和
==
。特殊值0表示复制
'tabstop'
的值。用空格缩进
'expandtab'
<Tab>
会插入尽可能多的制表字符,并以空格字符填充直至缩进宽度。
:help expandtab
插图
tabstop=8
和
softtabstop=3
,则在插入模式下:
<Tab>
将插入 3 个空格,因此总缩进为 3 列宽;
<Tab>
将再插入 3 个空格,使总缩进为 6 列宽;
<Tab>
将使总缩进宽度为9列;如果设置了
'expandtab'
,则总共使用9个空格来写入;否则,它将使用制表符(替换以前的空格)后跟空格符来编写;
<BS>
将撤消步骤 3;
<BS>
将撤消步骤 2;
<BS>
将撤消步骤 1。
'tabstop'
的值即可。它还根据您的要求设置
'expandtab'
。最后,由于您引发了自动缩进,我添加了相关选项:
'autoindent'
、
'smartindent'
和
'cindent'
;但您应该为此使用特定于语言的插件。
" length of an actual \t character:
set tabstop=4
" length to use when editing text (eg. TAB and BS keys)
" (0 for ‘tabstop’, -1 for ‘shiftwidth’):
set softtabstop=-1
" length to use when shifting text (eg. <<, >> and == commands)
" (0 for ‘tabstop’):
set shiftwidth=0
" round indentation to multiples of 'shiftwidth' when shifting text
" (so that it behaves like Ctrl-D / Ctrl-T):
set shiftround
" if set, only insert spaces; otherwise insert \t and complete with spaces:
set expandtab
" reproduce the indentation of the previous line:
set autoindent
" keep indentation produced by 'autoindent' if leaving the line blank:
"set cpoptions+=I
" try to be smart (increase the indenting level after ‘{’,
" decrease it after ‘}’, and so on):
"set smartindent
" a stricter alternative which works better for the C language:
"set cindent
" use language‐specific plugins for indenting (better):
filetype plugin indent on
您可以调整这些设置并将其写在您的
.vimrc
或
.nvimrc
文件中。当然,此外,您可以根据每个缓冲区的文件类型选择特定设置。例如:
" do NOT expand tabulations in Makefiles:
autocmd FileType make setlocal noexpandtab
" for the C language, indent using 4‐column wide tabulation characters,
" but make <Tab> insert half‐indentations as 2 spaces (useful for labels):
autocmd FileType c setlocal noexpandtab shiftwidth=2
" use shorter indentation for Bash scripts:
autocmd FileType sh setlocal tabstop=2
set tabstop=2
set shiftwidth=2
set expandtab
set smartindent