89

私はC#開発者であり、利用可能なツールに関する知識を拡大することを最近決定しました。私が学ぶことにした最初のツールはVi/Vimです。これまでのところすべてが順調に進んでいますが、答えが見つからないように思われる質問がいくつかあります。

  1. ある範囲の行をヤンクしたいとしましょう。いろいろな方法があることは知っていますが、行番号でやりたいと思います。のように、代替コマンドがどのように機能するかに似ていると思いました81,91y。これを行う方法はありますか?

  2. g通常モードのコマンドについて少し混乱しています。それは無数のことをしているようで、gコマンドがそのコアで何をしているのかを実際に判断することはできません。それがモーションコマンドなのか、それとも通常モードで実行された他のコマンドの一種の「キャッチオール」なのか、私は混乱しています。g誰かがこれを説明するか、コマンドの良い説明を与えるリファレンスを私に教えてもらえますか?

4

10 に答える 10

146

ヤンクライン81-91

:81,91y<enter>

:指がキーとキーを見つけたくない場合は,、これも機能します(81行目に移動し、11行をヤンクします)

81gg11yy 

私の唯一の用途は gです5gg。5行目に移動します。 22gg:22行目。jimboが言ったように、それは実際には他のいくつかのコマンドの修飾子にすぎません。

完全を期すために、(http://vim.wikia.com/wiki/Power_of_g)はgコマンドモードでどのように機能するかについて多くのことを説明しています。

于 2010-01-07T19:27:00.857 に答える
24

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.

于 2014-12-16T09:49:17.840 に答える
12

I also like to use vim's relative line number option 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.

于 2014-05-24T16:20:57.350 に答える
11

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.

于 2015-02-04T12:11:58.560 に答える
4

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.

于 2010-01-07T19:31:34.373 に答える
3

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.

于 2018-06-07T12:33:19.513 に答える
2

Vim's :help index describes g as:

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

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

于 2010-01-07T19:37:15.800 に答える
2

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.

于 2019-08-11T02:31:16.707 に答える
1

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.

于 2019-09-25T23:54:42.153 に答える
0

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

于 2019-10-31T18:00:45.057 に答える