1

In my init.el file, I have a hook to setup my workspace when I open a particular type file. Specifically, when I open an R file (i.e., *.R) and ESS-mode starts, a couple of other windows open with vterm and R in them. My Elisp code looks like this:

;; R setup                                                                                                                                                                                                                                                                    
(require 'ess-site)
(add-hook 'ess-mode-hook
          (lambda()
            (set-window-buffer (selected-window)
                               (file-name-nondirectory (car (last command-line-args))))
            (split-window-horizontally)
            (other-window 1)
            (vterm)
            (split-window-vertically)
            (R)
            (other-window 1)
            ))

When I open a file, say emacs myfile.R, this works as expected. I run into a problem, however, if I open a file once Emacs is already up and running. For example, I run emacs myfile.R and then do C-x C-f myfile2.R then this causes more windows to open.

Q: Is there a way to only run this startup function when there isn't an existing emacs session?

  • Curious: are you running R in the vterm buffer, or are you using vterm for other stuff alongside R? – Tyler Oct 26 at 15:15
  • 1
    The idea was to have a vterm open for other stuff, like git. But now you mention it, I wonder if my R instance is running from vterm... Regardless, on my system, this function creates three windows: editor with the file I opened, vterm, and R. – Lyngbakr Oct 26 at 15:24
  • 1
    Ok. Without some (maybe a lot?) tweaking, R won't be running in vterm. I haven't tried vterm yet, but it sounds quite interesting. For git specifically, magit is amazing! – Tyler Oct 26 at 15:28
  • Ah, I see. (I'm a newb so I'm not really sure what I'm doing). Another person here recommended vterm and so far it's great. Thanks for the tip about magit! – Lyngbakr Oct 26 at 15:32
0

I think I have a solution. I define my setup in a function that is only called at startup, like so:

;; Define R setup function                                                                                                                                                                                                                                                    
(defun R_setup()
  "Setup R"
  (set-window-buffer (selected-window)
                     (file-name-nondirectory (car (last command-line-args))))
  (split-window-horizontally)
  (other-window 1)
  (vterm)
  (split-window-vertically)
  (R)
  (other-window 1)
  )

;; Startup hook for R                                                                                                                                                                                                                                              
(add-hook 'emacs-startup-hook
          (lambda()
            (if (equal (file-name-extension (car (last command-line-args))) "R")
                (R_setup))
          ))

This way I only check that it is an R file at startup and if it is then perform my setup. When Emacs is already up and running, no such check is performed, so no setup is performed.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.