Skip to content

Latest commit

 

History

History
356 lines (251 loc) · 14.6 KB

search-commands.org

File metadata and controls

356 lines (251 loc) · 14.6 KB

keybindings

Let’s use s-/ f as a prefix for searching filenames, and s-/ g as a prefix for searching full text using grep

Using s-/ f and s-/ g as prefix keybindings for searching filenames and full text, respectively, is a logical choice.

search

(define-key key-minor-mode-map (kbd "s-/ gr") 'consult-grep)

;; Search
(global-set-key (kbd "s-/ dg") 'deadgrep-current-directory) ; not incremental. but nicely formatted. lays it all out nicely in a buffer.
(global-set-key (kbd "s-/ pa") 'counsel-projectile-ag) ; as an alternative to deadgrep check out ag so maybe it's better

(global-set-key (kbd "s-/ rg") 'consult-ripgrep-current-directory) ; pretty slick, shows you the actual file context

(global-set-key (kbd "s-k rg") 'consult-ripgrep-current-directory) ; pretty slick, shows you the actual file context


(global-set-key (kbd "s-/ gg") 'consult-git-grep) ; pretty great, like projectile, doesn't respect .projectile

;; (global-set-key (kbd "s-/ st") 'consult-) ;
;; (global-set-key (kbd "s-/ l") 'counsel-) ;

(global-set-key (kbd "s-/ rr") 'roam-rg-search)

roam-rg-search

Source: Using consult-ripgrep with org-roam for searching notes - How To - Org-roam

(defun roam-rg-search ()
 "Search org-roam directory using consult-ripgrep. With live-preview."
 (interactive)
 (let ((consult-ripgrep-command "rg --null --ignore-case --type org --line-buffered --color=always --max-columns=500 --no-heading --line-number . -e ARG OPTS"))
 (consult-ripgrep org-roam-directory)))

(defun counsel-ag-roam ()
 "Do counsel-ag on the org roam directory"
 (interactive)
 (counsel-ag nil org-roam-directory))

consult notes

Can set paths later. Instrctions here: mclear-tools/consult-notes: Use consult to search notes

(use-package consult-notes
 :commands (consult-notes
       consult-notes-search-in-all-notes
       ;; if using org-roam
       consult-notes-org-roam-find-node
       consult-notes-org-roam-find-node-relation)
 :config
 (setq consult-notes-file-dir-sources '(("Name" ?key "path/to/dir"))) ;; Set notes dir(s), see below
 ;; Set org-roam integration, denote integration, or org-heading integration e.g.:
 (setq consult-notes-org-headings-files '("~/path/to/file1.org"
                      "~/path/to/file2.org"))
 (consult-notes-org-headings-mode)
 (when (locate-library "denote")
  (consult-notes-denote-mode)))

searching current directory instead of project directory

By default, consult-ripgrep it uses the project directory of the current buffer. If you want to change it to the current directory, you can either pass it explicitly as an argument, or use a prefix argument (C-u) to prompt for the directory2.

You can define a custom function that calls consult-ripgrep with the current directory as follows:

(defun consult-ripgrep-current-directory ()
 "Search current directory using consult-ripgrep."
 (interactive)
 (consult-ripgrep default-directory))


(defun deadgrep-current-directory ()
 "Search current directory using deadgrep."
 (interactive)
 (let ((search-term (read-string "Search term: ")))
  (deadgrep search-term default-directory)))

(defalias 'deadgrep 'deadgrep-project-directory)

(defalias 'rg-current-directory 'rg-dwim-current-dir)
(defalias 'rg-project-directory 'rg-dwim-project-dir)
(defalias 'rg-current-file 'rg-dwim-current-file)

consult org roam

consult-org-roam.el provides several functions to connect org-roam to consult’s completing read interface.

First, it provides the following standalone functions which enhance org-roam’s capabilities:

consult-org-roam-file-find
Search your org-roam files with consult’s completing-read and its live preview
consult-org-roam-backlinks
List backlinks to org-roam-node-at-point (e.g. currently open note) and sift through them with consult’s completing-read and its live preview
consult-org-roam-forward-links
List forward links contained in the currently opened note
consult-org-roam-search
Asynchronously search your roam-directory with grep or ripgrep

Second, it provides a minor mode called consult-org-roam-mode. When activated, org-roam-node-read is overridden, which is used by org-roam-node-find, org-roam-node-insert and org-roam-refile. By doing so, all functions utilizing completing-read resort to consult for performing completion. Furthermore, the same is done for org-roam-ref-read so that consult is used for completing references as well.

Eventually, you might want to suppress previewing for certain functions. This can be done by adding using consult-customize.

 (use-package consult-org-roam
  :ensure t
  :after org-roam
  :init
  (require 'consult-org-roam)
  ;; Activate the minor mode
  (consult-org-roam-mode 1)
  :custom
  ;; Use `ripgrep' for searching with `consult-org-roam-search'
  (consult-org-roam-grep-func #'consult-ripgrep)
  ;; Configure a custom narrow key for `consult-buffer'
  (consult-org-roam-buffer-narrow-key ?r)
  ;; Display org-roam buffers right after non-org-roam buffers
  ;; in consult-buffer (and not down at the bottom)
  (consult-org-roam-buffer-after-buffers t)
  :config
  ;; Eventually suppress previewing for certain functions
  (consult-customize
  consult-org-roam-forward-links
  :preview-key (kbd "M-."))
  :bind
  ;; Define some convenient keybindings as an addition
  ;; ("C-c n e" . consult-org-roam-file-find)
  ;; ("C-c n b" . consult-org-roam-backlinks)
  ;; ("C-c n l" . consult-org-roam-forward-links)
  ;; ("C-c n r" . consult-org-roam-search)
)

keybindings

;; (define-key key-minor-mode-map (kbd "s-G") 'counsel-projectile-grep); this fails to ignore files specified in .projectile
;; (define-key key-minor-mode-map (kbd "s-G") 'projectile-grep) ; this successfully ignores those files but isn't incremental

(define-key key-minor-mode-map (kbd "C-s-g ") 'consult-ripgrep-current-directory)
(define-key key-minor-mode-map (kbd "s-G") 'counsel-projectile-ag)

affe

(use-package affe
 :config
 ;; Manual preview key for `affe-grep'
 (consult-customize affe-grep :preview-key "M->"))

(use-package ffap)
(defun find-file-at-point-or-affe-find ()
 "Open the file at point if one exists, otherwise run affe-find."
 (interactive)
 (let ((filename (ffap-file-at-point))) ; Check if there is a file at point
 (if filename
  (find-file filename) ; Open file at point if it exists
  (affe-find)))) ; Otherwise, run affe-find

(define-key key-minor-mode-map (kbd "C-x C-f") 'find-file-at-point-or-affe-find)

affe: search full text within frequently used directories

(defun affe-grep-gnulisp-directory (&optional initial)
 "Fuzzy grep in the /Users/jay/gnulisp directory with optional INITIAL input."
 (interactive "P")
 (affe-grep "/Users/jay/gnulisp" initial))

(defun affe-grep-yasnippets-directory (&optional initial)
 "Fuzzy grep in the /Users/jay/gnulisp directory with optional INITIAL input."
 (interactive "P")
 (affe-grep "/Users/jay/emacs/interesting-snippets/org-mode" initial))

(define-key key-minor-mode-map (kbd "s-k g l") 'affe-grep-gnulisp-directory)

(define-key key-minor-mode-map (kbd "s-/ g l") 'affe-grep-gnulisp-directory)

(define-key key-minor-mode-map (kbd "s-/ g ub") 'affe-grep-bash-scripts)
(define-key key-minor-mode-map (kbd "s-/ g up") 'affe-grep-bash-profile)

;; (define-key key-minor-mode-map (kbd "s-/ g r") 'consult-org-roam-search)
;; (define-key key-minor-mode-map (kbd "s-/ g r") 'affe-grep-org-roam)


search by filename within frequently used directories

(defun counsel-find-file-in-yasnippets ()
 "Use counsel-find-file to search for files in the org-mode snippets directory."
 (interactive)
 (let ((default-directory "/Users/jay/emacs/interesting-snippets/org-mode/"))
 (counsel-find-file)))

(define-key key-minor-mode-map (kbd "s-k y a") 'affe-grep-gnulisp-directory)

book search

search filenames of book directory and proposal directory

📁 Proposal: /Users/jay/Dropbox/writing/proposal

📁 Book: /Users/jay/Dropbox/writing/book

(defun fzf-find-file--proposal-directory ()
 "Use counsel-fzf to search for files in the /Users/jay/Dropbox/writing/proposal/ directory."
 (interactive)
 ;; Use counsel-fzf with the specified directory as the root for searching.
 (counsel-fzf nil "/Users/jay/Dropbox/writing/proposal/"))

(defalias 'search-filename-proposal-directory 'fzf-find-file--proposal-directory)

(define-key key-minor-mode-map (kbd "s-/ f p") 'search-filename-proposal-directory)


(defun fzf-find-file--book-directory ()
 "Use counsel-fzf to search for files in the /Users/jay/Dropbox/writing/book/ directory."
 (interactive)
 ;; Use counsel-fzf with the specified directory as the root for searching.
 (counsel-fzf nil "/Users/jay/Dropbox/writing/book/"))

(defalias 'search-filename-book-directory 'fzf-find-file--book-directory)


(define-key key-minor-mode-map (kbd "s-/ f B") 'search-filename-book-directory)

simultaneously search both directories

In this function:

  • directory-files-recursively is used to get a list of all files in each directory (dir1 and dir2).
  • These lists are concatenated into all-files.
  • ivy-read is then used to create an interactive selection interface with the combined file list.

Explanation of How the Code Works:

  1. Interactive Declaration: (interactive) makes this function a command that can be called interactively within Emacs, for example, using M-x.
  2. Directory Definitions: The paths /Users/jay/Dropbox/writing/proposal/ and /Users/jay/Dropbox/writing/book/ are set as dir1 and dir2 respectively. These represent the two directories you want to search.
  3. Listing Files:
    • directory-files-recursively is used for both dir1 and dir2 to create a list of all files within these directories and their subdirectories. The empty string "" argument means it lists all types of files without filtering by extension.
  4. Combining File Lists: The lists files1 and files2 are combined into a single list all-files using append. This list now contains file paths from both directories.
  5. Interactive File Selection:
    • ivy-read is used to create an interactive selection interface. It takes all-files as the list of choices to present to the user.
    • The prompt "Find file: " is displayed to the user.
  6. File Selection Action:
    • The :action argument defines what to do when the user selects a file.
    • It uses a lambda function (lambda (f) (when f (find-file f))) to open the selected file. The when statement ensures that an action is only taken if a file is actually selected (i.e., f is not nil).

This function provides a user-friendly way to search and open files from two different directories using Emacs’s ivy completion system, offering a seamless integration with your Emacs workflow.

(defun counsel-fzf-both-proposal-and-book-dirs ()
  "Search in both /Users/jay/Dropbox/writing/proposal/ and /Users/jay/Dropbox/writing/book/ directories."
  (interactive)
  ;; Define the directories to search in as dir1 and dir2.
  (let* ((dir1 "/Users/jay/Dropbox/writing/proposal/")
         (dir2 "/Users/jay/Dropbox/writing/book/")
         ;; Use directory-files-recursively to list all files in dir1.
         ;; This function recursively lists files in a directory and its subdirectories.
         (files1 (directory-files-recursively dir1 ""))
         ;; Similarly, list all files in dir2.
         (files2 (directory-files-recursively dir2 ""))
         ;; Combine the file lists from both directories into all-files.
         (all-files (append files1 files2)))
    ;; Use ivy-read to present an interactive interface with the combined file list.
    ;; ivy-read is a part of the ivy completion framework, providing a simple and
    ;; efficient way to select an item from a list.
    (ivy-read "Find file: " all-files
              ;; Define an action to be performed when a file is selected.
              ;; In this case, it opens the selected file with find-file.
              :action (lambda (f) (when f (find-file f))))))


(defalias 'search-filename-both-book-and-proposal-directories 'counsel-fzf-both-proposal-and-book-dirs)

(define-key key-minor-mode-map (kbd "s-/ f B") 'search-filename-book-directory)

(define-key key-minor-mode-map (kbd "s-/ f b") 'search-filename-both-book-and-proposal-directories)

search full text of book directory and proposal directory

;; works but is overly complex. Do not use. use counsel-projectile-ag instead.
(defun rg-search-book-and-proposal-dirs (&optional initial-input)
 "Search using ripgrep in the book and proposal directories with optional INITIAL-INPUT."
 (interactive "sEnter search pattern: ")
 (let* ((input (if (and initial-input (not (equal initial-input "")))
          initial-input
         ".*"))
     (rg-cmd (concat "rg --vimgrep --color=always --hidden -g '!.git' '"
             input "' "
             "/Users/jay/Dropbox/writing/book "
             "/Users/jay/Dropbox/writing/proposal")))
  (compilation-start rg-cmd 'grep-mode)))

;; don't need any of this. I put both directories in the same projectile project ("both"), so just use counsel-projectile-ag


(defalias 'grep-both-directories 'counsel-projectile-ag)
(defalias 'grep-book-directory 'consult-ripgrep-current-directory)
(defalias 'grep-proposal-directory 'consult-ripgrep-current-directory)


(define-key global-map (kbd "s-/ g b") 'grep-both-directories)

(defun projectile-display-project-root ()
 "Show the root directory of the current Projectile project."
 (interactive)
 (if (projectile-project-p)
   (message "Current project root: %s" (projectile-project-root))
  (message "Not in a project")))

counsel-projectile-ag

(setq counsel-projectile-find-file-matcher #'counsel--find-file-matcher)

(setq counsel-find-file-ignore-regexp "\\.html")