counsel-ag使用问题

对于counsel-ag,有两个问题想问一下:

1,必须输入3个字符才能够搜索,这个可以设置吗,比如两个或者1个。

2,有没有类似helm-ag的功能,可以指定搜索的起始目录。

(defun counsel-ag-function (string base-cmd extra-ag-args)
  "Grep in the current directory for STRING using BASE-CMD.
If non-nil, append EXTRA-AG-ARGS to BASE-CMD."
  (when (null extra-ag-args)
    (setq extra-ag-args ""))
  (if (< (length string) 3)
      (counsel-more-chars 3)
    (let ((default-directory (ivy-state-directory ivy-last))
          (regex (counsel-unquote-regex-parens
                  (setq ivy--old-re
                        (ivy--regex string)))))
      (let* ((args-end (string-match " -- " extra-ag-args))
             (file (if args-end
                       (substring-no-properties extra-ag-args (+ args-end 3))
                     ""))
             (extra-ag-args (if args-end
                                (substring-no-properties extra-ag-args 0 args-end)
                              extra-ag-args))
             (ag-cmd (format base-cmd
                             (concat extra-ag-args
                                     " -- "
                                     (shell-quote-argument regex)
                                     file))))
        (counsel--async-command ag-cmd)
        nil))))

这里的 cousel-more-chars 写死的是3,你如果想修改,可以自己去修改源码,作者貌似并没有提供 customize的选项,你可以去作者的 github repo 提issue。

  1. helm-ag 指定搜索目录是指 -G 后面跟的选项么?
1 个赞

输入helm-ag命令后,编辑器底部会显示一个路径,默认是当前路径,你输入Enter后便选择了起始搜索路径,然后输入需要查找的内容,counsel-ag则直接输入查找的内容,没有指定路径的步骤。

不想要三个字母,后面输入空格补上就可以了。 想要指定目录 ctrl+u 然后再使用counsel-ag就可以了

3 个赞

多谢,ctrl-u调用的哪个命令,不好意思我用的evil,ctrl-u是个prefix

SPC u.

给abo-abo 提个 issue?

1,必须输入3个字符才能够搜索,这个可以设置吗,比如两个或者1个。

补空格不行,空格也会被当成搜索关键字。

昨天给 abo-abo 提了一个 issue,他已经解决了,现在可以通过 counsel-more-chars-alist 来设置了。 详见: https://github.com/abo-abo/swiper/issues/1663#event-1733210302

我默认是使用vmacs-counsel-rg这个命令来启用rg 在当前目录下搜索,

如果想搜当前symbol,就使用C-.

如果想切换到git. root目录,就用C-;

C-o 或者 C-uC-; 则会让你选择目录

如果想向上层目录就C-l或C-h,基本满足我的需要,

另外也可以先切到对应的dired 再执行此命令 至于如果快速切换到想去的dired ,这是另一个话题 可以 package-install:ivy-dired-history https://github.com/jixiuf/ivy-dired-history

如果想实现helm-grep-do-grep. 那样 “foo !bar” 搜索foo ,但是过滤掉bar 则 (setq counsel-rg-base-command "~/.emacs.d/bin/rgwrapper -z %s ") ;这是用go 写的对rg的简单包装 https://github.com/jixiuf/vmacs/blob/master/bin/rgwrapper.go

(vmacs-leader "g" 'vmacs-counsel-rg)

  (define-key counsel-ag-map (kbd "C-l") 'vmacs-counsel-ag-up-directory)
  (define-key counsel-ag-map (kbd "C-o") 'vmacs-counsel-rg-select-directory)
  (define-key counsel-ag-map (kbd "C-;") 'vmacs-counsel-ag-toggle-git-root)
  (define-key counsel-ag-map (kbd "C-h") 'vmacs-counsel-ag-up-directory)

(define-key ivy-minibuffer-map (kbd "C-.") 'ivy-next-history-element)


;;;###autoload
(defun vmacs-counsel-rg (&optional arg)
  "Use `counsel-rg' to search for the selected region or
 the symbol around point in the current project with riggrep"
  (interactive "P")
  (let ((input (if (region-active-p)
                   (buffer-substring-no-properties
                    (region-beginning) (region-end))
                 ""))
        (default-directory default-directory)
        (extra-rg-args ""))
    (when current-prefix-arg
      (setq extra-rg-args
            (read-from-minibuffer (format
                                   "%s args: "
                                   (car (split-string counsel-ag-command))))))
    (counsel-rg  input default-directory extra-rg-args
                 (concat "rg in " (abbreviate-file-name default-directory)))))

;;;###autoload
(defun vmacs-counsel-ag-up-directory ()
  "Go to the parent directory."
  (interactive)
  (let* ((cur-dir (directory-file-name (abbreviate-file-name default-directory)))
         (up-dir (abbreviate-file-name (file-name-directory (expand-file-name cur-dir)))))
    (setf (ivy-state-directory ivy-last) up-dir)
    (when (string-suffix-p cur-dir  (directory-file-name(ivy-state-prompt ivy-last)))
      (setf (ivy-state-prompt ivy-last) (concat "rg in" up-dir)))
    (setq default-directory up-dir))
  (counsel-ag-function ivy-text))


(defvar vmacs-last-ag-directory default-directory)
;;;###autoload
(defun vmacs-counsel-ag-toggle-git-root ()
  "Toggle go to the git root directory."
  (interactive)
  (if current-prefix-arg
      (vmacs-counsel-rg-select-directory)
    (let (dir (vc-root (vc-find-root default-directory ".git")))
      (if (string= (expand-file-name default-directory)
                   (expand-file-name vc-root))
          (setq dir (abbreviate-file-name (or vmacs-last-ag-directory default-directory)))
        (setq dir (abbreviate-file-name vc-root)))
      (setf (ivy-state-directory ivy-last) dir)
      (setf (ivy-state-prompt ivy-last) (concat "rg in " dir))
      (setq vmacs-last-ag-directory default-directory)
      (setq default-directory dir))
    (counsel-ag-function ivy-text)))


(defvar vmacs-last-ivy-text )
;; (defvar vmacs-last-state )
;;;###autoload
(defun vmacs-counsel-rg-select-directory()
  " dynamicly select directory in counsel-ag session."
  (interactive)
  (setq vmacs-last-ivy-text (or ivy-text ""))
  ;; (setq vmacs-last-state ivy-last)
  (ivy-quit-and-run
    (let ((extra-rg-args ""))
      (setq default-directory (read-directory-name "rg in directory:"))
      (setq ivy-last vmacs-last-ivy-text)
      (counsel-rg  vmacs-last-ivy-text default-directory extra-rg-args
                   (concat "rg in " (abbreviate-file-name default-directory)))


      )
    ))

更多配置参考

2 个赞

[search-ag失效并提示Symbol’s function definition is void: counsel-more-chars]

(search-ag失效并提示Symbol’s function definition is void: counsel-more-chars)

我用Windows10下的Spacemacs 0.200.13 master版本,search-ag或者search-project-ag都不能使用,并提示if: Symbol’s function definition is void: counsel-more-chars ,本人小白,您方便具体说一下怎么解决吗?谢谢~

我不用spacemacs 你可以看下下载下来的包是什么版本的 试着更新一下看看