Heim  >  Fragen und Antworten  >  Hauptteil

vimscript – vim löscht oder ersetzt den gesamten Abschnitt

Es gibt einen Text wie diesen

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.

Ich hoffe, ich kann alle Zeilen zwischen --BEGIN-- --END-- löschen. Gibt es eine direkte Methode in vim?

Eine weitere zusätzliche Frage ist, wie man einen Inhalt hinzufügt, wenn bereits eine
--BEGIN---Markierung vorhanden ist.

Alle oben genannten hoffen, Textdateien auf skriptbasierte Weise verarbeiten zu können.


Nur ein Beispiel: Die Anzahl der Zeilen zwischen --BEGIN--, --END-- ist ungewiss
Das manuelle Markieren ist etwas mühsam. Die eigentliche Datei ist nicht klein und hat eine ähnliche Struktur
Mehr.

ringa_leeringa_lee2712 Tage vor764

Antworte allen(5)Ich werde antworten

  • ringa_lee

    ringa_lee2017-05-16 16:43:00

    :g/--BEGIN--/,/--END--/d
    如果要同时删除前后的空行
    :g/--BEGIN--/-1,/--END--/+1d

    Antwort
    0
  • 怪我咯

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

    vim可以做到,

    在命令模式下:

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

    其中的\_是关键,这个可以匹配多行。
    已经有--BEGIN--标识如何替换,这个也可以用上面的替换思路来做
    :%s/^--BEGIN--/&要替换的内容/g

    Antwort
    0
  • PHP中文网

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

    光标移至开始部分
    Esc v 进行view模式
    j 向下选中操作部分
    d 删除 x 剪切 两个都可以

    Antwort
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 16:43:00

    既然要脚本化,就用 sed 呗:

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

    Antwort
    0
  • 淡淡烟草味

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

    ndd,n为要删除的行数。

    Antwort
    0
  • StornierenAntwort