[Practical.Vim(2012.9)].Drew.Neil.Tip28 学习摘要

Tip28

Use Line Numbers as an Address

If we enter an Ex command consisting only of a number, then Vim will interpret that as an address and move our cursor to the specified line.

Specify a Range of Lines by Visual Selection

Instead of addressing a range of lines by number, we could just make a visual selection. If we ran the command 2G followed by VG, we would make a visual selection that looks like this:

除了通过行数来指定文本区域,我们可以先做一个visual selection.如果我们依次运行命令2G,VG,我们就会得到这样一个visual selection

If we press the : key now, the command-line prompt will be prepopulated with the range :’<,’>. It looks cryptic, but you can think of it simply as a range standing for the visual selection. Then we can specify our Ex command, and it will execute on every selected line:

如果这时我们输入: ,命令提示会弹出范围 :’<,’> 。这个看起来很奇怪,但是你可以把这个当作一个visual selection的选择范围。然后我们可以输入Ex命令,然后就会在每一行上执行。

The ‘< symbol is a mark standing for the first line of the visual selection, while ‘> stands for the last line of the visual selection.

‘<表示visual selection的第一行,’>表示visual selection的最后一行。

Specify a Range of Lines by Patterns

Vim also accepts a pattern as an address for an Ex command, such as the one shown here:

Vim在Ex命令中还可以把pattern作为address,例如下面

the range begins on the line containing an opening <html>tag and ends on the line containing the corresponding closing tag.

这个范围开始于包含<html>标签的行,结束于包含对应关闭标签的行。

Modify an Address Using an Offset

Suppose that we wanted to run an Ex command on every line inside the<html></html>block but not on the lines that contain the<html> and</html> tags themselves. We could do so using an offset:

假设我们打算在<html></html> 范围内的每一行执行Ex命令,但是并不包含<html></html> 所在行,我们可以用offset来实现。

The general form for an offset goes like this:

offset的一般使用方法如下:

:{address}+n

If n is omitted, it defaults to 1. The {address} could be a line number, a mark, or a pattern.

如果没有n,默认值为1. address可以为行数,mark,或pattern.

Suppose that we wanted to execute a command on a particular number of lines, starting with the current line. We could use an offset relative to the current line:

假设我们打算在特定的行上执行命令,从当前行开始,我们可以利用offset这样做。

:.,.+3p

The . symbol stands for the current line, so :.,.+3 is equivalent to :2,5 in this case.

. 表示当前行,所以 :.,.+3 在这个例子中表示:2,5

Line 0 doesn ’t really exist, but it can be useful as an address in certain contexts. In particular, it can be used as the final argument in the :copy {address} and :move {address} commands when we want to copy or move a range of lines to the top of a file

0行并不存在,不过在特定上下文中非常有用。特别是当我们项复制或移动一段代码到文件的头部的时候,它可以作为:copy {address}:move {address} 命令的最终参数。

时间: 2024-10-24 19:53:11

[Practical.Vim(2012.9)].Drew.Neil.Tip28 学习摘要的相关文章

[Practical.Vim(2012.9)].Drew.Neil.Tip97 学习摘要

Meet The Global Command The :global command allows us to run an Ex command on each line that matches a particular pattern. Let's start by studying its syntax. The :global command takes the following form (see :h :g ): :global命令可以让我们在每一行匹配特定pattern的文本

[Practical.Vim(2012.9)].Drew.Neil.Tip10学习摘要

Use Counts to Do Simple Arithmetic 在vim中,执行<C-a>和<C-x>命令可以对文本中的数字直接进行加或减. 如果不提供数字而直接执行上面的命令的话,默认对光标所在的数字值进行加1或减1. 如果在命令前面加一个数字,就会对光标所在的数字加或减这个数. 如果光标所在位置不是数字,就会自动在当前行后面查找数字然后定位到该数字上. 如下面的测试文件 我们要复制最后一行然后把0px改为-180px. 除了直接对数字进行更改外可以使用命令<C-x&

[Practical.Vim(2012.9)].Drew.Neil.Tip51 学习摘要

Trace Your Selection with Precision Text Objects 对于括号,引号,以及例如html,xml中的标签<a> </a>等,都是成对出现,Vim能够理解这种结构方式,对它们限定的区域文本进行选择. 对于如下js代码 执行如下命令 最开始光标在url的r上,然后输入v命令,进入 visual模式,i}表示选择{}里面的内容但是不包含{},a"表示选择""内的内容且包含""..it表示包含标签内

[Practical.Vim(2012.9)].Drew.Neil.Tip49 学习摘要

Find by Character 在Vim中快速定位到某个字符的命令为f In this case, the fx command does nothing. Vim searches forward for an occurrence of the "x" character, but no matches are found, so the cursor doesn 't move. The fo command finds an occurrence of the "

[Practical.Vim(2012.9)].Drew.Neil.Tip50 学习摘要

Search to Navigate 在Vim中快速定位到某处的另外方法就是搜索word,使用方法就是符号/后跟要搜索的word,如下: 由上图可以看出执行命令后会显示多个匹配项,我们可以使用n或N命令将光标移动到前一个或后一个匹配项上. Operate with a Search Motion 类似f命令,我们可以结合操作命令和移动命令(在Vim中称搜索命令为移动命令,因为移动了光标,前面的f命令也是移动命令),来快速对某个区域文本进行操作,如删除或修改. 我们在normal模式下输入v进入v

[Practical.Vim(2012.9)].Drew.Neil.Tip16学习摘要

Do Back-of-the-Envelope Calculations in Place 在vim中我们可以利用表达式寄存器来直接进行数学运算然后把结果插入我们的文档. 表达式寄存器可以通过=来访问,在insert模式中我们可以输入<C-r>=来启动这个表达式寄存器,这时在屏幕底部出现一个终端,我们输入计算式,然后回车,计算结果就会插入到文档光标所在位置.如下:

[Practical.Vim(2012.9)].Drew.Neil.Tip12学习摘要

Operator+Motion=Action 在Vim中,d{motion}命令可以删除单个字符dl,也可以删除整个单词daw,整个段落dap. 同样的可以用c命令来改变单词caw或锻炼cap.y命令类似使用. g~, gu, gU命令是由两个语法组成.如果我们要让整个word变为大写就可可以gUaw, 让整个段落变为大写,用gUap. vim还有另外一个语法就是,当一个operaor 命令重复出现时,在当前行执行命令.例如dd删除当前行,yy复制当前行,gu命令是个特殊例子,我们可以用gUgU

[Practical.Vim(2012.9)].Drew.Neil.Tip100学习摘要

Alphabetize the Properties of Each Rule in a CSS File When combining an Ex command with :global, we can also specify a range for our chosen [cmd]. Vim allows us to set the range dynamically using the :g/{pattern} as a reference point. 当用:global结合Ex命令

[Practical.Vim(2012.9)].Drew.Neil.Tip47 学习摘要

Distinguish Between Real Lines and Display Lines Unlike many text editors, Vim makes a distinction between real lines and display lines. When the 'wrap' setting is enabled (and it's on by default), each line of text that exceeds the width of the wind