简体   繁体   English

同时编辑多行代码的最快方法

[英]Fastest way to edit multiple lines of code at the same time

What is the best way to do the same action across multiple lines of code in the RStudio source editor?在 RStudio 源代码编辑器中跨多行代码执行相同操作的最佳方法是什么?

Example 1示例 1

Let's say that I copy a list from a text file and paste it into R (like the list below).假设我从文本文件中复制了一个列表并将其粘贴到 R(如下面的列表)。 Then, I want to add quotation marks around each word and add a comma to each line, so that I can make a vector.然后,我想在每个单词周围添加引号并在每一行添加一个逗号,这样我就可以制作一个向量。

Krista Hicks
Miriam Cummings
Ralph Lamb
Jaylene Gilbert
Jordon Sparks
Kenna Melton

Expected Output预计 Output

"Krista Hicks",
"Miriam Cummings",
"Ralph Lamb",
"Jaylene Gilbert",
"Jordon Sparks",
"Kenna Melton"

Example 2示例 2

How can I add missing parentheses on multiple lines.如何在多行中添加缺少的括号。 For example, if I have an if statement, then how can I add the missing opening parentheses for names on line 1 and line 4.例如,如果我有一个if语句,那么我如何为第 1 行和第 4 行的names添加缺少的左括号。

if (!is.null(names pattern))) {
  vec <- FALSE
  replacement <- unname(pattern)
  pattern[] <- names pattern)
}

Expected Output预计 Output

if (!is.null(names(pattern))) {
  vec <- FALSE
  replacement <- unname(pattern)
  pattern[] <- names(pattern)
}

*Note: These names are just from a random name generator. *注意:这些名称仅来自随机名称生成器。

RStudio has support for multiple cursors, which allows you to write and edit multiple lines at the same time. RStudio 支持多光标,允许您同时编写和编辑多行。

Example 1示例 1

You can simply click Alt on Windows/Linux (or option on Mac) and drag your mouse to make your selection, or you can use Alt + Shift to create a rectangular selection from the current location of the cursor to a clicked position.您只需在 Windows/Linux 上单击Alt (或在 Mac 上单击选项)并拖动鼠标进行选择,或者您可以使用Alt + Shift创建一个从 cursor 的当前位置到单击的 position 的矩形选区。

在此处输入图像描述


Example 2示例 2

Another multiple cursor option is for selecting all matching instances of a term.另一个多重 cursor 选项用于选择术语的所有匹配实例。 So, you can select names and press Ctrl + Alt + Shift + M .因此,您可以输入 select names ,然后按Ctrl + Alt + Shift + M。 Then, you can use the arrow keys to move the cursors to delete the space and add in the parentheses.然后,您可以使用箭头键移动光标以删除空格并添加括号。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM