reddit / emacs

How to change counsel-fzf-action to use find-file-other-window

pragmat1c1 ยท 2022-05-31 14:19
I am using u/abo-abo 's excellent package `counsel.el` a lot, and I am loving it. Now `counsel-fzf` uses `find-file` in `counsel-fzf-action` to open a file in the currently open window, but I'd rather use `find-file-other-window`. - Here's the code snippet from `counsel.el`: (defun counsel-fzf-action (x) "Find file X in current fzf directory." (with-ivy-window (let ((default-directory counsel--fzf-dir)) (find-file x)))) As you can see, it calls `find-file`. So how can I tell it to use `find-file-other-window`? - Now I could use an advice to overwrite this, but I am thinking that `counsel` has other mechanisms to accomplish this. I am thinking that `ivy-set-actions` should be able to accomplish what I want. Here is an example I came across: (ivy-set-actions 'counsel-fzf '(("j" find-file-other-window "other window") ("f" find-file-other-frame "other frame") ("b" counsel-find-file-cd-bookmark-action "cd bookmark") ("x" counsel-find-file-extern "open externally") ("r" counsel-find-file-as-root "open as root") ("R" find-file-read-only "read only") ("l" find-file-literally "open literally") ("k" counsel-find-file-delete "delete") ("c" counsel-find-file-copy "copy file") ("m" counsel-find-file-move "move or rename") ("d" counsel-find-file-mkdir-action "mkdir"))) But that requires that I hit `M-o` to call this menu, and then hit `j` for calling `find-file-other-window`. So how can I change the default behaviour? ## Edit: Here's what I did As a quick-fix I copied the relevant 3 functions, renamed them and added my desired functionality to them: ;;;;; change counsel-fzf to open file in other window (defun counsel-fzf-other-window (&optional initial-input initial-directory fzf-prompt) "Open a file using the fzf shell command. INITIAL-INPUT can be given as the initial minibuffer input. INITIAL-DIRECTORY, if non-nil, is used as the root directory for search. FZF-PROMPT, if non-nil, is passed as `ivy-read' prompt argument." (interactive (let ((fzf-basename (car (split-string counsel-fzf-cmd)))) (list nil (when current-prefix-arg (counsel-read-directory-name (concat fzf-basename " in directory: ")))))) (counsel-require-program counsel-fzf-cmd) (setq counsel--fzf-dir (or initial-directory (funcall counsel-fzf-dir-function))) (ivy-read (or fzf-prompt "fzf: ") #'counsel-fzf-function :initial-input initial-input :re-builder #'ivy--regex-fuzzy :dynamic-collection t :action #'counsel-fzf-action-other-window :caller 'counsel-fzf-other-window)) (ivy-configure 'counsel-fzf-other-window :occur #'counsel-fzf-occur :unwind-fn #'counsel-delete-process :exit-codes '(1 "Nothing found"))