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)))
Dit zal echter niet werken in uw .emacs
opstartbestand omdat de buffer voor het te bezoeken bestand nog niet beschikbaar is. In plaats daarvan kunt u code toevoegen aan uw .emacs
-bestand om zoiets als het bovenstaande toe te voegen aan de emacs-startup-hook
, die wordt uitgevoerd nadat de argumenten van de commandoregel zijn afgehandeld. Deze code vergroot bijvoorbeeld de haak om een variabele met de naam cmd-line-file-buffer
in te stellen in de buffer van het bestand dat wordt bezocht:
(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)))))))