search

Home  >  Q&A  >  body text

vimscript - vim delete or replace entire section

There is a text like this

As of initscripts-2012.07.5, the default format of /etc/rc.conf and
/etc/crypttab have changed. See their respective man pages for 
more details.

--BEGIN--
The purpose of this change is to unify the configuration of systemd
and initscripts. This will allow us to share code and documentation, 
and should make maintenance of initscripts simpler in the long-run.
--END--

The old format is still supported, so old config files should still
work unchanged.

Hopefully --BEGIN-- Delete all the lines between --END--. Is there any direct method for vim?

Another additional question is how to add a piece of content when there is already a
--BEGIN-- mark.

The above all hope to process text files in a scripted manner.


Just an example, the number of lines between --BEGIN--,--END-- is uncertain
Marking this manually is a bit laborious. The actual file is not small and has a similar structure
More.

ringa_leeringa_lee2776 days ago837

reply all(5)I'll reply

  • ringa_lee

    ringa_lee2017-05-16 16:43:00

    :g/--BEGIN--/,/--END--/d
    If you want to delete the blank lines before and after at the same time
    :g/--BEGIN--/-1,/--END--/+1d

    reply
    0
  • 怪我咯

    怪我咯2017-05-16 16:43:00

    vim can do it,

    In command mode:

    %s/--BEGIN--\_.*--END--//g
    

    The _ is the key, this can match multiple lines.
    There is already a way to replace the --BEGIN-- logo. This can also be done using the above replacement idea
    :%s/^--BEGIN--/&Content to be replaced/g

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 16:43:00

    Move the cursor to the beginning
    Esc v to enter view mode
    j Scroll down to select the operation part
    d Delete x Cut Both are OK

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 16:43:00

    Since you want to script it, just use sed:

    sed -i '/--BEGIN--/,/--END--/d' files_to_modify
    

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-16 16:43:00

    ndd, n is the number of rows to be deleted.

    reply
    0
  • Cancelreply