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

发布于 2025-01-14 01:19:23 字数 877 浏览 3 评论 0原文

在 RStudio 源编辑器中跨多行代码执行相同操作的最佳方法是什么?

示例 1

假设我从文本文件复制一个列表并将其粘贴到 R 中(如下列表所示)。然后,我想在每个单词周围添加引号,并在每行添加逗号,这样我就可以创建一个向量。

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

预期输出

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

示例 2

如何在多行上添加缺少的括号。例如,如果我有一个 if 语句,那么如何在第 1 行和第 4 行上为 names 添加缺少的左括号。

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

预期输出

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

*注意:这些名称仅来自随机名称生成器。

What is the best way to do the same action across multiple lines of code in the RStudio source editor?

Example 1

Let's say that I copy a list from a text file and paste it into R (like the list below). 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

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

Example 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 (!is.null(names pattern))) {
  vec <- FALSE
  replacement <- unname(pattern)
  pattern[] <- names pattern)
}

Expected Output

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

*Note: These names are just from a random name generator.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一腔孤↑勇 2025-01-21 01:19:23

RStudio 支持多个光标,允许您同时写入和编辑多行。

示例 1

您只需在 Windows/Linux 上单击 Alt(或在 Mac 上单击 选项)并拖动鼠标即可进行选择,或者您也可以使用Alt+Shift创建从光标当前位置到单击位置的矩形选区。

输入图像描述这里


示例 2

另一个多光标选项用于选择术语的所有匹配实例。因此,您可以选择 names 并按 Ctrl+Alt+Shift+M 。然后,您可以使用方向键移动光标删除空格并添加括号。

输入图像描述这里

RStudio has support for multiple cursors, which allows you to write and edit multiple lines at the same time.

Example 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.

enter image description here


Example 2

Another multiple cursor option is for selecting all matching instances of a term. So, you can select names and press Ctrl+Alt+Shift+M. Then, you can use the arrow keys to move the cursors to delete the space and add in the parentheses.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文