Vim 拉动行范围

发布于 2024-08-17 00:16:06 字数 368 浏览 14 评论 0原文

我是一名 C# 开发人员,最近决定扩展我对可用工具的了解。我决定学习的第一个工具是 Vi/Vim。到目前为止,一切进展顺利,但有几个问题我似乎找不到答案:

  1. 假设我想删除一系列行。我知道有很多方法可以做到这一点,但我想通过行号来做到这一点。我认为它与替代命令的工作方式类似,例如 81,91y。有办法做到这一点吗?

  2. 我对正常模式下的 g 命令有点困惑。它似乎做了很多事情,但我无法真正确定 g 命令的核心功能。我很困惑它是运动命令还是通过正常模式运行的其他命令的“全部捕获”。有人可以解释一下这一点,或者给我指出一个可以很好地解释 g 命令的参考资料吗?

I'm a C# developer who has just recently decided to expand my knowledge of the tools available to me. The first tool I've decided to learn is Vi/Vim. Everything has been going well so far, but there are a couple of questions I can't seem to find the answer to:

  1. Lets say I wanted to yank a range of lines. I know there are many ways of doing so, but I would like to do it by line number. I figured it would be similar to how the substitute commands work, something like 81,91y. Is there a way to do this?

  2. I'm a little confused about the g command in normal mode. It seems to do a myriad of things and I can't really determine what the g command does at its core. I'm confused on whether or not it's a motion command or a kind of "catch all" for other commands ran through normal mode. Can someone please explain this or point me to a reference that gives a good explanation of the g command?

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

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

发布评论

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

评论(10

放低过去 2024-08-24 00:16:06

猛拉第 81-91 行

:81,91y<enter>

可以(转到第 81 行,猛拉 11 行)

81gg11yy 

如果您的手指不喜欢找到 :, 键,这也 仅使用 g5gg。去5号线。 22gg:第22行。正如 jimbo 所说,它实际上只是其他一些命令的修饰符。

为了完整起见,(http://vim.wikia.com/wiki/Power_of_g) 解释了很多g 在命令模式下如何工作。

Yank lines 81-91

:81,91y<enter>

If your fingers don't like to find the : and , keys, this would work as well (go to line 81, yank 11 lines)

81gg11yy 

My only use of g is 5gg. To go to the 5th line. 22gg: 22nd line. As jimbo said, it's really only a modifier for some other commands.

For completeness, (http://vim.wikia.com/wiki/Power_of_g) explains a lot of how g works in command mode.

魄砕の薆 2024-08-24 00:16:06

您还可以使用“t”将当前行复制到当前光标位置。

:81,91t.<enter>

这会将第 81-91 行粘贴到光标所在行的下方。

我从 http://vimcasts.org 学到了这一点,这是关于 VIM 的优秀资源。

You can also copy the current lines to your present cursor location using 't'.

:81,91t.<enter>

This will paste the lines 81-91 under the line the cursor is on.

I learned this from http://vimcasts.org which is an excellent resource on VIM.

迷你仙 2024-08-24 00:16:06

如果伴有计数值,G 命令将转到某个行号。 81G 会将您置于第 81 行。

y 命令可以与移动结合使用,例如 G。因此,要将所有内容拉到第 91 行,您可以使用 y91G。

一起得到:

81Gy91G

转到第 81 行,然后在转到第 91 行时猛拉。

The G command goes to a certain line number, if it's accompanied by a count value. 81G puts you on line 81.

The y command can be combined with a movement, like G. So to yank everything until line 91 you can use y91G.

Together you get:

81Gy91G

Go to line 81, then yank while going to line 91.

枕花眠 2024-08-24 00:16:06

我还喜欢使用 vim 的相对行号选项(set rnu),这意味着我只需输入:

:-10,-7ya a

将文本拉入命名缓冲区 a 中。

注意指定 A 会将您要拉取的内容附加到缓冲区 a 的当前内容中。

不要忘记,您还可以使用类似的命令复制文本块和移动文本块:

:-10,-7co .

表示将四行文本复制到当前行上方 10 行,

:-10,-7mo .

表示将四行文本移动 10 行从当前行上方到下方。

I also like to use vim's relative line number option (set rnu) which means I can just enter:

:-10,-7ya a

to yank the text into named buffer a.

N.B. Specifying A will append what you're yanking to the current contents of buffer a.

Don't forget you can also copy blocks of text and move blocks of text around as well with the similar commands:

:-10,-7co .

means copy the four lines of text 10 lines above to below the current line, and

:-10,-7mo .

means move the four lines of text 10 lines above to below the current line.

全部不再 2024-08-24 00:16:06

除了 :91,96y a 之外,它将 (y) 第 91 至 96 行拉入寄存器 a 中,(用 "ap 粘贴),被拉出的行可以附加到寄存器:

:91,96y A

A寄存器的大写导致对寄存器a的附加操作code> 而不是覆盖。寄存器的大写总是这样工作,例如 :let @A=';' 附加一个 ; 到寄存器 a

使用加号 (+) 或减号 (-) 引用相对于当前光标位置的行:

:-10,+10y b

即,它将在当前光标位置周围拉出 (y) 21 行并将它们放入寄存器 中 >b

缺少输入实际上也代表当前光标位置,这意味着:

:-5,y a

会将上面 5 行到当前光标位置的文本拉入命名缓冲区 a 中,并且:

:,+5y a

将当前光标位置之后的 5 行复制到缓冲区 a 中。

注意:如果缓冲区 a 中有一个宏,它就会被之前的复制覆盖,就像复制一样。寄存器和宏寄存器实际上是同一件事。这就是为什么,巧合的是,您可以粘贴宏,对其进行编辑,然后将其拉回到寄存器中。我个人使用左手触及的字母进行猛拉,使用右手触及的字母进行宏。

移动文本块,看起来像这样:

:+10,+13m.

这意味着将当前光标前面 10 行的四行移动到当前行的下方。

附录

我之前混淆了 :91,95ya a 中的 yaya{motion} 的同义词,其中运动由 91,95 提供。这是不正确的,ya 中的“a”是完全没有必要的。在我看来,我的 help yank 并未表明 ya 可能是 yank 的别名。

In addition to :91,96y a which yanks (y) lines 91 through 96 into register a, (pasted with "ap), the yanked lines can be appended to the register with:

:91,96y A

I.e. the capitalization of the A register causes an appending operation into register a instead of an overwrite. Capitalization of the register always works like this, e.g. :let @A=';' appends a ; to register a.

Using plus (+) or minus (-) references lines relative to the current cursor position:

:-10,+10y b

I.e. it would yank(y) 21 lines around the current cursor position and put them in register b.

An absence of input actually represents the current cursor position as well, which means that this:

:-5,y a

would yank the text from 5 lines above to current cursor position into named buffer a, and:

:,+5y a

would yank the 5 lines after the current cursor position into buffer a.

Note: If you have a macro in buffer a it was just overwritten by the previous yank, as yank registers and macro registers are really the same thing. Which is why, coincidentally, you can paste a macro, edit it, and then yank it back into it's register. I personally use letters reached by my left hand for yanks, and letters reached by my right hand for macros.

Moving blocks of text around, looks like this:

:+10,+13m.

which means move the four lines positioned 10 lines ahead of current cursor, to below the current line.

Addendum

I previously confused ya in :91,95ya a to be somehow synonymous with ya{motion} where the motion was supplied by 91,95. This was incorrect and the "a" in ya is completely unnecessary. In my defense, my help yank does not convey that ya is a possible alias of yank.

海的爱人是光 2024-08-24 00:16:06

g 本身不执行任何操作。它是包含一堆不相关命令的几个元命令之一。

z 是另一个类似的命令。

g doesn't do anything by itself. It's one of a couple meta-commands that holds a bunch of sorta-unrelated commands.

z is yet another command like that.

两人的回忆 2024-08-24 00:16:06

最好的解决方案是按 v 进入“视觉模式”。选择行后,只需按 y 复制它们。然后按 p 粘贴复制的行。

The best solution would be to enter "visual mode", by pressing v. And after selecting lines just copy them by pressing y. Then paste copied lines by pressing p.

楠木可依 2024-08-24 00:16:06

Vim 的 :help indexg 描述为:

|g|             g{char}            extended commands, see |g| below

向下滚动(或 :help g)查看列表。

Vim's :help index describes g as:

|g|             g{char}            extended commands, see |g| below

Scroll down (or :help g) for a list.

刘备忘录 2024-08-24 00:16:06

作为 Vi/Vim 的长期用户,我倾向于使用“标记”代替行号(或“行标记”)。它的工作原理如下:m 是“标记”字符;然后使用任何字母来识别/命名该标记。要返回标记,请在命名标记前加上单引号 ('a),这些标记可用作范围。示例:

File:
    <line 1>
    <line 2>
    <line 3>
    <line 4>
    <line 5>

在命令模式下将光标移动到第 2 行,输入ma。滚动到第 4 行,输入mb
要从标记 a 拉到标记 b,请键入:

    :'a,'byank

要从标记 a 删除到标记 b,请键入:

    :'a,'bdel

要搜索从标记a到标记b并将'ine'替换为'ink':

    :'a,'bs/ine/ink/g

复制标记a到标记b > 并粘贴到当前位置下方(“点”始终引用光标当前所在的行):

    :'a,'bco . 

在标记 a 到标记 b 之间移动代码行,向右移动一个选项卡(使用相反的 V 形符号 < 向左移动):

    :'a,'b> 

在命令模式下,只需键入 'a 即可返回到标记标记为 a 的行。输入 '' 会将您移回到上一个位置(不幸的是,只记住前一个位置,而不是后两个位置)。

您可以拉入命名缓冲区、复制、删除行、搜索和替换部分代码等,而无需知道行号。

As a long time Vi/Vim user I tend to use 'marks' instead of line numbers (or 'line markers'). It works like this: m is the 'mark' character; then use any letter to identify/name the mark. To return to a mark preface the named mark with a single quote ( 'a)These marks can be used as the range. Examples:

File:
    <line 1>
    <line 2>
    <line 3>
    <line 4>
    <line 5>

When in command mode move cursor to line 2, typema. scroll to line 4, typemb.
To yank from mark a to mark b type:

    :'a,'byank

To delete from mark a to mark b type:

    :'a,'bdel

To search from mark a to mark b and replace 'ine' with 'ink':

    :'a,'bs/ine/ink/g

To copy mark a through mark b and paste below the current position (the 'dot' always references the line where the cursor currently is positioned):

    :'a,'bco . 

Shift lines of code, between mark a through mark b, one tab to the right (use opposite chevron, <, to move left):

    :'a,'b> 

In command mode you can move back to marks by simply typing 'a to move back to the line marked a. Typing '' moves you back to previous position (unfortuantely only remembers the previous position, not two back).

You can yank to named buffers, copy, delete lines, search&replace just portions of your code, etc. without needing to know the line numbers.

独﹏钓一江月 2024-08-24 00:16:06

要将行从第 81 行拉到第 91 行:

方法 1:81gg11yy

不错,但你必须做一些数学运算才能找出要拉动的行数

方法 2:81gg 然后 shift+v 然后 91gg然后y

我认为这是最好的,因为这很简单,你只需要知道明显的事情,即你想要从哪个行号拉到哪个行号

To yank lines from line number 81 to 91 :

approach 1: 81gg11yy

not bad but you have to do little bit of math to find out how many lines to yank

approach 2: 81gg then shift+v then 91gg then y

BEST IN MY OPINION because this is straight forward, you only have to know the obvious thing i.e from which line number to which line number you want to yank

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