vim是强大的文本编辑软件。其相比notepad的轻量级文本编辑软件的优势就不用细说了。vim有针对R语言的扩展插件,可以直接向R
console发送代码。但是我用这个插件编辑总是有问题,而且感觉不太灵敏。还好有Autohotkey,我们可以用其打造自己的vim-r扩展。
1. 可以在vim的多种模式下发送代码。不用再由插入状态切换为命令状态,然后再发送代码。
2.
可能你需要在不同环境下向R发送代码,比如你在网页上看到了示例程序,想顺手跑一下。这个可以参考以前的从Notepad++向R语言发送代码的程序。为了方便,我将这两个快捷键写到了一个Autohotkey文件下。
(上面的C-c需要用“<”和“>”括起来。第三个命令是vim下使用windows的全选操作。对下面的快捷键是不需要的,只是一起和复制、粘贴命令列出,供大家选择)
1. 在vim编辑器,用alt+d 向R
console发送代码。在可视模式下,发送的是选择的部分代码。在命令和插入模式下,发送的是该行代码。
2. 在Notepad++和其他文本编辑器,及其他地方,用Ctrl+d向R
console发送代码。同样,如果有选择,发送的是所选部分的代码。如果没有选择,则发送的是光标所在行的代码。
3. 如果在使用过程中发现bug请留言。
;
; AutoHotkey Version: 2.x
; Language:
English
; Platform:
Winxp
; Author:
banmudi
;
; Script Function:
; Template AutoHotkey script.
;
; ;;;;;;;;;;;;;;;;;;;;;;;
; ; codetoR
; ;;;;;;;;;;;;;;;;;;;;;;;
;
#WinActivateForce ;To
prevent the task bar flashes
!d::
WinGet,notepadWinID, ID, A
; save current window ID to return here
later
;WinGet, notepadWinID, ID, ahk_pid %ThisPID%
;IfWinNotExist ahk_class R console
IfWinNotExist,R Console
{
;Run, Rgui, , , ThisPID
;if R hasn't been started, start
RGui
Run, D:Program FilesRR-3.0.0ini386Rgui.exe, , , ThisPID
;if R hasn't been started,
start RGui
;MsgBox yy
WinWait,ahk_pid %ThisPID%
;MsgBox xx
}
;MsgBox here1
;IfWinExist ahk_class R Console
IfWinExist,R Console
{
;MsgBox here2
clipboard = ;Empty the clipboard
WinActivate ahk_id %notepadWinID%
;Send {"}{+}{y}
; copy
selection to clipboard
Send ^c
;
if (StrLen(clipboard)<1)
Send {ESC}{"}{+}{y}{y} ;
copy the current line to clipboard
WinActivate ; ahk_class RGui
;WinGet WinRID, ID, A
;MsgBox here2
Send ^V {enter}
;Send,{enter}
;MsgBox here1
}
;Sleep 30
; wait until the data is pasted (normally not
needed)
;WinActivate ahk_id %notepadWinID%
; go back to the original window
;WinActivate ahk_id %WinRID%
;WinWaitActive ; wait until original is
active (normally not needed)
Return
;Notepad
^D::
WinGet,notepadWinID, ID, A
; save current window ID to return here
later
;WinGet, notepadWinID, ID, ahk_pid %ThisPID%
;IfWinNotExist ahk_class R console
IfWinNotExist,R Console
{
;Run, Rgui, , , ThisPID
;if R hasn't been started, start
RGui
Run, D:Program FilesRR-3.0.0ini386Rgui.exe, , , ThisPID
;if R hasn't been started,
start RGui
;MsgBox yy
WinWait,ahk_pid %ThisPID%
;MsgBox xx
}
;MsgBox here1
;IfWinExist ahk_class R Console
IfWinExist,R Console
{
;MsgBox here2
clipboard = ;Empty the clipboard
WinActivate ahk_id %notepadWinID%
Send ^c
; copy selection to
clipboard
if (StrLen(clipboard)<1)
Send, {home}+{end}^c ; copy the current
line to clipboard
WinActivate ; ahk_class RGui
;WinGet WinRID, ID, A
;MsgBox here2
Send ^V {enter}
;Send,{enter}
;MsgBox here1
}
;Sleep 30
; wait until the data is pasted (normally not
needed)
;WinActivate ahk_id %notepadWinID%
; go back to the original window
;WinActivate ahk_id %WinRID%
;WinWaitActive ; wait until original is
active (normally not needed)
Return