Useful keyboard shortcuts and tips for ESS/R

自古美人都是妖i 提交于 2019-12-31 07:58:27

问题


I would like to ask regular ESS/R users what key bindings do they use frequently and tips on using ESS/R.


回答1:


I have set several shortcuts in my .emacs file. The most useful are:

C-tab to switch between the R command line and the file (similar to josh answer, but much faster):

(global-set-key [C-tab] 'other-window)

Control and up/down arrow keys to search history with matching what you've already typed:

(define-key comint-mode-map [C-up] 'comint-previous-matching-input-from-input)
(define-key comint-mode-map [C-down] 'comint-next-matching-input-from-input)

Comment-uncomment a selected region with C-d or C-maj-d

(defun uncomment-region (beg end)
  "Like `comment-region' invoked with a C-u prefix arg."
  (interactive "r")
  (comment-region beg end -1))

(define-key ess-mode-map (kbd "C-d") 'comment-region)
(define-key ess-mode-map (kbd "C-S-d") 'uncomment-region)

Also I've also enabled CUA mode (from options menu) and reconfigured quite a lot of shortcuts to require only two keystrokes (instead of four in standard mode):

;; Delete selection when pressing [delete] key
  (delete-selection-mode t)

;; ESS Mode (.R file)
  (define-key ess-mode-map "\C-l" 'ess-eval-line-and-step)
  (define-key ess-mode-map "\C-p" 'ess-eval-function-or-paragraph-and-step)
  (define-key ess-mode-map "\C-r" 'ess-eval-region)

;; iESS Mode (R console)
  (define-key inferior-ess-mode-map "\C-u" 'comint-kill-input)
  (define-key inferior-ess-mode-map "\C-w" 'backward-kill-word)
  (define-key inferior-ess-mode-map "\C-a" 'comint-bol)
  (define-key inferior-ess-mode-map [home] 'comint-bol)

;; Comint Mode (R console as well)
  (define-key comint-mode-map "\C-e" 'comint-show-maximum-output)
  (define-key comint-mode-map "\C-r" 'comint-show-output)
  (define-key comint-mode-map "\C-o" 'comint-kill-output)

;; Search with C-f / C-F (control-maj-F for backware search)
   (global-set-key "\C-f" 'isearch-forward)
   (global-set-key (kbd "C-S-f") 'isearch-backward)
   (define-key isearch-mode-map "\C-f" 'isearch-repeat-forward)
   (define-key isearch-mode-map (kbd "C-S-f") 'isearch-repeat-backward)

;; Save with C-s / C-S
  (global-set-key (kbd "C-s") 'save-buffer)
  (global-set-key (kbd "C-S-s") 'write-file)
  ;; need to redefine them for isearch mode (don't know why)
  (define-key isearch-mode-map (kbd "C-s") 'save-buffer)
  (define-key isearch-mode-map (kbd "C-S-s") 'write-file)

;; Pause = dedicate window. 
  (defun toggle-current-window-dedication ()
   (interactive)
   (let* ((window    (selected-window))
          (dedicated (window-dedicated-p window)))
     (set-window-dedicated-p window (not dedicated))
     (message "Window %sdedicated to %s"
              (if dedicated "no longer " "")
              (buffer-name))))
  (global-set-key [pause] 'toggle-current-window-dedication)

;; delete = delete
  (global-set-key [delete] 'delete-char)

;; C-b = list buffers
  (global-set-key (kbd "C-b") 'bs-show)

You will find many more useful shortcuts in ESS documentation.




回答2:


C-c C-z ess-switch-to-end-of-ESS

is nice to jump from your source file that you are editing foo.R to the R console




回答3:


I found this link to be extremely helpful. It provides elisp code to make Shift+Enter do many common tasks in a context dependent fashion.

http://kieranhealy.org/blog/archives/2009/10/12/make-shift-enter-do-a-lot-in-ess/




回答4:


Great stuff, have been using it for ages. Unfortunately as of 15-11-2013 the uncomment key binding may not work due to EMACS changes (I think, at least it was working before I loaded the latest version). This is because the default uncomment function has 3 arguments but the one defined above has 2. The best way to fix this is to simply delete the uncomment function from the code and retain the keybinding, so it uses the default uncomment function. Or in other words just use this:

  • (define-key ess-mode-map (kbd "C-d") 'comment-region)
  • (define-key ess-mode-map (kbd "C-S-d") 'uncomment-region)



回答5:


M-n and M-p in the ESS R console for next/previous command.



来源:https://stackoverflow.com/questions/2901198/useful-keyboard-shortcuts-and-tips-for-ess-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!