简体   繁体   English

vim宏,直到匹配模式

[英]vim macro until pattern is matched

I have an array of states that no one is going to have to modify often, so I want to remove the white space. 我有一系列状态,没有人需要经常修改,因此我想删除空白。

I tried the following keystrokes as there are some whitespace characters between the commas in the array and the endlines: 我尝试了以下击键,因为数组和结尾行之间的逗号之间有一些空格字符:

q a /, ENTER FORWARD v /\\n ENTER d q a /, ENTER FORWARD v /\\n ENTER d

Unfortunately, whomever formatted this neglected to place any whitespace after 'Montana', so when I run the macro 51@a , it breaks after 27 iterations. 不幸的是,任何格式化此格式的人都忽略了将任何空格放在'Montana'之后,因此当我运行宏51@a ,它在27次迭代后就中断了。

How can I have a macro only run a pattern if \\s is matched, or better yet, how can I run a macro until it recognizes ); 我如何才能让宏仅在\\s匹配的情况下运行模式,或者更好的是,如何运行宏直到它识别出); (end of array). (数组末尾)。

EDIT: Here is an example. 编辑:这是一个例子。 Note the two white space characters after all entries except keys MT , NE , NV and NH . 请注意除MTNENVNH键之外的所有条目之后的两个空格字符。

$state_list = array('AL'=>"Alabama",  
        'AK'=>"Alaska",  
        'AZ'=>"Arizona",  
        'AR'=>"Arkansas",  
        'CA'=>"California",  
        'CO'=>"Colorado",  
        'CT'=>"Connecticut",  
        'DE'=>"Delaware",  
        'DC'=>"District Of Columbia",  
        'FL'=>"Florida",  
        'GA'=>"Georgia",  
        'HI'=>"Hawaii",  
        'ID'=>"Idaho",  
        'IL'=>"Illinois",  
        'IN'=>"Indiana",  
        'IA'=>"Iowa",  
        'KS'=>"Kansas",  
        'KY'=>"Kentucky",  
        'LA'=>"Louisiana",  
        'ME'=>"Maine",  
        'MD'=>"Maryland",  
        'MA'=>"Massachusetts",  
        'MI'=>"Michigan",  
        'MN'=>"Minnesota",  
        'MS'=>"Mississippi",  
        'MO'=>"Missouri",  
        'MT'=>"Montana",
        'NE'=>"Nebraska",
        'NV'=>"Nevada",
        'NH'=>"New Hampshire",
        'NJ'=>"New Jersey",
        'NM'=>"New Mexico",
        'NY'=>"New York",
        'NC'=>"North Carolina",
        'ND'=>"North Dakota",
        'OH'=>"Ohio",  
        'OK'=>"Oklahoma",  
        'OR'=>"Oregon",  
        'PA'=>"Pennsylvania",  
        'RI'=>"Rhode Island",  
        'SC'=>"South Carolina",  
        'SD'=>"South Dakota",
        'TN'=>"Tennessee",  
        'TX'=>"Texas",  
        'UT'=>"Utah",  
        'VT'=>"Vermont",  
        'VA'=>"Virginia",  
        'WA'=>"Washington",  
        'WV'=>"West Virginia",  
        'WI'=>"Wisconsin",  
        'WY'=>"Wyoming"
);

To: 至:

$state_list=array('AL'=>"Alabama",'AK'=>"Alaska",'AZ'=>"Arizona",'AR'=>"Arkansas",'CA'=>"California",'CO'=>"Colorado",'CT'=>"Connecticut",'DE'=>"Delaware",'DC'=>"District Of Columbia",'FL'=>"Florida",'GA'=>"Georgia",'HI'=>"Hawaii",'ID'=>"Idaho",'IL'=>"Illinois",'IN'=>"Indiana",'IA'=>"Iowa",'KS'=>"Kansas",'KY'=>"Kentucky",'LA'=>"Louisiana",'ME'=>"Maine",'MD'=>"Maryland",'MA'=>"Massachusetts",'MI'=>"Michigan",'MN'=>"Minnesota",'MS'=>"Mississippi",'MO'=>"Missouri",'MT'=>"Montana",'NE'=>"Nebraska",'NV'=>"Nevada",'NH'=>"New Hampshire",'NJ'=>"New Jersey",'NM'=>"New Mexico",'NY'=>"New York",'NC'=>"North Carolina",'ND'=>"North Dakota",'OH'=>"Ohio",'OK'=>"Oklahoma",'OR'=>"Oregon",'PA'=>"Pennsylvania",'RI'=>"Rhode Island",'SC'=>"South Carolina",'SD'=>"South Dakota",'TN'=>"Tennessee",'TX'=>"Texas",'UT'=>"Utah",'VT'=>"Vermont",'VA'=>"Virginia",'WA'=>"Washington",'WV'=>"West Virginia",'WI'=>"Wisconsin",'WY'=>"Wyoming");

EDIT: 编辑:

Just googled vim macro until pattern matched, and came across my own question. 只是谷歌vim宏,直到模式匹配,并遇到了我自己的问题。 I have a better example now: 我现在有一个更好的例子:

    namespace A{
            class a{}
            class a{}
    }
    namespace B{
            class b{}
            class b{}
            class b{}
    }

Needs to become: 需要成为:

    namespace A{
            class Aa{}
            class Aa{}
    }
    namespace B{
            class Bb{}
            class Bb{}
            class Bb{}
    }

This cannot be solved with the previously accepted answer. 这不能用先前接受的答案解决。

Honestly the easiest answer would be to join the lines. 坦率地说,最简单的答案就是加入队伍。

If you visual select the entire region and just press J (or feed the range to :join ) all of the lines will end up on one line. 如果视觉上选择整个区域,然后按J (或将范围输入到:join ),则所有行将以一行结尾。 (There may be excess whitespace in-between elements but thats easier to fix then trying to write the macro). (元素之间可能会有多余的空格,但这更容易解决,然后尝试编写宏)。

If you then want to remove the excess whitespace you could run 如果您想删除多余的空格,可以运行

:s/,\s\+/,/g

on the joined line. 在连接线上。

Take a look at :h J and :h :join 看看:h J:h :join

First of all, you can easily remove all trailing whitespace with one command: 首先,您可以使用一个命令轻松删除所有结尾的空格:

:%s/\s\+$//e

That will work on every line of the file. 这将适用于文件的每一行。 If you want to do it only on this fragment of code, you can specify different range (instead of %, which is whole file). 如果只想在这段代码上执行此操作,则可以指定不同的范围(而不是%,它是整个文件)。 For example, you can pass line numbers (1 to 51): 例如,您可以传递行号(1到51):

:1,51s/\s\+$//e

or first visually select, and then run command removing whitespace (you have to be on the first line starting this sequence): 或首先在视觉上进行选择,然后运行命令删除空格(您必须在开始此序列的第一行中):

V/)<CR>:s/\s\+$//e

If you really want to use macro, you can slightly tweak (and simplify) your macro: 如果您确实想使用宏,则可以稍微调整(并简化)宏:

qa0f,lDq

and then run it on every line at once by first of all visually selecting all of them (again, start on line you want, I won't put gg here because it might be just part of the file): 然后首先在视觉上选择所有它们,然后一次在每一行上运行它(再次,从所需的行开始,我不会在这里放gg ,因为它可能只是文件的一部分):

V/)<CR>:normal @q

This will play the macro over every selected line, up to the line with ) . 这将播放宏在每个选定的线,最多与线)

I hope it helps :) 希望对您有所帮助:)

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

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