vim ide_将Vim变成R IDE

董凡
2023-12-01

vim ide

by Kade Killary

由Kade Killary

如何将Vim转换为R的IDE (How to Turn Vim Into an IDE for R)

Warning: No, this is not the R setup to use if you are a beginner. The RStudio IDE is amazing and should probably always be your default tool. However, if you happen to belong to the outcast realms of Vim / Emacs land, then this post might be for you. Also, I’m going to mention Vim and Neovim throughout the post, at this point they are largely one in the same. So, if you are tied to one or the other it shouldn’t matter.

警告 ,如果您是初学者,则这不是要使用的R设置。 RStudio IDE非常棒,应该始终是您的默认工具。 但是,如果您碰巧属于Vim / Emacs领域的被淘汰者,那么此文章可能适合您。 另外,我将在整个帖子中提及Vim和Neovim ,在这一点上,它们在很大程度上是相同的。 因此,如果您被一个或另一个所束缚,那没关系。

为什么不仅仅使用RStudio? (Why Not Just Use RStudio?)

A great question indeed. For me, the main reasons are speed and familiarity. Yes, I know RStudio has Vim keybindings, but it isn’t the real thing. At this point, I’m ruined by Vim. A lone madman vigorously hitting <esc>; and <C-f> in Microsoft Word only to be disappointed.

确实是个好问题。 对我来说,主要原因是速度和熟悉度。 是的,我知道RStudio具有Vim键绑定,但这不是真实的东西。 在这一点上,我被Vim毁了。 一个孤独的狂人大力袭击<e sc> ; and ; and Microsoft Word中的<Cf>只会令人失望。

However, all is not lost.
但是,一切并没有丢失。

病毒 (R In Vim)

At first, the pursuit of R in Vim seems like an exercise in brutalism. You’re options are few and support seems bleak.

起初,在Vim中追求R似乎是野蛮主义的一种练习。 您的选择很少,而且支持似乎很暗淡。

Your best option will be to utilize a separate :terminal buffer. The basic workflow goes as follows:

最好的选择是利用一个单独的:terminal缓冲区。 基本工作流程如下:

  • Write code in myFile.R

    myFile.R编写代码

  • Visually select code

    直观地选择代码
  • Paste code in :terminal buffer

    将代码粘贴到:terminal缓冲区中

  • Execute code

    执行代码
  • Rinse and repeat

    冲洗并重复

This may not seem too bad, however it gets tedious fairly quickly. Plus, this approach leaves a lot to be desired. Mainly, viewing what’s defined, perusing the data, and some basic completion + linting.

这看起来似乎还不错,但是很快就会变得乏味。 另外,这种方法还有很多不足之处。 主要是,查看定义的内容,细读数据以及一些基本的完成和添加。

Nvim-R进行救援 (Nvim-R To The Rescue)

Nvim-R is easily one of my favorite plugins for Vim. It takes an old water pistol and transforms it into a fully functioning machine gun. It comes stocked with many gems that will make you regret you haven’t been using it all along. So, enough with the poetic waxing. Let’s jump into making Vim our new R home.

Nvim-R很容易成为我最喜欢的Vim插件之一。 它需要一支旧的水枪,并将其转变为功能齐全的机关枪。 它配备了许多宝石,会让您后悔自己一直没有使用它。 因此,用诗般的打蜡就足够了。 让我们进入将Vim打造为我们的新R家吧。

The first step to R enlightenment is…you guessed it, installing Nvim-R. I use Vim-Plug, so that’s what I show below. However, you can just as easily install it using whatever plugin manager you choose.

R启发的第一步是…您猜对了,安装了Nvim-R 。 我使用Vim-Plug ,所以这是我在下面显示的内容。 但是,您可以使用选择的任何插件管理器轻松安装它。

Plug 'jalvesaq/Nvim-R'

Now if you open an R file and hit \rf you’ll see a terminal buffer appear with an R console tied to your current session. To end it, hit \rq.

现在,如果您打开一个R文件并点击\rf您将看到一个终端缓冲区出现,其中R控制台绑定到当前会话。 要结束它,请点击\rq

One important thing to note, the console is not tied to just the current buffer. This means that you can have multiple buffers all feeding into the same console. This can be good / bad depending on your personal preferences. I enjoy it, but it can definitely throw you for a loop if you’re careless. For a deeper dive on how R and Vim communicate in Nvim-R you can head here.

需要注意的一件事是,控制台不仅仅局限于当前缓冲区。 这意味着您可以将多个缓冲区全部馈入同一控制台。 这可能是好事,也可能是坏事,具体取决于您的个人喜好。 我很喜欢它,但是如果您粗心的话,它肯定会让您陷入困境。 要深入了解R和Vim在Nvim-R中的通信方式,可以到这里

秘制酱 (Secret Sauce)

Now that you’ve got the basics up and running we can dive into all that Nvim-R has to offer. There are a vast amount of built-in shortcuts, for the full list you can read the documentation here. I will briefly cover a handful of useful commands that will serve you well on a daily basis.

现在您已经掌握了基础知识并可以运行,我们可以深入研究Nvim-R提供的所有内容。 有大量的内置快捷方式,有关完整列表,您可以在此处阅读文档。 我将简要介绍一些有用的命令,这些命令将每天为您提供服务。

传送线 (Sending Lines)

The most immediate need is to be able to send lines of code. There are a variety of ways to do so in Nvim-R:

最直接的需求是能够发送代码行。 Nvim-R有多种方法可以执行此操作:

  • Send :: Entire File \aa

    发送::整个文件\aa

  • Send :: Entire Block \bb

    发送::整个块\bb

  • Send :: Entire Function \ff

    发送::整个函数\ff

  • Send :: Entire Selection \ss

    发送::整个选择\ss

  • Send :: Entire Line \l

    发送::整行\l

As you can begin to see the forward slash \ is the leader for many operations. However, most of these, and the minor distinctions between them, are superfluous. You will likely be better served by remapping a few of them.

如您所见,正斜杠\是许多操作的领导者。 但是,其中大多数以及它们之间的微小区别是多余的。 重新映射其中一些可能会更好地为您服务。

" in your .vimrc /init.vim
“ remapping the basic :: send linenmap , <Plug>RDSendLine
“ remapping selection :: send multiple linesvmap , <Plug>RDSendSelection
“ remapping selection :: send multiple lines + echo linesvmap ,e <Plug>RESendSelection

I chose to remap the basic send line + multiple lines to my comma key. This significantly cuts down on the number of keys I have to utilize. Furthermore, the ,e mapping allows me to check that the lines I sent were computed correctly. For the most part, these three mappings will allow you to do everything you need to. There are a few more worth mentioning though, and may add something to your workflow if remapped.

我选择将基本发送行+多行重新映射到我的逗号键。 这大大减少了我必须使用的键的数量。 此外, ,e映射使我可以检查发送的行是否正确计算。 在大多数情况下,这三个映射将使您能够执行所需的所有操作。 不过,还有更多值得一提的内容,如果重新映射,可能会在您的工作流程中添加一些内容。

对象浏览器 (Object Browser)

First up, the object browser. This feature, solicited by typing \ro will allow you to see what variables and libraries are active in your current environment.

首先,是对象浏览器。 通过键入\ro寻求此功能,将使您能够查看当前环境中哪些变量和库处于活动状态。

Objects can also be viewed by typing \rl, which will run the ls() function in your current console.

也可以通过键入\rl来查看对象,这将在当前控制台中运行ls()函数。

文献资料 (Documentation)

In order to get a better understanding of your code you have a couple options. From within Nvim-R there are two of particular note \rh — help and \re — example. Each of these will open in a split buffer with the relevant information.

为了更好地理解您的代码,您有两种选择。 在Nvim-R内部,有两个特别的注释\rh帮助和\re re-示例。 这些中的每一个都将在包含相关信息的拆分缓冲区中打开。

Another worthwhile option is the Dash plugin. The easiest path to usage is as follows:

另一个值得选择的选项是Dash插件 。 最简单的用法如下:

“ install plugin :: using vim-plugPlug ‘rizzatti/dash.vim’
“ remap search keynmap <silent> <leader>d <Plug>DashSearch<CR>

Now, when you are seeking more information on a piece of R code, or any other language, all you have to do is place your key over the word and hit <leader>d. The Dash app will then pop up with the relevant information. You can also search Google and Stack Overflow from within it. It’s a great tool, especially for Vimmers who utilize Vim for a variety of languages.

现在,当您要寻找有关R代码或任何其他语言的更多信息时,您所要做的就是将键放在单词上,然后按<leade r> d。 然后,Dash应用程序将弹出相关信息。 您还可以在其中搜索Google和Stack Overflow。 这是一个很棒的工具,特别是对于将Vim用于多种语言的Vimmer而言。

查看资料 (Viewing Data)

Next up is getting a quick peak at your data. RStudio comes with a beautiful built in data viewer, which can be handy for getting a sense of the data. In Vim, it’s a little harder, but not impossible.

接下来,您的数据将Swift达到峰值。 RStudio带有漂亮的内置数据查看器,可以方便地获取数据。 在Vim中,这有点困难,但并非不可能。

Nvim-R allows you to view a data frame by using the \rv command. This will either show the data frame using X Quartz, on Mac, or the CSV plugin for Vim, if you have it installed.

Nvim-R允许您使用\rv命令查看数据帧。 如果已安装,它将在Mac上使用X Quartz或Vim的CSV插件显示数据框。

The CSV plugin comes with a whole host of additional features for manipulating data, but that is outside of the scope of this article. By and large, my suggestion would be to use Excel. While overly beloved by many, it does serve as a good data viewer.

CSV插件附带了用于处理数据的全部其他功能,但这超出了本文的范围。 总的来说,我的建议是使用Excel。 尽管它被许多人过度喜爱,但它确实是一个很好的数据查看器。

其他技巧 (Other Tips & Tricks)

Despite what you may think, there is still more, and even more that I won’t cover. But, these next few tricks are definitely worth keeping in mind.

尽管您可能会想,但还有更多,我将不介绍。 但是,以下几个技巧绝对值得牢记。

内联代码输出 (Inline Code Output)

If you have a line of code and hit \o you’ll see the output rendered as comments in your current file.

如果您有一行代码并按\o您将在当前文件中看到以注释形式呈现的输出。

功能 (Functions)

Instead of doing the basic flow of str() + plot(), etc…Nvim-R allows for a simplified flow.

代替了str() + plot()等的基本流程,Nvim-R简化了流程。

  • summary() :: \rs

    summary():: \rs

  • plot() :: \rg

    plot():: \rg

  • args() :: \ra

    args():: \ra

  • setwd() :: \rd

    setwd():: \rd

  • print() :: \rp

    print():: \rp

  • names() :: \rn

    names():: \rn

(Arrows)

Arrows can be a pain to type. Luckily, Nvim-R makes it easier by mapping _ to &lt;-. This may, or may not, completely bewilder you. If you’re someone who prefers snake case, then you’ll have to hit the underscore twice in order to get an actual underscore, and not an arrow. However, you can override this setting. I find it useful, and have adjusted, but can certainly understand others being irked.

箭头可能很难打。 幸运的是,Nvim-R通过将_映射到&l t;-使其变得更容易。 这可能会使您完全困惑,也可能不会。 如果您是喜欢蛇形的人,那么您必须打两次下划线才能获得实际的下划线,而不是箭头。 但是,您可以覆盖此设置。 我认为它很有用,并且已经进行了调整,但是可以肯定地理解其他人的不满。

完成时间 (Completion)

Often I see lack of code completion as a major reason people skip Vim. This notion is incorrect though. Code completion is very much apart of Vim. For our specific purpose, namely completion for R, there are, yet again, a few options.

通常,我认为缺少代码补全是人们跳过Vim的主要原因。 但是,这个概念是不正确的。 代码完成与Vim的区别很大。 对于我们的特定目的,即R的完成,还有一些选择。

Nvim-R完成 (Nvim-R Completion)

Nvim-R supports code completion out of the box. You will have to manually engage it by using <C-X>;<C-O> for an object name, or <C-X><C-A> for a function argument. For some people this workflow is ideal, but in the current age of VS Code’s Intellisense, and other such options, this feels clunky.

Nvim-R开箱即用地支持代码完成。 您将必须使用<CX> ; <CO>作为对象ct name, o <CX> <CA>作为函数参数来手动使用它。 对于某些人来说,此工作流程是理想的,但是在VS Code的Intellisense以及其他此类选项的当前时代,这感觉很笨拙。

牛顿米 (Ncm-R)

Ncm-R is your best bet for as-you-type sort of completions. It’s a fairly new package, but a much welcomed addition. It integrates with Nvim-R to provide asynchronous completions for R via nvim-completion-manager.

Ncm-R是您按需完成类型的最佳选择。 这是一个相当新的软件包,但是非常受欢迎。 它与Nvim-R集成以通过nvim-completion-manager为R提供异步完成。

Ncm-R provides rich completions for all of the following:

Ncm-R为以下所有内容提供丰富的完成内容:

  • Objects from the global R environment

    全局R环境中的对象
  • Functions from all loaded packages

    所有已加载包中的功能
  • Packages inside library() and require()

    library()require()内部的包

  • Datasets inside data()

    data()内部的data()

  • Arguments inside functions

    函数内部的参数
  • Variables inside pipes %&gt;% and ggplots +

    管道内的变量%&g t;%和ggplo t s +

For a basic set up add the code below to your Vim configuration file.

要进行基本设置,请将以下代码添加到Vim配置文件中。

Plug ‘roxma/nvim-completion-manager’Plug ‘gaalcaras/ncm-R’
“ Optional: for snippet supportPlug ‘sirver/UltiSnips’

R语言服务器 (R Language Server)

If you are unfamiliar with what a language server is head here. If you are familiar, then this project may be of interest. It is still in the early days, and largely experimental, but does work and currently supports both code completion and linting.

如果您不熟悉语言服务器,请前往这里 。 如果您很熟悉,那么可能会对这个项目感兴趣。 它仍处于早期阶段,并且主要是实验性的,但是确实有效,并且目前支持代码完成和插入。

林亭 (Linting)

Last but not least, linting. The set up for quality linting is fairly straightforward. You’ll want to use ALE, Asynchronous Lint Engine, as your driver. You can set this up in your .vimrc as follows:

最后但并非最不重要的是,棉绒。 质量棉绒的设置非常简单。 您将要使用ALE (异步Lint Engine)作为驱动程序。 您可以在.vimrc中进行如下设置:

Plug ‘w0rp/ale’

Now all you need to do is install lintr. This can be done by using install.packages(‘lintr’).

现在,您需要做的就是安装lintr 。 这可以通过使用install.packages('lintr')

Now, the next time you open up an .R file you should be good to go.

现在,下次打开.R文件时,应该会做的很好。

结论 (Conclusion)

At this point, you have a killer set up for R in Vim. There is certainly more to do though if you’re curious. Areas for further exploration include looking through all of the documentation in the plugins I have linked throughout this article. You’ll find many helpful tips and tricks in there, as well as useful settings. I hope this helped to get you up and running with R in Vim!

至此,您已经为Vim中的R设置了一个杀手er。 如果您好奇的话,当然还有很多事情要做。 需要进一步探索的领域包括浏览我在本文中链接的插件中的所有文档。 您会在其中找到许多有用的提示和技巧,以及有用的设置。 我希望这可以帮助您在Vim中使用R启动并运行!

For more about Vim head to my blog!

有关Vim的更多信息,请访问我的博客!

奖金部分 (Bonus Section)

While I know I said you should explore on your own. It doesn't’ hurt to provide some additional settings that can make your life easier.

我知道我说过,您应该自己探索。 提供一些其他设置可以使您的生活更轻松,这也没有什么坏处。

“ settings :: Nvim-R plugin
“ R output is highlighted with current colorschemelet g:rout_follow_colorscheme = 1
“ R commands in R output are highlightedlet g:Rout_more_colors = 1

翻译自: https://www.freecodecamp.org/news/turning-vim-into-an-r-ide-cd9602e8c217/

vim ide

 类似资料: