简体   繁体   English

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

[英]Setting autoindentation to spaces in neovim?

I was wondering how I could set the autoindentation to four spaces upon startup in neovim, because I use spaces for indentation.我想知道如何在 Neovim 启动时将自动缩进设置为四个空格,因为我使用空格进行缩进。

Thanks in advance.提前致谢。

I don't know Neovim specifically, but (from what I read there ) I guess that it is compatible with Vim on this topic.我并不特别了解 Neovim,但是(从我在那里读到的内容)我猜它在这个主题上与 Vim 兼容。 So explanations below apply to pure Vim.所以下面的解释适用于纯 Vim。

The option you are looking for is 'expandtab' .您正在寻找的选项是'expandtab' However, for clarity, I explain indenting width before going into this option.然而,为了清楚起见,我在进入这个选项之前解释了缩进宽度。

Indenting width(s)缩进宽度

The width of an indentation is controlled by several options.缩进的宽度由几个选项控制。 By “indenting”, here, I mean for example pressing <Tab> in Insert Mode (or <BS> , backspace, which undoes an existing indentation), or increasing the indenting level automatically (depending on the language).这里的“缩进”是指例如在插入模式下按<Tab> (或<BS> ,退格键,撤消现有缩进),或自动增加缩进级别(取决于语言)。

:help tabstop
:help softtabstop
:help shiftwidth

The integer option 'tabstop' dictates the width used to display an actual tabulation character ( \\t ) (not directly what you are interested in, but see below).整数选项'tabstop'规定了用于显示实际制表字符 ( \\t ) 的宽度(不是您感兴趣的直接内容,但请参见下文)。

The integer option 'softtabstop' says how wide an indentation is supposed to span.整数选项'softtabstop'表示缩进应该跨越的宽度。 The special value 0 means to replicate the value of 'tabstop' (or more exactly, to disable the “soft tab stops” feature), the special value −1 means to replicate the value of 'shiftwidth' .特殊值 0 表示复制'tabstop'的值(或更准确地说,禁用“软制表位”功能),特殊值 -1 表示复制'shiftwidth'的值。

The integer option 'shiftwidth' gives the width used for shifting commands, such as << , >> and == .整数选项'shiftwidth'给出用于移位命令的宽度,例如<<>>== The special value 0 means to replicate the value of 'tabstop' .特殊值 0 表示复制'tabstop'的值。

Indenting with spaces用空格缩进

When 'expandtab' is set, indenting is always done using space characters only.当设置了'expandtab' ,缩进总是只使用空格字符。 Otherwise, pressing <Tab> inserts as many tabulation characters as possible, and complete with space characters up to the indenting width.否则,按<Tab>插入尽可能多的制表字符,并用空格字符完成缩进宽度。

:help expandtab

An illustration插图

For example, if tabstop=8 and softtabstop=3 , then, in Insert Mode:例如,如果tabstop=8softtabstop=3 ,则在插入模式下:

  1. pressing <Tab> on an empty line will insert 3 spaces, so that the total indentation is 3‐column wide;在空行上按<Tab>将插入 3 个空格,因此总缩进为 3 列宽;
  2. pressing <Tab> again will insert 3 more spaces, so that the total indentation is 6‐column wide;再次按下<Tab>将再插入 3 个空格,因此总缩进为 6 列宽;
  3. pressing <Tab> will make the total indentation 9‐column wide;<Tab>将使总缩进 9 列宽; if 'expandtab' is set, then it will be written using a total of 9 spaces;如果设置了'expandtab' ,那么它将使用总共 9 个空格写入; otherwise, it will be written using a tabulation character (which replaces former whitespaces) followed by a space character;否则,它将使用制表字符(替换以前的空格)后跟空格字符编写;
  4. pressing <BS> will undo step 3;<BS>将撤消第 3 步;
  5. pressing <BS> will undo step 2;<BS>将撤消第 2 步;
  6. pressing <BS> will undo step 1.<BS>将撤消步骤 1。

Example configuration示例配置

Most often, you want to make it simple and have the same value for the three width options.大多数情况下,您希望使其简单并为三个宽度选项设置相同的值。 Here is a example configuration that identifies all three options, so that you just need to change the value of 'tabstop' to your liking.这是一个标识所有三个选项的示例配置,因此您只需根据自己的喜好更改'tabstop'的值。 It also sets 'expandtab' as you requested.它还会根据您的要求设置'expandtab' Finally, since you evoked auto‐indentation, I included related options: 'autoindent' , 'smartindent' and 'cindent' ;最后,由于您调用了自动缩进,因此我包含了相关选项: 'autoindent''smartindent''cindent' but you should rather use language‐specific plugins for that.但是您应该为此使用特定于语言的插件。

" 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

You can tweak these settings and write them down in your .vimrc or .nvimrc file.您可以调整这些设置并将它们写在您的.vimrc.nvimrc文件中。

Of course, additionally, you can select specific settings for each buffer, based on its filetype.当然,此外,您可以根据每个缓冲区的文件类型为每个缓冲区选择特定设置。 For example:例如:

" 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

If you want to use 2 spaces indentation, put this in your config:如果你想使用 2 个空格缩进,把它放在你的配置中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM