Mac OS X 终端中的 Vim:逐字移动光标

发布于 2024-10-23 00:48:09 字数 338 浏览 3 评论 0原文

我已按照 OS X 中的指导进行操作终端逐字移动光标? 现在在终端中我可以逐字移动光标。

然而,在 Vim 中,我只能逐字向后移动。无论我将 \033f 设置为 option+cursor-right 还是 shift+cursor-right,我都无法逐字前进。我想到的唯一解决方法是进入正常模式并单击 移动到下一个单词。知道如何解决这个问题吗?谢谢。

I have followed the guidance in Is there any way in the OS X Terminal to move the cursor word by word? and now in terminal I can move cursor word by word.

However, in Vim, I am only able to move backward word by word. I cannot move forward word by word, no matter I set \033f to option+cursor-right or shift+cursor-right. The only workaround I figured out is to go to normal mode and click <w> to move to next word. Any idea about how to fix that? Thanks.

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

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

发布评论

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

评论(4

闻呓 2024-10-30 00:48:09

w 不是解决方法。这是逐字移动的主要方式(另请参见:WbBe、<代码>E)。您想要的是一种根本无法帮助您学习 Vim 的解决方法。

w is not a workaround. It's the primary way to move word by word (see also: W, b, B, e, E). What you want is a workaround that won't help you learn Vim at all.

漫雪独思 2024-10-30 00:48:09

有些人可能会认为使用 VIM 本机键盘快捷键 移动并不是一种解决方法,但对我来说当然既非常不方便又非常低效。特别是您需要在插入模式和正常模式之间切换才能移动到下一个单词的部分。

这就是为什么我想出了一个基于 这个答案关于超级用户。这个想法是将 Terminal.app 提供的输入直接映射到 VIM 中。 SU 上的答案显示了要在 vimrc 文件中放入哪些内容,以使 Home/End 键按预期工作。

我的以下调整版本包括单词上的 Option+arrow(或 Alt+arrow)导航。我试图尽可能模仿终端移动的行为。因此,按 Option+Right (Alt+Right) 会将插入符号移动到单词后的下一个字符(而不是单词的最后一个字符,即 VIM < code>w 本机行为)。

let tp=$TERM_PROGRAM
if tp == 'Apple_Terminal'
  :" map Mac OS X Terminal.app

  " map Home/End:
  :map <ESC>[H <Home>
  :map <ESC>[F <End>
  " small 'o' letter in <C-o> means no exit from the insert mode
  :imap <ESC>[H <C-o><Home>
  :imap <ESC>[F <C-o><End>
  :cmap <ESC>[H <Home>
  :cmap <ESC>[F <End>

  " map Option+Left/Option+Right:
  " for this to work you must have the bindings in Settings > Keyboard set
  " as follows:
  " 'option cursor left' to '\033b'
  " 'option cursor right' to '\033f'
  :map <ESC>f el
  :imap <ESC>b <C-o>b
  :imap <ESC>f <C-o>el
  :cmap <ESC>f el
endif

这是一个虽小但很重要的好处,您无需退出插入模式即可获得Home/End导航。在 10.8.5 上测试。

Some may argue that moving around with VIM native keyboard shortcuts is not a workaround, but for me it sure is both very inconvenient and very inefficient. Especially the part where you need to switch between insert mode and normal mode just to move to the next word.

That's why I came up with a solution based on this answer on SuperUser. The idea is to map input provided by the Terminal.app directly in VIM. The answer on SU shows what to put into your vimrc file for the Home/End keys to work as expected.

My tweaked version below includes the Option+arrow (or Alt+arrow) navigation on words. I've tried to mimic the behavior of Terminal's moving around as close as I could get. So pressing Option+Right (Alt+Right) will move the caret to the next character after the word (as opposed to the last character of the word, which is VIMs w native bahavior).

let tp=$TERM_PROGRAM
if tp == 'Apple_Terminal'
  :" map Mac OS X Terminal.app

  " map Home/End:
  :map <ESC>[H <Home>
  :map <ESC>[F <End>
  " small 'o' letter in <C-o> means no exit from the insert mode
  :imap <ESC>[H <C-o><Home>
  :imap <ESC>[F <C-o><End>
  :cmap <ESC>[H <Home>
  :cmap <ESC>[F <End>

  " map Option+Left/Option+Right:
  " for this to work you must have the bindings in Settings > Keyboard set
  " as follows:
  " 'option cursor left' to '\033b'
  " 'option cursor right' to '\033f'
  :map <ESC>f el
  :imap <ESC>b <C-o>b
  :imap <ESC>f <C-o>el
  :cmap <ESC>f el
endif

As a small, but significant bonus you get Home/End navigation without exiting the insert mode. Tested on 10.8.5.

美胚控场 2024-10-30 00:48:09

让我为你澄清一些事情。 Bash(在终端内运行的 shell 程序)具有您所习惯的行为。 (使用 Alt+f 向前移动一个单词,使用 Alt+b 向后移动一个单词。)这最初是为了像 Emacs 那样做的。您可以使用该命令

set -o vi

切换到 vim 行为。在此模式下,您可以使用 Esc 切换到普通模式,并像在 vim 中一样移动;然后按 i 返回插入模式。

因此,如果 Vim 没有尝试像 Emacs 那样行事,请不要感到惊讶。 vim 背后的全部力量就在于这些简单的动作。

Let me clear a few things up for you. Bash (the shell program running inside the terminal) has the behavior you are used to. (Using Alt+f to move forward by word, and Alt+b to move backward by word.) This was originally done to be like Emacs. You can use the command

set -o vi

to switch to vim behavior. In this mode, you can use Esc to switch to normal mode, and move around like in vim; then press i to go back to insert mode.

So don't be surprised that Vim isn't trying to act like Emacs. The whole power behind vim lies behind these simple motions.

把回忆走一遍 2024-10-30 00:48:09

截至 11 月 23 日,这些简单的快捷方式对我来说适用于在终端应用程序中打开 vim(插入模式)。这可以 OOTB 运行,不需要编辑 rc 文件等。

Shift+left/right - 逐字移动

Fn+Shift+left/right - 跳转到行首或行尾

Fn+Shift+up/down - 向上翻页/向下翻页

As of Nov'23, these simple shortcuts work for me with vim (insert mode) open in Terminal app. This works OOTB, no editing of rc files etc. is needed.

Shift+left/right - move word by word

Fn+Shift+left/right - jump to beginning or end of the line

Fn+Shift+up/down - page up/page down

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文