关于 Emacs 中的向前和向后单词行为
我不知道我的设置是否有问题,但是当我按Mf(转发一个单词)时 我在哪里并不重要,它永远不会将光标放在下一个单词中(只是在单词之间)。 Mb 不会发生这种情况,它将光标放在前一个单词的开头。
这是正常行为吗?如何将光标置于以下单词的开头?
I don't know if there's something wrong with my settings but when I press M-f (forward a word)
it doesn't matter where I am, it never place the cursor in the next word (just between words). This doesn't happen with M-b which place my cursor in the beginning of the previous word.
Is this a normal behavior? How do I place my cursor at the beginning of the following word?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
所描述的宏解决方案是在会话中获得此行为的好方法,但如果这是您想要的默认行为,则有点不方便,因为每次启动 emacs 时都必须定义它。如果你想让 Mf 一直这样工作,你可以定义一个 elisp 函数并将其绑定到键上。将其放入您的 .emacs 文件中:
The macro solution described is a great way to get this behavior in a session, but it's a little inconvenient if that's the default behavior you want, since you have to define it every time you start emacs. If you want M-f to work like this all the time, you can define an elisp function and bind it to the key. Put this in your .emacs file:
好的,我们就清楚了,我假设您正在谈论命令 forward-word 和 backward-word 这些命令默认绑定到 Alt +f 和 Alt+b
例如字符串:“Hello dolly,我在这里”
如果您的光标位于“H”上的“Hello”,并且您执行forward-word,光标将移动到“Hello”和“dolly”之间的空格,但听起来您希望光标位于字母“d”上“多莉”而不是在它前面。
因此,向前单词执行两次,然后向后单词执行一次。
这会将光标放在“dolly”的“d”上。
这可以通过宏来自动化。
;; = 注释,不要输入它们
Ctrl+x ( ;;启动宏
Alt+f< /kbd> Alt+f Alt+b
Ctrl+x ) ;;end macro
然后要运行最后定义的宏,请执行以下操作:
Ctrl+x e
编辑:正如帕斯卡在评论中提到的,这也可以通过
Alt+f Ctrl+f
来完成您也可以将其放入宏中,无论哪种方式结果都是相同的。
Ok, just so we're clear, Im going to assume you are talking about the commands forward-word and backward-word these are bound by default to Alt+f and Alt+b
eg string: "Hello dolly I am here"
If your cursor is on the "H" of "Hello", and you do forward-word the cursor will move to the space between "Hello" and "dolly", but it sounds like you want the cursor to be on the letter "d" of "dolly" instead of in-front of it.
So, do forward-word twice, then backward-word once.
That will put your cursor on the "d" of "dolly".
This can be automated with a macro.
;; = comments, do not type them
Ctrl+x ( ;;start macro
Alt+f Alt+f Alt+b
Ctrl+x ) ;;end macro
Then to run last defined macro do this:
Ctrl+x e
EDIT: as pascal mentioned in a comment, this can also just be done with
Alt+f Ctrl+f
You could put that into a macro as well, either way the result is the same.
这是正确的行为。根据 Emacs 手册,“[f]向前运动在单词的最后一个字母之后停止,而向后运动在第一个字母之前停止。”
为什么会这样呢?也许是为了与kill-word (Md)保持一致。
That is correct behavior. According to the Emacs manual, "[f]orward motion stops right after the last letter of the word, while backward motion stops right before the first letter."
Why is it this way? Perhaps to be consistent with kill-word (M-d).
向前移动两次并向后移动一次就可以了,除非您位于前面有空格的行的开头。然后前进两次并后退一次将使您移动到下一个单词而不是第一个单词。下面的代码将完美模仿 vi 的“w”命令。我写得很快,所以可以进一步清理这段代码。
Moving forward twice and backwards once is fine unless you are at the beginning of a line with spaces in the front. Then going forward twice and back once will move you to the next word not the first word. The code below will mimic vi's "w" command perfectly. I've written this quite fast so this code can be cleaned up further.
尝试如下所示:
Try something like following:
您可以通过使用
misc.el
中的forward-to-word
和backward-to-word
来实现此行为。我将它们绑定到Meta-F/Meta-B
(即按下 Shift 键)。这些相当于forward-word/backward-word
的Meta-f/Meta-b
我的 .emacs 具有以下绑定
You can achieve this behavior by using the
forward-to-word
andbackward-to-word
found inmisc.el
. I have these bound toMeta-F/Meta-B
(i.e. with Shift pressed). These are equivalent toMeta-f/Meta-b
forforward-word/backward-word
My .emacs has the following bindings