LoginSignup
9
7

More than 5 years have passed since last update.

Emacsでファイルパスから中身を透視する

Posted at

recentf などを実行した時に、ファイルの中身がわからず、確認する手間が発生するときがあります。
そこで、ファイルパスから中身を透視(second sight)できる package を作ってみました。

本体は GitHub/second-sight に置いておきます。PR/issue 待ってます。

これは何?

ファイルパスからその中身を、ファイルを開かずに確認できます。

thing-at-point での例
thing-at-point

counsel-find-file での例
counsel

何が嬉しいの?

buffer を開く手間を減らせます!
buffer 移動をせずに中身を見られるので、buffer 迷子にならずに済みます!

セットアップ

事前準備

この package は posframe.el を使っています。
そのため posframe.el を事前にインストールする必要があります。

本設定

git clone して init.el を以下のように編集するだけです。

(add-to-list 'load-path "/path/to/second-sight")
(require 'second-sight)

;; 必須ではないですが、おすすめです
(global-set-key (kbd "YOUR KEY") 'second-sight)

使い方

基本

以下を評価すると、ファイルの中身が記述された posframe が表示されます。

(second-sight-file "filepath")

また、カーソルの下にファイルパスがある状態で M-x second-sight を実行しても posframe が表示されます。

"/pa|th/to/file"
↑ your cursor

posframe を消したい場合は、もう一度式を評価するか M-x second-sight を実行すればOKです。

ivy/counsel との連携

;; この設定が必要
(global-set-key (kbd "YOUR KEY") 'second-sight)

この package は部分的に ivy/counsel をサポートしてます。
例えば、 counsel-find-filecounsel-recentf を実行後に上記のショートカットを打つと、選択している候補のファイルパスの内容を透視できます。

その他の活用方法

この package は second-sight-file という関数を提供しています。

(defun second-sight-file (full-path)
    "Basic function to show second-sight's posframe with the file-content at FULL-PATH."
    (second-sight--show-file-safely full-path))

もしお気に入りのコマンド(find-filehelm 等)を対応させたい場合は、候補選択中にコレを呼び出せば posframe を表示できます。
例えば、dired はこんな感じで対応できます。

(defun second-sight-dired ()
    "Second-sight for `dired'."
    (interactive)
    (let* ((full-path (dired-get-filename)))
        (second-sight-file full-path)))
9
7
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
9
7