【问题标题】:How to correctly indent text when using `put` in Vim在 Vim 中使用“put”时如何正确缩进文本
【发布时间】:2017-03-13 21:44:19
【问题描述】:

putting 在 Vim 中输入文本时遇到问题。

假设我想将我的 /* 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>');
    }
}

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

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

/* 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>');
    }
}

但是使用 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>');
        }
    }
  1. 手动修复不正确缩进的代码。

当我用o 开始一个新行时,Vim 自动缩进很好,所以它似乎也应该将putting 处理到一个新行......有没有一个命令可以让我put new具有正确缩进的代码行?

【问题讨论】:

    标签: vim indentation copy-paste paste auto-indent


    【解决方案1】:

    您可以使用]p[p 在当前行的缩进级别粘贴。请注意,这仅适用于寄存器的内容是逐行的。见:h ]p

    如果你想使用]p 和朋友,但总是希望它是线性的,那么我建议你看看 Tim Pope 的 unimpaired.vim 插件。它还提供&gt;p/&lt;p 映射,将一个缩进级别粘贴得更深/更浅,以及=p/=P 粘贴然后重新缩进,类似于p=']

    【讨论】:

      【解决方案2】:
      :nnoremap p p`[v`]=
      

      取自https://github.com/sickill/vim-pasta

      【讨论】:

      • :nnoremap p p`[v`]= 怎么样?来自github.com/sickill/vim-pasta。或者你可以试试那个插件。
      • 该代码似乎有效,谢谢。我将用几种不同的格式对其进行测试....
      【解决方案3】:

      :nnoremap p p='] 与其他答案相同,但击键次数更少。无需视觉选择。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-16
        • 2010-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-27
        相关资源
        最近更新 更多