3

When I open an R file in ESS-mode, I'd like emacs to setup my panes in a certain format.

enter image description here

To this end, I've added the following to my init.el.

(add-hook 'ess-mode-hook
          (lambda()
            (split-window-horizontally)
            (other-window 1)
            (vterm)
            (R)
            ))

When I run emacs myfile.R, emacs opens and prompts me to specify a project directory, which I do. This results in the vertical split on the right, which gives me pretty much what I show above. Awesome! But there is one problem. While myfile.R is opened as a buffer, the left-hand pane actually shows the scratch buffer. Why is that and how do I avoid it?

7
  • 1
    There are too many factors/variables that could affect the layout when Emacs starts, and I couldn't possibly list even half of them in this comment. Rather than trying to deal with all of that, how about beginning your lambda with something like (delete-other-windows) [to return the layout to just one visible window] and then go from there? Eventually, there are more precise functions you may wish to use for specifying a window-layout, but the ones you've chose are what most beginners use and I've even seen some users with more experience still post answers using those same functions.
    – lawlist
    Oct 23, 2020 at 0:00
  • ... After (delete-other-windows), you may wish to consider following up that command with something like (set-window-buffer (selected-window) (find-file-noselect FILE 'nowarn)) or replace (find-file-noselect FILE 'nowarn) with BUFFER-OR-NAME, which may be known or can be obtained if need be.
    – lawlist
    Oct 23, 2020 at 0:15
  • Thanks for the help, @lawlist – it's much appreciated. Following your advice, it works if I hard code BUFFER-OR-NAME as myfile.R (as per my example). Is there a way to figure out what the passed filename is programmatically? Perhaps pulling it from buffer-list or command-line-args somehow... (First day using Elisp, so I'm still finding my feet.)
    – Dan
    Oct 23, 2020 at 0:27
  • 1
    If I launch Emacs 27 from the terminal with /absolute/path/to/preferred/version/of/emacs -nw foo.txt, then (car (last command-line-args)) returns "foo.txt".
    – lawlist
    Oct 23, 2020 at 1:03
  • @lawlist Perfect! Do you want to post that as an answer so that I can accept it?
    – Dan
    Oct 23, 2020 at 1:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.