简体   繁体   English

Vim - 删除直到找到模式,但反向

[英]Vim - delete until find a pattern, but in reverse direction

I know how to delete a part of text from cursor position to occurence of pattern, I mean d /<pattern> . 我知道如何从光标位置删除文本的一部分到模式的出现,我的意思是d /<pattern> I am wondering how to delete a part of text before cursor position until occurrence of pattern which is before cursor position. 我想知道如何在光标位置之前删除文本的一部分,直到出现在光标位置之前的模式。

Being in position x in file: 在文件中的位置x:

>>>>
aaaa
===x
bbbb
<<<<

I want to delete 我想删除

>>>>
aaaa
====

This is work if pattern is exists only once in file... and vim doesn't have a choice. 如果模式在文件中只存在一次,那么这是有效的...并且vim没有选择。 However there is a problem with file like this: 但是这样的文件存在问题:

>>>>
aaaa
====
bbbb
<<<<

foo
boo

>>>>
cccc
====
dddd
<<<<

You can search backwards using ? 你可以使用向后搜索? . So to delete back until a pattern, use d ?<pattern> . 因此,要删除直到模式,请使用d ?<pattern>

One can delete ahead using f or t. 可以使用f或t删除。 For example d2f_ would delete up to and including the second underscore, and d2t_ would delete up to but excluding the second underscore. 例如,d2f_将删除最多并包括第二个下划线,d2t_将删除最多但不包括第二个下划线。 I just tried with gVim on a machine running you-know-what and it works in reverse if you capitalise the f or t. 我只是在运行你知道什么的机器上尝试使用gVim,如果你把f或t大写的话它会反向运行。 So d2F_ would delete BACK TO and including the second-to-last underscore and d2T_ would delete BACK TO but excluding the second-to-last underscore. 因此d2F_将删除BACK TO并包括倒数第二个下划线,d2T_将删除BACK TO但排除倒数第二个下划线。

I'm so happy to have found this that I could just press my cheek glands. 我很高兴找到这个,我可以按下我的脸颊腺体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM