How can I emulate Vim's * search in GNU Emacs?

前端 未结 9 1531
星月不相逢
星月不相逢 2020-12-23 16:57

In Vim the * key in normal mode searches for the word under the cursor. In GNU Emacs the closest native equivalent would be:

C-s C-w

But th

相关标签:
9条回答
  • 2020-12-23 17:38

    There are lots of ways to do this:

    http://www.emacswiki.org/emacs/SearchAtPoint

    0 讨论(0)
  • 2020-12-23 17:47

    Based on your feedback to my first answer, how about this:

    (defun my-isearch-word-at-point ()
      (interactive)
      (call-interactively 'isearch-forward-regexp))
    
    (defun my-isearch-yank-word-hook ()
      (when (equal this-command 'my-isearch-word-at-point)
        (let ((string (concat "\\<"
                              (buffer-substring-no-properties
                               (progn (skip-syntax-backward "w_") (point))
                               (progn (skip-syntax-forward "w_") (point)))
                              "\\>")))
          (if (and isearch-case-fold-search
                   (eq 'not-yanks search-upper-case))
              (setq string (downcase string)))
          (setq isearch-string string
                isearch-message
                (concat isearch-message
                        (mapconcat 'isearch-text-char-description
                                   string ""))
                isearch-yank-flag t)
          (isearch-search-and-update))))
    
    (add-hook 'isearch-mode-hook 'my-isearch-yank-word-hook)
    
    0 讨论(0)
  • 2020-12-23 17:49

    How about built in commands M-b C-s C-w (start of word,search,word search)

    0 讨论(0)
提交回复
热议问题