The eval
solution will work, but this is a lot of stuff to type on the command-line if you use this search frequently. We can actually change the command-line parameters so that --search <string>
starts up with Emacs. Just modify your initialization file with this (if you are using lexical-binding
):
(add-to-list 'command-switch-alist '("--search" . command-line-search))
(defun command-line-search (_switch)
(let ((search-text (pop command-line-args-left)))
(add-hook ; Use a hook that runs after files load. Otherwise, your CLI
; option will have to be given AFTER any buffers that you wish to
; search, which is not great.
'window-setup-hook
#'(lambda ()
(isearch-forward
nil ; This should be t if you want regex searching.
t) ; This must be t, or the call will block, preventing the next
; line.
(isearch-yank-string search-text)))))
If you have not set lexical-binding
(the default), you have to live with a global variable, but this will do the trick:
(add-to-list 'command-switch-alist '("--search" . command-line-search))
(defvar search-text nil
"Initial text given from the CLI that will be searched.")
(defun command-line-search (_switch)
(setq search-text (pop command-line-args-left))
(add-hook ; Use a hook that runs after files load. Otherwise, your CLI option
; will have to be given AFTER any buffers that you wish to search,
; which is not great.
'window-setup-hook
#'(lambda ()
(isearch-forward
nil ; This should be t if you want regex searching.
t) ; This must be t, or the call will block, preventing the next line.
(isearch-yank-string search-text))))
Now all you have to do is emacs --search <search_term> <file>
like you wanted.
--eval
, which can take a script to startisearch
, and that can be put into a wrapper script.--eval
states "evaluate Emacs Lisp expression EXPR". Do you have an example for this (or a link to examples I can dive into)? I am still getting used to the editor and scripting in lisp is something I have never seen before I touched Emacs. I did manage (somehow) the startup configuration, so Emacs opens in a way I want and I understood most of what I did there.emacs +$(grep -hn "<search>" <file> | head -n 1 | cut -d":" -f1) <file>
. Just wanted to avoid the extra commands to get the line numbers if it would work within Emacs natively.