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

Di *_* W. 4 vim neovim

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

提前致谢。

Pau*_*rba 13

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

set tabstop=2
set shiftwidth=2
set expandtab
set smartindent
Run Code Online (Sandbox Code Playgroud)


gar*_*ing 13

路亚:

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
Run Code Online (Sandbox Code Playgroud)

一篇解释选项卡设置的文章


Maë*_*lan 8

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

您正在寻找的选项是'expandtab'。然而,为了清楚起见,我在进入这个选项之前解释了缩进宽度。

缩进宽度

缩进的宽度由几个选项控制。这里的“缩进”是指例如<Tab>在插入模式下按下(或<BS>退格键,取消现有缩进),或自动增加缩进级别(取决于语言)。

:help tabstop
:help softtabstop
:help shiftwidth
Run Code Online (Sandbox Code Playgroud)

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

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

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

用空格缩进

当'expandtab'设置,缩进仅使用空格字符经常做。否则,按会<Tab>插入尽可能多的制表字符,并用空格字符完成缩进宽度。

:help expandtab
Run Code Online (Sandbox Code Playgroud)

插图

例如,如果tabstop=8和softtabstop=3,则在插入模式下:

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

您可以调整这些设置并将它们写在您的.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
Run Code Online (Sandbox Code Playgroud)