Neovim自动识别细微差别

发布于 2025-01-23 17:41:26 字数 972 浏览 1 评论 0原文

我刚刚从VIM切换到Neovim。以下vim配置设置从凹痕中获得我想要的行为:

set tabstop=4
set shiftwidth=4
set softtabstop=4
set autoindent
set backspace=indent,eol,start

这里的相关部分是自动设置:

set autoindent

在每个newline上,这会导致VIM匹配上一行的凹痕:

def demo_autoindent():
    a = 'This line was manually indented'
····

a的声明是手动的缩进了一个步骤,但第二行被自动识别到同一水平。这里我已经用一系列·字符表示自动结合。

neovim匹配此行为。但是,Neovim试图对块有点聪明,或者在这种情况下,在Python中宣布词典:

def example_neovim():
    b = {
············

请注意,Neovim具有 自动识别这一行。如果有的话,它将具有与b的声明相同的4空间缩进。相反,Neovim增加了一个额外的两个凹痕,使总数达到了12个空间。

显然,它打算做的是添加一个进一步的缩进:

def example_neovim_intention():
    c = {
········

我如何将Neovim配置为:

  1. 匹配VIM的行为,只是自动识别到同一级别。
  2. 声明dict时,添加单个(而不是双重)额外的缩进。

I've just switched from vim to neovim. The following vim config settings get the behaviour I want from indentation:

set tabstop=4
set shiftwidth=4
set softtabstop=4
set autoindent
set backspace=indent,eol,start

The relevant part here is the autoindent:

set autoindent

On each newline, this causes vim to match the indentation of the previous line:

def demo_autoindent():
    a = 'This line was manually indented'
····

The declaration of a was manually indented one step, but the second line was auto-indented to the same level. Here I have represented the auto-indent with a series of · characters.

Neovim matches this behaviour. However, Neovim tries to be a bit clever with blocks, or in this case declaring dictionaries in python:

def example_neovim():
    b = {
············

Note that Neovim has not auto-indented this line. If it had, it would have the same 4-space indent as the declaration of b. Instead, Neovim has added an extra two indents, bringing the total to 12-spaces.

Clearly what it intends to do is add one further indent:

def example_neovim_intention():
    c = {
········

How do I configure Neovim to either:

  1. Match the behaviour of vim, just auto-indent to the same level.
  2. Add a single (rather than a double) extra indent when declaring e.g. a dict.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦回旧景 2025-01-30 17:41:26

我正面临着完全相同的问题,我使用了VIM很长时间,最近我转到Neovim。

除“ Python”外,其他所有语言的自动凹陷都很好。我发现该文档非常有用: https://neovim.io/doc/doc/doc/user/indent/indent/indent/indent .html#_python

:h ft-python-indent

Neovim将检查此变量g:python_indent,如果未定义,将创建一个默认一个,并且它将是这样(Neovim 0.8.2):

{
  'disable_parentheses_indenting': v:false,
  'closed_paren_align_last_line': v:true,
  'searchpair_timeout': 150,
  'continue': 'shiftwidth() * 2',
  'open_paren': 'shiftwidth() * 2',
  'nested_paren': 'shiftwidth()'
}

更改这些属性/code>,open_parenloss_paren_align_last_line如下。

{
  'disable_parentheses_indenting': v:false,
  'closed_paren_align_last_line': v:false,
  'searchpair_timeout': 150,
  'continue': 'shiftwidth()',
  'open_paren': 'shiftwidth()',
  'nested_paren': 'shiftwidth()'
}

另一种方法

可以更轻松地解决怪异的凹痕问题。它在我这边起作用,但也许根据以下答复越野车。

设置 nvim-treesitter/nvim-treesitter/nvim-treesitter 并打开树木 - sitter的抚养功能。

require('nvim-treesitter/nvim-treesitter').setup {
  ...
  indent = { enable = true },
  ...
}

I was facing exactly the same issue, I used VIM for quite a long time and recently I switch to NeoVim.

Auto indention on for every other languages except "python" works well. I found this document quite useful: https://neovim.io/doc/user/indent.html#_python.

:h ft-python-indent

NeoVim will check this variable g:python_indent, if it is not defined a default one will be created, and it would be something like this (NeoVim 0.8.2):

{
  'disable_parentheses_indenting': v:false,
  'closed_paren_align_last_line': v:true,
  'searchpair_timeout': 150,
  'continue': 'shiftwidth() * 2',
  'open_paren': 'shiftwidth() * 2',
  'nested_paren': 'shiftwidth()'
}

Change these properties continue,open_paren and closed_paren_align_last_line as follows.

{
  'disable_parentheses_indenting': v:false,
  'closed_paren_align_last_line': v:false,
  'searchpair_timeout': 150,
  'continue': 'shiftwidth()',
  'open_paren': 'shiftwidth()',
  'nested_paren': 'shiftwidth()'
}

Another Approach

This can fix the weird indention issue easier. It work on my side, but maybe buggy according to the replies below.

Setup nvim-treesitter/nvim-treesitter and turn on tree-sitter's indention feature.

require('nvim-treesitter/nvim-treesitter').setup {
  ...
  indent = { enable = true },
  ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文