【问题标题】:Save exact cursor position in the line在行中保存准确的光标位置
【发布时间】:2021-09-30 02:07:51
【问题描述】:

我正在寻找方法让 Vim 在我退出它或在缓冲区之间切换时保存我的确切光标位置。

我的配置中有以下配置:

autocmd BufReadPost *
  \ if line("'\"") > 0 && line ("'\"") <= line("$") |
  \ exe "normal g'\"" |
  \ endif

它只将光标返回到我的光标所在行的开头,而不是光标在其中的位置;即我的光标返回到行首,而不是它所在的位置。

有没有办法把光标的准确位置带回来?

【问题讨论】:

    标签: vim macvim


    【解决方案1】:

    您的 sn-p 似乎是在 :help restore-cursor 下找到的那个的变体,它可以按照您想要的方式工作:

    autocmd BufReadPost *
      \ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit'
      \ |   exe "normal! g`\""
      \ | endif
    

    区别在于您使用g'(“g 然后单引号”)与他们使用g`(“g 然后反引号”):

    • 前者跳转到标记"的行首,就像'a会跳转到标记a的行首,
    • 后者跳转到标记"的确切位置,就像`a会跳转到标记a的确切位置。

    请参阅:help mark-motions,更具体地说,请参阅:help g'

    【讨论】:

    • 这有助于并真正将光标带回确切位置。但只有当我退出 Vim 时,当我在缓冲区之间切换时,光标才会回到行首。
    • 看起来autocmd BufEnter * silent! normal! g`" 有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 2020-03-09
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多