ITPub博客

首页 > 数据库 > NoSQL > windows下构建R语言vim编辑平台

windows下构建R语言vim编辑平台

NoSQL 作者:yaosj36 时间:2014-03-15 03:33:33 0 删除 编辑
vim是强大的文本编辑软件。其相比notepad的轻量级文本编辑软件的优势就不用细说了。vim有针对R语言的扩展插件,可以直接向R console发送代码。但是我用这个插件编辑总是有问题,而且感觉不太灵敏。还好有Autohotkey,我们可以用其打造自己的vim-r扩展。

## 优点:
1. 可以在vim的多种模式下发送代码。不用再由插入状态切换为命令状态,然后再发送代码。
2. 可能你需要在不同环境下向R发送代码,比如你在网页上看到了示例程序,想顺手跑一下。这个可以参考以前的从Notepad++向R语言发送代码的程序。为了方便,我将这两个快捷键写到了一个Autohotkey文件下。
map C-c "+y
map C-v  "+p
map C-a  "+ggVG
 
(上面的C-c需要用“<”和“>”括起来。第三个命令是vim下使用windows的全选操作。对下面的快捷键是不需要的,只是一起和复制、粘贴命令列出,供大家选择)

 
## 用法
1. 在vim编辑器,用alt+d 向R console发送代码。在可视模式下,发送的是选择的部分代码。在命令和插入模式下,发送的是该行代码。
2. 在Notepad++和其他文本编辑器,及其他地方,用Ctrl+d向R console发送代码。同样,如果有选择,发送的是所选部分的代码。如果没有选择,则发送的是光标所在行的代码。

## 目前发现的bug
1. 刚用vim打开文件时,第一次在可视模式下发送代码,第一段的代码可能部分未发送。不过随后再在可视模式下,则没有问题。
2. 没有R插件中的直接source文件功能。这个需要autohotkey读取当前文件的路径,然后还要同时向剪贴板发送source命令。有时间研究一下。
----已解决,可参考最新的博文。
3. 如果在使用过程中发现bug请留言。

## Autohotkey源码如下。注意需要改自己的R安装路径。

;
; 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



<!-- 正文结束 -->

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22178037/viewspace-1120585/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2009-07-23