Reputation: 591
I am trying to migrate from RStudio to emacs ess and i am trying now to find auto-completion functionality in emacs ess similar to Rstudio
For my question i'll use a simple RStudio example and i'll appreciate if you could tell me how to do the same in emacs ess (The purpose is of course to generalize):
in RStudio
when i type the text
libr
and then press TAB I getlibrary()
inside the parentesis
when i type "tidyv" like that:
library(tidyv)
and then TAB i getlibrary(tidyverse)
Could you please describe in a simple step by step manner how i can do the same in emacs ess?
Thanks
Rafael
Upvotes: 3
Views: 2149
Reputation: 32446
This should work for you with the default company-backend after a recent backend addition. You will just need to update your ESS from melpa. If company-R-library
is not part of your company-backends
in an ESS buffer, just add it in your ESS mode hook. Note that you should have set ess-use-company
.
In response to comment, ess-use-company
is a variable you should set to t
in your config, not a function to call with M-x. I'll assume you have an init file, if not you can easily find information about that. Wherever you keep your config for ess in your init file, you could add (although this is the default, so unless you set ess-use-auto-complete
, this is probably already set)
(setq ess-use-company t)
Every mode has a hook, which is a function it runs after it is setup in a buffer, allowing users to customize the mode. You can add such a hook using (add-hook 'ess-mode-hook 'my-ess-mode-hook)
, where my-ess-mode-hook
is a function you write with your customizations, eg. (company-mode)
unless you have that turned on globally (most likely).
company-backends
is also a variable, from an ESS buffer you can type M-:company-backends
to evaluate lisp code to see the value of that variable. Make sure you reinstall the latest version of ESS from melpa.
Edit: full init to test -- company-R-library
may not have been added to default backends
(setq-default package-archives
'(("melpa" . "http://melpa.milkbox.net/packages/")
("gnu" . "http://elpa.gnu.org/packages/")))
(setq package-enable-at-startup nil)
(package-initialize)
;;; company
(require 'company)
(setq tab-always-indent 'complete)
(setq company-idle-delay 0.5
company-show-numbers t
company-minimum-prefix-length 2
company-tooltip-flip-when-above t)
(global-set-key (kbd "C-M-/") #'company-complete)
(global-company-mode)
;;; ESS
(defun my-ess-hook ()
;; ensure company-R-library is in ESS backends
(make-local-variable 'company-backends)
(cl-delete-if (lambda (x) (and (eq (car-safe x) 'company-R-args))) company-backends)
(push (list 'company-R-args 'company-R-objects 'company-R-library :separate)
company-backends))
(add-hook 'ess-mode-hook 'my-ess-hook)
(with-eval-after-load 'ess
(setq ess-use-company t))
Start emacs with
emacs -Q -l /path/to/this/init.el
Open an R file, start the inferior R process, then type library(ti
M-C-/ and you should get package completions.
Upvotes: 5
Reputation: 3785
Okay, so I have it working for me, quite well with the following setup and steps:
init.el
package-install [RET] company
package-install [RET] ess
M-x company-mode
to enable company-mode
in the current bufferAt this point, autocomplete automatically pops up completion suggestions on R objects. Autocomplete has to be triggered for inside parentheses. Say I have my cursor inside the function parentheses for shiny:runApp()
. To get completion suggestions for the function arguments, I need to run the command company-complete
.
You an do this with M-x company-complete
or M-x company-complete-common
and you should see the following:
I decided to bind this to Shift-Tab with the following line in my init.el:
(global-set-key (kbd "<backtab>") 'company-complete-common)
And to have company mode enabled globally with:
(add-hook 'after-init-hook 'global-company-mode)
Upvotes: 1