【问题标题】:Return to last cursor in VIM after indenting缩进后返回到vim中的最后一个光标
【发布时间】:2014-03-10 10:48:58
【问题描述】:

我使用gg=G 在 VIM 中自动格式化整个文件。整个文件缩进后,光标回到文件开头。

如何让 VIM 将光标返回到最后一个位置?假设我在第 45 行调用了命令,我希望 VIM 在缩进文件后返回到这一行。

编辑:如果有人感兴趣,我确实将以下命令添加到我的 .vimrc 中

" Autoformat document                                                                                                                     
nnoremap F gg=G''

因此,通过使用Shit+f,我现在可以重新格式化整个文档并保持在同一个光标处。

【问题讨论】:

  • 您可以创建一个映射到:1) 标记当前位置 2) 执行 gg=G 3) 返回标记位置。我曾经有过这样的事情,但我不能再资助相应的配置了:(

标签: vim indentation


【解决方案1】:

如 /u/nemo157 所述,在How do I fix the indentation of an entire file in Vi? 的回复评论中,发出'' 会将您返回到最后一个位置。根据 vim 文档:

                            *''* *``*
''  ``      To the position before the latest jump, or where the
            last "m'" or "m`" command was given.  Not set when the
            |:keepjumps| command modifier was used.
            Also see |restore-position|.

因此,如果您执行gg=G'',您将回到起点。

【讨论】:

    【解决方案2】:

    您可以手动设置一个命名标记(例如ma)并在使用`a重新缩进后返回该位置。

    或者,the anwolib plugin 提供了一个 :KeepView 命令,它使 any (Ex) 命令(所以你需要在这里用:normal! 翻译)保持当前视图:

    :KeepView normal! gg=G
    

    【讨论】:

      猜你喜欢
      • 2011-06-30
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多