关于 Emacs 中的向前和向后单词行为

发布于 2024-08-18 06:39:05 字数 133 浏览 2 评论 0原文

我不知道我的设置是否有问题,但是当我按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 技术交流群。

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

发布评论

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

评论(6

慵挽 2024-08-25 06:39:05

所描述的宏解决方案是在会话中获得此行为的好方法,但如果这是您想要的默认行为,则有点不方便,因为每次启动 emacs 时都必须定义它。如果你想让 Mf 一直这样工作,你可以定义一个 elisp 函数并将其绑定到键上。将其放入您的 .emacs 文件中:

(defun next-word (p)
   "Move point to the beginning of the next word, past any spaces"
   (interactive "d")
   (forward-word)
   (forward-word)
   (backward-word))
(global-set-key "\M-f" 'next-word)

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:

(defun next-word (p)
   "Move point to the beginning of the next word, past any spaces"
   (interactive "d")
   (forward-word)
   (forward-word)
   (backward-word))
(global-set-key "\M-f" 'next-word)
故人爱我别走 2024-08-25 06:39:05

好的,我们就清楚了,我假设您正在谈论命令 forward-wordbackward-word 这些命令默认绑定到 Alt +fAlt+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.

无声情话 2024-08-25 06:39:05

这是正确的行为。根据 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).

冬天旳寂寞 2024-08-25 06:39:05

向前移动两次并向后移动一次就可以了,除非您位于前面有空格的行的开头。然后前进两次并后退一次将使您移动到下一个单词而不是第一个单词。下面的代码将完美模仿 vi 的“w”命令。我写得很快,所以可以进一步清理这段代码。

(defun forward-word-to-beginning (&optional n)
  "Move point forward n words and place cursor at the beginning."
  (interactive "p")
  (let (myword)
    (setq myword
      (if (and transient-mark-mode mark-active)
        (buffer-substring-no-properties (region-beginning) (region-end))
        (thing-at-point 'symbol)))
    (if (not (eq myword nil))
      (forward-word n))
    (forward-word n)
    (backward-word n)))

(global-set-key (kbd "M-C-f") 'forward-word-to-beginning)

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.

(defun forward-word-to-beginning (&optional n)
  "Move point forward n words and place cursor at the beginning."
  (interactive "p")
  (let (myword)
    (setq myword
      (if (and transient-mark-mode mark-active)
        (buffer-substring-no-properties (region-beginning) (region-end))
        (thing-at-point 'symbol)))
    (if (not (eq myword nil))
      (forward-word n))
    (forward-word n)
    (backward-word n)))

(global-set-key (kbd "M-C-f") 'forward-word-to-beginning)
七堇年 2024-08-25 06:39:05

尝试如下所示:

;; replace common word-operations on same-syntax-operations

(require 'thingatpt)

(global-set-key "\M-f" 'forward-same-syntax)

(global-set-key "\M-b" (lambda() 
                         (interactive)
                         (forward-same-syntax -1)))

(defun kill-syntax (&optional arg)
  "Kill ARG sets of syntax characters after point."
  (interactive "p")
  (let ((opoint (point)))
    (forward-same-syntax arg)
    (kill-region opoint (point))))

(global-set-key "\M-d" 'kill-syntax)

(global-set-key [(meta backspace)] (lambda()
                                     (interactive) 
                                     (kill-syntax -1)))

Try something like following:

;; replace common word-operations on same-syntax-operations

(require 'thingatpt)

(global-set-key "\M-f" 'forward-same-syntax)

(global-set-key "\M-b" (lambda() 
                         (interactive)
                         (forward-same-syntax -1)))

(defun kill-syntax (&optional arg)
  "Kill ARG sets of syntax characters after point."
  (interactive "p")
  (let ((opoint (point)))
    (forward-same-syntax arg)
    (kill-region opoint (point))))

(global-set-key "\M-d" 'kill-syntax)

(global-set-key [(meta backspace)] (lambda()
                                     (interactive) 
                                     (kill-syntax -1)))
红墙和绿瓦 2024-08-25 06:39:05

您可以通过使用 misc.el 中的forward-to-wordbackward-to-word 来实现此行为。我将它们绑定到 Meta-F/Meta-B (即按下 Shift 键)。这些相当于 forward-word/backward-wordMeta-f/Meta-b

我的 .emacs 具有以下绑定

(global-set-key (kbd "M-F") #'forward-to-word)
(global-set-key (kbd "M-B") #'backward-to-word)

You can achieve this behavior by using the forward-to-word and backward-to-word found in misc.el. I have these bound to Meta-F/Meta-B (i.e. with Shift pressed). These are equivalent to Meta-f/Meta-b for forward-word/backward-word

My .emacs has the following bindings

(global-set-key (kbd "M-F") #'forward-to-word)
(global-set-key (kbd "M-B") #'backward-to-word)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文