LoginSignup
8
8

More than 5 years have passed since last update.

RStudioのショートカットキー変更

Last updated at Posted at 2015-08-12

2015/12/25 追記
我輩の希望的観測は見事にはずれた。今日新たにリリースされたRstudioのPreviewRelease(v0.99.836)では、ショーカットキーの作成ができなくなった。なおショートカットキーのキーバインド変更は引き続きできるようなので安心した。

Rstudioの今後としては新たにAddinの形で機能を追加していく方針になりつつあるようだ。

Rstudioのショートカットの変更

RStudioでショートカットキーの変更ができるようになったのでメモる。
なおv0.99.644からなので投稿時現在の環境ではdevelop版を使うことになる。
参考というかほぼそのまま公式サイトのパクリ。

既存のショートカットの変更

現在行までのコードを実行してくれる便利なショートカット"Ctrl + Alt + T"はTerminalを開くショートカットと
もろかぶりなので変更する。

コンフリクトもわかりやすく表示される。

* なお、Altキーは特殊なようで、Altキーを用いたショートカットは登録できなかったが、
  正式リリースまでに修正されるかも(希望的観測)

新しいショートカットの作成

まずはショートカットキーを保存するディレクトリを作成する。

mk_sht.sh
mkdir -p ~/.R/keybindings/R/

このディレクトリの下にショートカットの設定を書いたRscriptを保存する。

Hello.R
insert_hello <- function(content, range) {
    action <- replaceTextAction(c(0, 0, 0, 0), 
                                "# Hello, world!\n")
    userCommandResult(action)
}

registerUserCommand("hello-world-insert",
                    "Ctrl+X Ctrl+G",
                    insert_hello)

使用する関数は
registerUserCommand、userCommandResult、replaceTextActionで、組み合わせて作る。

ここからよくわからなかった。作ろうとしたのはpipeRのOperatorを挿入するショートカット。

insert_pipe.R
insert_pipeR <- function(content, range){
  action <- replaceTextAction(range = c(0, 0, 0, 0), 
                              text = " %>>% ")
  userCommandResult(action)
}

registerUserCommand("insert pipeR oprtr",
                    "Ctrl + Alt + M",
                    insert_pipeR, 
                    overwrite = TRUE)

これはうまく動かない。これを実行するとスクリプトの先頭に挿入される。
みそはreplaceTextActionの引数にrangeを入れることでカーソルのある場所に挿入してくれる。

insert_pipe.R
insert_pipeR <- function(content, range){
  action <- replaceTextAction(range = range, 
                              text = " %>>% ")
  userCommandResult(action)
}

registerUserCommand("insert pipeR oprtr",
                    "Ctrl + Alt + M",
                    insert_pipeR, 
                    overwrite = TRUE)

referenceをみてよくわからなかったが、コード本体のコメントとregisterUserCommandのdescriptionで解決。

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8