在 Vim 中编程:*您*使用什么特定机制来“复制粘贴”?变量?

发布于 2024-12-14 16:30:43 字数 1228 浏览 8 评论 0原文

注意

这被标记为潜在的主观问题,但它不是主观的。要求学习人们使用 Vim 的各种特定方式,以便来自“鼠标和键盘”面向文本编辑器的人可以了解更多 Vim 编辑方式。

不是关于个人偏好或哪种编辑器或编辑风格最好的主观问题。

这是一个关于在 Vim 编辑器中获取结果所需的机械步骤的具体问题,使用替代编辑器作为交叉引用的基线。

问题

假设您的 Vim 中有以下代码,并且您想要从 beforeafter,其中 before 看起来像这样:

// Before //

$mynames    = Array();
$mynames['alice'] = 'alpha';

... 和之后看起来像这样...

// After //

$mynames    = Array();
$mynames['alice'] = 'alpha';
$mynames['betty'] = 'bravo';
$mynames['cindy'] = 'charlie';
$mynames['deana'] = 'delta';

非 Vim 编辑器会如何做

使用非 Vim 编辑器,程序员 A 只需复制 alice 的第一行,将其多次粘贴到编辑器中,然后重新- 编辑值,以便alicealpha 被替换为适当的值,一次编辑一行。

使用非 vim 编辑器,程序员 B 将创建一个跨越四行的矩形选区,然后开始输入常用文本 $mynames[''] = '';,然后返回并填写适当的值,一次编辑一行。

VIM 怎么样?

鉴于 Vim 与当今的“鼠标和键盘”风格编辑器有很大不同,因此要求深入了解使用 Vim 进行编辑的具体步骤。显然,可以单独键入每一行,但假设在 Vim 中存在一种节省时间的方法,与上面程序员 A 和程序员 B 的做法相比。

1) vim 程序员如何使用上述节省时间的方法来进行此编辑操作?

2) 如果有人要在互联网上搜索更多关于 Vim 编辑会话与“鼠标和键盘”风格编辑的具体“逐步”比较的示例,人们会搜索什么?

NOTE

This was flagged as a potentially subjective question, but it is not subjective. It is requested to learn the various specific ways that people use Vim so that a person coming from a "mouse-and-keyboard" oriented text editor might learn more of the Vim way of editing.

This is not a subjective question about personal preferences or which editor or editing style is best.

This is a specific question about the mechanical steps one would take to obtain an outcome in the Vim editor, using alternative editors as a baseline for cross-reference.

PROBLEM

Suppose you have the following code in your Vim and you want to get from before to after where before looks like this:

// Before //

$mynames    = Array();
$mynames['alice'] = 'alpha';

... and after looks like this ...

// After //

$mynames    = Array();
$mynames['alice'] = 'alpha';
$mynames['betty'] = 'bravo';
$mynames['cindy'] = 'charlie';
$mynames['deana'] = 'delta';

HOW NON-VIM EDITORS WOULD DO IT

Using a non-vim editor, programmer A would simply copy the first line for alice, paste it multiple times into the editor and then re-edit the values so that alice and alpha are replaced with the appropriate values, editing one line at a time.

Using a non-vim editor, programmer B would create a rectangular selection that spans four lines, and just start typing the common text $mynames[''] = ''; and then go back and fill in the appropriate values, editing one line at a time.

HOW ABOUT VIM?

Given that Vim is a significantly different approach from "mouse-and-keyboard" style editors of the day, this is a request for insight on the specific steps one takes in editing with Vim. Obviously, it is possible to just type each line individually, but it is assumed that there is a time-saving way to do this in Vim in a way that compares to what programmer A and programmer B did above.

1) How would a vim programmer go about doing this edit operation, using a time-saving method like those above?

2) If someone were to search the Internet for more examples of specific 'step-by-step' comparisons of Vim editing sessions vs "mouse-and-keyboard" style editing, what would one search for?

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

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

发布评论

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

评论(3

快乐很简单 2024-12-21 16:30:53

我喜欢 AlexRus 的解决方案(我喜欢 Vim 宏)。

但我认为更现实的情况是粘贴来自其他应用程序/文档的键/值对:

betty bravo
cindy charlie
deana delta

并在每一行上执行一系列转换。

解决方案 1

我们可以使用 jj 或其他方式选择所有三行,并对所选内容应用一系列搜索/替换:

:'<,'>s/^/$mynames['
gv to reselect
:'<,'>s/ /'] = '
gv to reselect
:'<,'>s/$/';

整个编辑序列如下所示这个:

<S-v>jj:s/^/$mynames['<CR>gv:s/ /'] = '<CR>gv:s/$/';<CR>

解决方案 2

我们可以应用单个搜索/替换

:'<,'>s/^\(.*\) \(.*\)$/$myname['\1'] = '\2';

,其中搜索部分隔离行的开头 (^)、单词之间的空格 () 和结尾行 ($) 通过实际匹配它们之间的文本,替换部分将整行替换为 $myname[' + 第一个匹配项 (\1) + '] = ' + 第二个匹配 (\2) + ';

我不擅长正则表达式,所以我必须检查我的笔记才能将它们放在一起,但我毫不怀疑许多 Vim 用户能够一次性输入这种命令。我会的,总有一天。

整个编辑序列如下所示:

<S-v>jj:s/^\(.*\) \(.*\)$/$myname['\1'] = '\2';<CR>

解决方案 3

使用相同的设置,我们可以在第一行的开头使用 进入 VISUAL-BLOCK 模式;,根据需要向下移动并输入 I$myaccess[' 以获得:

$mynames['betty bravo
$mynames['cindy charlie
$mynames['deana delta

使用 f< 将光标移动到单词之间的空格/代码>,再次点击 ,将选择范围扩展到底部并输入 c'] = ' 获取:

$mynames['betty'] = 'bravo
$mynames['cindy'] = 'charlie
$mynames['deana'] = 'delta

然后移动到行尾$,再次点击 ,再次选择所需内容,然后键入 A'; 作为最后一次触摸。

整个编辑顺序如下所示:

<C-v>jjI$myaccess['<Esc>f <C-v>jjc'] = '<Esc>
lt;C-v>jjA';<Esc>

I like AlexRus's solution (I love Vim macros).

But I think that a more realistic situation would be to paste the key/value pairs from some other application/document:

betty bravo
cindy charlie
deana delta

and perform a bunch of transformations on each line.

SOLUTION 1

We could select all three lines with <S-v>jj or some other way and apply a series of search/replace on the selection:

:'<,'>s/^/$mynames['
gv to reselect
:'<,'>s/ /'] = '
gv to reselect
:'<,'>s/$/';

The whole editing sequence looks like this:

<S-v>jj:s/^/$mynames['<CR>gv:s/ /'] = '<CR>gv:s/$/';<CR>

SOLUTION 2

We could apply a single search/replace

:'<,'>s/^\(.*\) \(.*\)$/$myname['\1'] = '\2';

where the search part isolates the beginning of the line (^), the space between words () and the end of the line ($) by actually matching the text between them and the replace part replaces the whole line with $myname[' + the first match (\1) + '] = ' + the second match (\2) + ';.

I'm bad at regex so I had to check my notes to put it together but I have no doubt many Vim users are able to type that kind of command in one go. I will, someday.

The whole editing sequence looks like this:

<S-v>jj:s/^\(.*\) \(.*\)$/$myname['\1'] = '\2';<CR>

SOLUTION 3

With the same setup, we could enter VISUAL-BLOCK mode at the beginning of the first line with <C-v>, go as far down as necessary and type I$myaccess['<Esc> to obtain:

$mynames['betty bravo
$mynames['cindy charlie
$mynames['deana delta

move the cursor to the space between words with f<Space>, hit <C-v> again, expand the selection to the bottom and type c'] = '<Esc> to obtain:

$mynames['betty'] = 'bravo
$mynames['cindy'] = 'charlie
$mynames['deana'] = 'delta

then move to the end of the line with $, hit <C-v> again, select what you want again and type A';<Esc> for the last touch.

The whole editing sequence looks like this:

<C-v>jjI$myaccess['<Esc>f <C-v>jjc'] = '<Esc>
lt;C-v>jjA';<Esc>
筱武穆 2024-12-21 16:30:51

如果我提前知道不同的值是什么,我会采用迂回方法。我将从以下开始:

$mynames    = Array();
alice alpha
betty bravo 
cindy charlie
deana delta

然后我将光标放在 alice 前面,点击 Ctrl+V,向下移动到 deana,然后按 Shift+I 进入插入模式,然后键入 $mynames[',然后键入 Esc。这会将文本插入到所有选定的行中。然后我重复 '] = ',最后是 ';'

这不是最有效的方法,但通常是第一个想到的方法。

If I know ahead of time what the different values are going to be, I'll to the roundabout approach. I'll start with this:

$mynames    = Array();
alice alpha
betty bravo 
cindy charlie
deana delta

Then I will place my cursor in front of alice, hit Ctrl+V, move down to deana, then hit Shift+I to go into insert mode and type $mynames[' followed by Esc. This inserts the text in all selected lines. Then I repeat that for '] = ', followed finally by ';'

Not the most efficient way, but usually the first that comes to mind.

-残月青衣踏尘吟 2024-12-21 16:30:50

我使用相同的方法,首先我复制一行。然后在我需要的时候粘贴它。

然后您可以创建宏来编辑按键。当光标位于我需要工作的第一行时。 (第一行粘贴)

    qq f[ci'<C>-<o>q  "recordes a macro to find a [block] and 
change inner quotes ' and stays in insert mode

然后您可以随时通过 @q 播放宏。 (我有一个地图 Q = @q 用于通过 Shift+q 快速启动宏)
您可以使用与值相同的方式:

 qq f=f'ci'<C>-<o>q

用于查找值块并进入插入模式的宏。

比较的答案是我将节省将手从键盘移动到鼠标的时间=编辑行数。选择要更改的块。毫无疑问,Vim 的生产力更高。

I use the same, first i copy a line. then pasting it any times what i need.

Then you can create a macro to edit a keys. When cursor is on first line where i need to work. (a frist pasted line)

    qq f[ci'<C>-<o>q  "recordes a macro to find a [block] and 
change inner quotes ' and stays in insert mode

Then you can play your macro any time by @q . (I have a map Q = @q for fast macro start by Shift+q)
The same way you can use for values:

 qq f=f'ci'<C>-<o>q

Macro for find a value block and go to insert mode.

And the answer for comparsion i will save time to move my hand from keyboard to mouse times = number of edit lines. Selecting a block for changing. Vim is more productive no doubt.

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