Vim宏在视觉选择的每一行 [英] Vim Macro on Every Line of Visual Selection

查看:106
本文介绍了Vim宏在视觉选择的每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在选择中的每一行上运行一个宏,而不是总计总的行数.例如,我可能会编写一个宏进行转换:

I'd like to run a macro on every line in a selection, rather than totalling up the number of lines in my head. For instance, I might write a macro to transform:

Last, First

进入

First Last

我希望它可以在所有这些行上运行:

and I'd like it to run on all these lines:

Stewart, John 
Pumpkin, Freddy
Mai, Stefan
...

有Vim大师的想法吗?

Any ideas Vim gurus?

这只是一个例子,显然这很容易被重新定义,但是出现了其他一些实例并不太容易,以至于我不喜欢使用宏.

This is just an example, obviously this is trivialy regexable, but there are other instances that come up that aren't quite so easy that I'd prefer to use macros.

推荐答案

假设您有一个宏q,该宏在一行上运行(并保持运行).然后,您可以使用以下命令在选择的每一行上运行它:

Suppose you had a macro q that ran (and remained) on a single line. Then you could run it on every line in your selection with:

:'<,'>normal @q

(如果您已经选择了一组行,则在命令行上按:会产生:'<,'>)

(if you already have a group of lines selected, hitting : produces :'<,'> on the command line)

例如,以下宏将一行中除第一个单词外的所有单词大写:

For example, the following macro capitalizes every word but the first on a line:

:let @q="^dwgU$P"

因此,请在以下(选择+行的地方)上运行它

So running it on the following (where the + lines are selected)

 0000: a long long time ago
 0001: in a galaxy far away
+0002: naboo was under an attack
+0003: and i thought me and qui-gon jinn
+0004: could talk the federation in
 0005: to maybe cutting them a little slack.

使用上面的normal @q命令,生成:

With the above normal @q command, produces:

 0000: a long long time ago
 0001: in a galaxy far away
 0002: naboo WAS UNDER AN ATTACK
 0003: and I THOUGHT ME AND QUI-GON JINN
 0004: could TALK THE FEDERATION IN
 0005: to maybe cutting them a little slack.

这篇关于Vim宏在视觉选择的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆