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

标签 vim neovim

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

提前致谢。

最佳答案

我不知道 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=8softtabstop=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
    

    关于vim - 在neovim中将自动缩进设置为空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51995128/

    相关文章:

    c++ - ccls 安装在 neovim 中但无法正常工作

    vim - 如何在 Gvim 中为一种语言添加智能感知?

    function - 如何在 Vim 函数中模拟按键?

    json - 在特定点折叠 JSON

    lua - Neovim:缺少 nvim-tree-web-devicons 的图标

    vim - Neovim 在 tmux 中丢失颜色方案。我看到的所有过去的解决方案都不适合我

    search - VIM:合并两个搜索

    vim - Vim 中追加模式和插入模式的区别

    linux - 如何配置 Vim 备份目录

    javascript - 执行 vim.schedule lua 回调时出错