質問

Using vim. My original file looks like this:

Wire 1_blah
Wire 2_blah
Wire 3_blah
reg blah_blah_1
reg blah_blah_2
reg blah_blah_3

(Except, imagine about 4000 lines)

What I want as a result is the following:

Wire 1_blah
Wire 2_blah

reg blah_blah_1
reg blah_blah_2

Wire 3_blah
reg blah_blah_3

Meaning, I want to delete all lines with the number 3 and paste them at the end. I tried the following:

:'<,'>g/3/d

and then

p

But this only remembers the last line deleted.

Any suggestions on how to do this quickly and efficiently? I'd hate to lose time doing it manually.

Thanks!

役に立ちましたか?

解決

:g/3/m$

does what you want:

  • :g[lobal]/foo/command executes command on every line matching foo,
  • :m$ moves the current line to the end of he buffer,
  • :g/3/m$ moves all lines containing a 3 to the end of the buffer.

See :help :global and :help :move.

Ex commands for the win!

edit

To cut all those lines in the same register, say register a, use:

:g/3/d A

meaning "cut and append these lines to register a".

See :help registers.

他のヒント

If you look at :help :g, you will see an option to execute normal commands on the matches. the following command will move every line to the bottom, in the order they were found.

:g/3/normal ddGp

Documentation copied from :help :g.

To repeat a non-Ex command, you can use the ":normal" command: >
    :g/pat/normal {commands}
Make sure that {commands} ends with a whole command, otherwise Vim will wait
for you to type the rest of the command for each match.  The screen will not
have been updated, so you don't know what you are doing.  See |:normal|.

I don't know if those empty lines in your output example are needed. if not, the other answers work for you. If you really want those empty lines, this line works for you:

:g/3/exec "normal! DGo\<esc>p"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top