在 Vim 中使用 `put` 时如何正确缩进文本

tce*_*act 1 vim copy-paste indentation paste auto-indent

我在 Vim 中发短信时遇到问题putting

假设我想将我的/* Comment */行粘贴到该$('table').append行下方...

/* Comment */

for (var i=1; i<=lineLength ; i++) {
    $('table').append('<tr></tr>');
    for (var j=1; j<=lineLength; j++) {
    $('table tr:nth-last-child(1)').append('<td></td>');
    }
}
Run Code Online (Sandbox Code Playgroud)

在大多数文本编辑器中,我的工作流程是

  1. 选择/* Comment */,点击剪切。
  2. 将光标移动到第一行代码末尾并按回车键。
  3. 文本编辑器自动缩进,我只需点击粘贴即可。

IE

/* Comment */

for (var i=1; i<=lineLength ; i++) {
    $('table').append('<tr></tr>');
    | <==Pipe is position of cursor before paste; pasted lines are inserted here.
    for (var j=1; j<=lineLength; j++) {
    $('table tr:nth-last-child(1)').append('<td></td>');
    }
}
Run Code Online (Sandbox Code Playgroud)

但对于 vim,我似乎必须这样做:

  1. 移动到/* Comment */线上,点击dd
  2. 移动到$('table').append线上,点击p

新代码:

for (var i=1; i<=lineLength ; i++) {
        $('table').append('<tr></tr>');
/* Comment */. <== Comment is not correctly indented.
        for (var j=1; j<=lineLength; j++) {
        $('table tr:nth-last-child(1)').append('<td></td>');
        }
    }
Run Code Online (Sandbox Code Playgroud)
  1. 手动修复错误缩进的代码。

当我用 开始新行时,Vim 自动缩进很好o,所以看起来它也应该处理putting新行......是否有一个命令可以让我put使用正确的缩进来编写新代码行?

Pet*_*ker 5

您可以使用]p[p在当前行的缩进级别进行粘贴。请注意,这仅在寄存器的内容是逐行的情况下才有效。看:h ]p

如果你想使用]pand 朋友,但总是希望它是逐行的,那么我建议你看看 Tim Pope 的unimpaired.vim插件。它还提供>p/<p映射,将一层缩进更深/更浅地粘贴,以及=p/=P粘贴然后重新缩进,类似于p='].