Vim 拉动行范围
我是一名 C# 开发人员,最近决定扩展我对可用工具的了解。我决定学习的第一个工具是 Vi/Vim。到目前为止,一切进展顺利,但有几个问题我似乎找不到答案:
假设我想删除一系列行。我知道有很多方法可以做到这一点,但我想通过行号来做到这一点。我认为它与替代命令的工作方式类似,例如
81,91y
。有办法做到这一点吗?我对正常模式下的
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:
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?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 theg
command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
猛拉第 81-91 行
可以(转到第 81 行,猛拉 11 行)
如果您的手指不喜欢找到
:
和,
键,这也 仅使用g
为5gg
。去5号线。22gg
:第22行。正如 jimbo 所说,它实际上只是其他一些命令的修饰符。为了完整起见,(http://vim.wikia.com/wiki/Power_of_g) 解释了很多
g
在命令模式下如何工作。Yank lines 81-91
If your fingers don't like to find the
:
and,
keys, this would work as well (go to line 81, yank 11 lines)My only use of
g
is5gg
. 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.您还可以使用“t”将当前行复制到当前光标位置。
这会将第 81-91 行粘贴到光标所在行的下方。
我从 http://vimcasts.org 学到了这一点,这是关于 VIM 的优秀资源。
You can also copy the current lines to your present cursor location using 't'.
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.
如果伴有计数值,
G
命令将转到某个行号。81G
会将您置于第 81 行。y
命令可以与移动结合使用,例如G
。因此,要将所有内容拉到第 91 行,您可以使用 y91G。一起得到:
转到第 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, likeG
. So to yank everything until line 91 you can usey91G
.Together you get:
Go to line 81, then yank while going to line 91.
我还喜欢使用 vim 的相对行号选项(set rnu),这意味着我只需输入:
将文本拉入命名缓冲区 a 中。
注意指定 A 会将您要拉取的内容附加到缓冲区 a 的当前内容中。
不要忘记,您还可以使用类似的命令复制文本块和移动文本块:
表示将四行文本复制到当前行上方 10 行,
表示将四行文本移动 10 行从当前行上方到下方。
I also like to use vim's relative line number option (set rnu) which means I can just enter:
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:
means copy the four lines of text 10 lines above to below the current line, and
means move the four lines of text 10 lines above to below the current line.
除了
:91,96y a
之外,它将 (y
) 第 91 至 96 行拉入寄存器a
中,(用"ap 粘贴
),被拉出的行可以附加到寄存器:即
A
寄存器的大写导致对寄存器a
的附加操作code> 而不是覆盖。寄存器的大写总是这样工作,例如:let @A=';'
附加一个;
到寄存器a
使用加号 (+) 或减号 (-) 引用相对于当前光标位置的行:
即,它将在当前光标位置周围拉出 (
y
) 21 行并将它们放入寄存器中 >b
。缺少输入实际上也代表当前光标位置,这意味着:
会将上面 5 行到当前光标位置的文本拉入命名缓冲区
a
中,并且:将当前光标位置之后的 5 行复制到缓冲区
a
中。注意:如果缓冲区
a
中有一个宏,它就会被之前的复制覆盖,就像复制一样。寄存器和宏寄存器实际上是同一件事。这就是为什么,巧合的是,您可以粘贴宏,对其进行编辑,然后将其拉回到寄存器中。我个人使用左手触及的字母进行猛拉,使用右手触及的字母进行宏。移动文本块,看起来像这样:
这意味着将当前光标前面 10 行的四行移动到当前行的下方。
附录
我之前混淆了
:91,95ya a
中的ya
与ya{motion}
的同义词,其中运动由91,95
提供。这是不正确的,ya
中的“a”是完全没有必要的。在我看来,我的help yank
并未表明ya
可能是yank
的别名。In addition to
:91,96y a
which yanks (y
) lines 91 through 96 into registera
, (pasted with"ap
), the yanked lines can be appended to the register with:I.e. the capitalization of the
A
register causes an appending operation into registera
instead of an overwrite. Capitalization of the register always works like this, e.g.:let @A=';'
appends a;
to registera
.Using plus (+) or minus (-) references lines relative to the current cursor position:
I.e. it would yank(
y
) 21 lines around the current cursor position and put them in registerb
.An absence of input actually represents the current cursor position as well, which means that this:
would yank the text from 5 lines above to current cursor position into named buffer
a
, and: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:
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 withya{motion}
where the motion was supplied by91,95
. This was incorrect and the "a" inya
is completely unnecessary. In my defense, myhelp yank
does not convey thatya
is a possible alias ofyank
.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.最好的解决方案是按 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.
Vim 的
:help index
将g
描述为:向下滚动(或
:help g
)查看列表。Vim's
:help index
describesg
as:Scroll down (or
:help g
) for a list.作为 Vi/Vim 的长期用户,我倾向于使用“标记”代替行号(或“行标记”)。它的工作原理如下:
m
是“标记”字符;然后使用任何字母来识别/命名该标记。要返回标记,请在命名标记前加上单引号 ('a
),这些标记可用作范围。示例:在命令模式下将光标移动到第 2 行,输入
ma
。滚动到第 4 行,输入mb
。要从标记 a 拉到标记 b,请键入:
要从标记 a 删除到标记 b,请键入:
要搜索从标记a到标记b并将'ine'替换为'ink':
复制标记a到标记b > 并粘贴到当前位置下方(“点”始终引用光标当前所在的行):
在标记 a 到标记 b 之间移动代码行,向右移动一个选项卡(使用相反的 V 形符号
<
向左移动):在命令模式下,只需键入
'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:When in command mode move cursor to line 2, type
ma
. scroll to line 4, typemb
.To yank from mark a to mark b type:
To delete from mark a to mark b type:
To search from mark a to mark b and replace 'ine' with 'ink':
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):
Shift lines of code, between mark a through mark b, one tab to the right (use opposite chevron,
<
, to move left):In command mode you can move back to marks by simply typing
'a
to move back to the line markeda
. 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.
要将行从第 81 行拉到第 91 行:
方法 1:
81gg11yy
方法 2:
81gg
然后shift+v
然后91gg
然后y
To yank lines from line number 81 to 91 :
approach 1:
81gg11yy
approach 2:
81gg
thenshift+v
then91gg
theny