You can use the variable command-line-args
to get a list of elements making up the command line used to invoke emacs. If your command line consists of just two elements — "emacs"
and the filename to visit — you can get the buffer associated with the file visited like this:
(get-buffer (file-name-nondirectory (cadr command-line-args)))
ただし、訪問しているファイルのバッファーがまだ使用できないため、これは .emacs
の起動ファイルでは機能しません。代わりに、 .emacs
ファイルにコードを追加して、コマンドライン引数の処理後に実行される emacs-startup-hook
に上記のようなものを追加することができます。たとえば、このコードは、訪問先ファイルのバッファに cmd-line-file-buffer
という名前の変数を設定するようにフックを強化します。
(add-hook 'emacs-startup-hook
(lambda ()
(if (= 2 (length command-line-args))
(setq cmd-line-file-buffer
(get-buffer (file-name-nondirectory (cadr command-line-args)))))))