在 neovim 中设置自动缩进空格?

问题描述 投票:0回答:3

我想知道如何在 Neovim 启动时将自动缩进设置为四个空格,因为我使用空格进行缩进。

提前致谢。

vim neovim
3个回答
22
投票

我不太了解 Neovim,但是(从我读到的there)我猜它在这个主题上与 Vim 兼容。所以下面的解释适用于纯 Vim。

您正在寻找的选项是

'expandtab'
。不过,为了清楚起见,我在进入此选项之前解释了缩进宽度。

缩进宽度

缩进的

宽度由多个选项控制。这里的“缩进”是指在插入模式下按 <Tab>

(或按 
<BS>
、退格键,可撤消现有缩进),或自动增加缩进级别(取决于语言)。

:help tabstop :help softtabstop :help shiftwidth

整数选项

'tabstop'

指示用于显示实际制表字符的宽度(\t
)(不是您直接感兴趣的内容,但请参见下文)。

整数选项

'softtabstop'

表示缩进应该跨越多宽。特殊值 0 表示复制 'tabstop'
 的值(或更准确地说,禁用“软制表位”功能),特殊值 -1 表示复制 
'shiftwidth'
 的值。

整数选项

'shiftwidth'

给出用于移位命令的宽度,例如<<
>>
==
。特殊值0表示复制
'tabstop'
的值。

用空格缩进

当设置

'expandtab'

 时,缩进始终仅使用空格字符完成。否则,按 <Tab>
 会插入尽可能多的制表字符,并以空格字符填充直至缩进宽度。

:help expandtab

插图

例如,如果

tabstop=8

softtabstop=3
,则在插入模式下:

    在空行上按
  1. <Tab>
     将插入 3 个空格,因此总缩进为 3 列宽;
  2. 再次按
  3. <Tab>
     将再插入 3 个空格,使总缩进为 6 列宽;
  4. <Tab>
    将使总缩进宽度为9列;如果设置了
    'expandtab'
    ,则总共使用9个空格来写入;否则,它将使用制表符(替换以前的空格)后跟空格符来编写;
  5. <BS>
     将撤消步骤 3;
  6. <BS>
     将撤消步骤 2;
  7. <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
    

11
投票
如果您想使用 2 个空格缩进,请将其放入您的配置中:

set tabstop=2 set shiftwidth=2 set expandtab set smartindent
    

9
投票
卢阿:

local o = vim.o o.expandtab = true # expand tab input with spaces characters o.smartindent = true # syntax aware indentations for newline inserts o.tabstop = 2 # num of space characters per tab o.shiftwidth = 2 # spaces per indentation level

解释选项卡设置的文章

© www.soinside.com 2019 - 2024. All rights reserved.