6

Very similar to this question. I'm using helm-projectile to find files. My Emacs frame is divided in three vertical column windows. In one of the windows I have an open file from the project I'm working on, so I invoke helm-projectile and I can find other project files. However if I open the file, it opens in the current window.

What I would like to do is very similar to finding the file with helm and then C-c o to open it in another window, but I would like to tell Emacs which window I want to open the file in, maybe similar to how ace-window allows you to choose the window to jump to if your current frame is split in more than 2 windows.

How can this be done?

Cesar
  • 163
  • 5

1 Answers1

6

Allowing user to custom action is a great feature of helm, for example, to use ace-window to select a window for buffer to switch, all you need to do is implement it as an action:

(defun helm-buffer-ace-window (buffer)
  "Use ‘ace-window’ to select a window to display BUFFER."
  (ace-select-window)
  (switch-to-buffer buffer))

then use this action:

(add-to-list 'helm-type-buffer-actions
             '("Switch to buffer in Ace window ‘C-c C-e'" . helm-buffer-ace-window)
             :append)

Now, you should be able to use this new action from all helm sources which use helm-type-buffer-actions such as in C-c p b (helm-projectile-switch-to-buffer) and helm-buffers-list.


(Optional) Set up Key Binding for Action

You already can invoke the above action helm-buffer-ace-window from helm's Action Menu, if you like, it is also possible to assign a key binding for it, for example, C-c C-e

Firstly, wrap that action with a interactive command:

(defun helm-buffer-run-ace-window ()
  (interactive)
  (with-helm-alive-p
    (helm-exit-and-execute-action 'helm-buffer-ace-window)))

Secondly, bind C-c C-e to the command:

(define-key helm-buffer-map (kbd "C-c C-e") #'helm-buffer-run-ace-window)

The rest of code for Files:

(defun helm-find-ace-window (file)
  "Use ‘ace-window' to select a window to display FILE."
  (ace-select-window)
  (find-file file))

(add-to-list 'helm-find-files-actions
             '("Find File in Ace window" . helm-find-ace-window)
             :append)

(defun helm-file-run-ace-window ()
  (interactive)
  (with-helm-alive-p
    (helm-exit-and-execute-action 'helm-find-ace-window)))

;;; For `helm-find-files'
(define-key helm-find-files-map (kbd "C-c C-e") #'helm-file-run-ace-window)

;; For file commands in `helm-projectile'
;; NOTE: You have to restart Emacs to make following work since helm-projectile
;; can't recompute at-run-time, this is bad, it also has other issues, so I
;; don't use this package.
(define-key helm-projectile-find-file-map (kbd "C-c C-e") #'helm-file-run-ace-window)
xuchunyang
  • 14,302
  • 1
  • 18
  • 39
  • This does looks what I'm looking for, however I must be doing something wrong, as I can't see the action candidate listed when I do `helm-projectile-find-file` then TAB on a file name. The option is added to `'helm-type-buffer-actions` as I can see the new list element if I print the list, but it is not offered as an option within helm custom actions. – Cesar Oct 02 '15 at 23:05
  • buffer and file are treated differently, like `helm-buffer-ace-window` for buffers, you also have to add `helm-find-ace-window` to related file source's action like `helm-projectile-file-actions` or `helm-find-files-actions`, but helm-projectile looks like problematic: once `(helm-projectile-on)` gets called, it doesn't care about `helm-projectile-file-actions` anymore, so you need to modify it early, for example, use `(with-eval-after-load ...)` in your init.el. – xuchunyang Oct 03 '15 at 03:35
  • I'm marking it as accepted even though I can't make it work with `helm-projectile`. But it works flawlessly with `helm-find-files` and its awesome! Thank you very much! – Cesar Oct 03 '15 at 05:30
  • I have to make an answer because I don't have enough reputation to comment. xuchunyang's answer has a typo to be aware of. Just change this code: ``` (defun helm-file-run-ace-window () (interactive) (with-helm-alive-p (helm-exit-and-execute-action 'helm-file-ace-window)))` ``` To this: ``` (defun helm-file-run-ace-window () (interactive) (with-helm-alive-p ; the above code misspelled the function name (helm-exit-and-execute-action 'helm-find-ace-window))) ``` – Nicholas Hubbard May 16 '21 at 16:05
  • @NicholasHubbard Typo fixed. Thanks. – xuchunyang May 17 '21 at 15:51