2
1

More than 5 years have passed since last update.

Emacsのibufferでcounselを使ってibuffer-find-fileのようなことをする

Posted at

モチベーション

ibufferでC-x C-fすると、実はfind-fileではなくibuffer-find-fileが呼び出されている。
これは普通のfind-fileと違い、ibufferの現在のカーソル位置のディレクトリでfind-fileをする。
たとえば、~/project/test.cの上にカーソルをおいてibuffer-find-fileすると、~/projectを初期値としてfind-fileが走る。

一方で、counsel-find-fileという便利な関数がある。
これはcounselパッケージが持つ関数の1つで、counselの便利なUIと補完環境の上でfind-fileと同様のことができる。

そこで、ibufferの上で、しかもibufferでのカーソル位置のディレクトリを認識してcounsel-find-fileできると便利である。
これを行う関数を今回作った。

コード

use-packageを使っている。
使わない場合は、関数を抜き出して、define-keyなどで好きなキーバインドに設定すればよい。

(use-package ibuffer
  :config
  ;; ibuffer-find-fileを使わずにcounselを使う
  (defun ibuffer-find-file-by-counsel ()
    "Like `counsel-find-file', but default to the directory of the buffer
at point."
    (interactive)
    (let ((default-directory
            (let ((buf (ibuffer-current-buffer)))
              (if (buffer-live-p buf)
                  (with-current-buffer buf
                    default-directory)
                default-directory))))
      (counsel-find-file default-directory)))
  :bind (("C-x C-b" . ibuffer)
         :map ibuffer-mode-map ("C-x C-f" . 'ibuffer-find-file-by-counsel)))

参照

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1