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

前端 未结 9 1610
星月不相逢
星月不相逢 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:33

    ;Here is my version: Emulates Visual Studio/Windows key bindings 
    ; C-F3 - Start searching the word at the point
    ; F3 searches forward and Shift F3 goes reverse
    
    (setq my-search-wrap nil)
    
    (defun my-search-func (dir)
      (interactive)
      (let* ((text (car search-ring)) newpoint)
            (when my-search-wrap  
                 (goto-char (if (= dir 1) (point-min) (point-max)))
                 (setq my-search-wrap nil))
            (setq newpoint (search-forward text nil t dir))
            (if newpoint
              (set-mark (if (= dir 1) (- newpoint (length text))
                             (+ newpoint (length text))))
              (message "Search Failed: %s" text) (ding)
              (setq my-search-wrap text))))
    
    (defun my-search-fwd () (interactive) (my-search-func 1))
    (defun my-search-bwd () (interactive) (my-search-func -1))
    
    (defun yank-thing-into-search ()
       (interactive)
       (let ((text (if mark-active
              (buffer-substring-no-properties (region-beginning)(region-end))
                     (or (current-word) ""))))
         (when (> (length text) 0) (isearch-update-ring text) (setq my-search-wrap nil)
                (my-search-fwd))))
    (global-set-key (kbd "")    'my-search-fwd)            ; Visual Studio like search keys
    (global-set-key (kbd "")  'my-search-bwd)
    (global-set-key (kbd "")  'yank-thing-into-search)
    
    

提交回复
热议问题