简体   繁体   English

用于在vim中用空格包围字符的命令

[英]Command to surround a character with spaces in vim

I am trying to use vim properly - to aid me I've mapped my arrow keys to "" so that I am forced to use {hjlk} to move around. 我正在尝试正确使用vim - 为了帮助我,我将箭头键映射到“”,以便我被迫使用{hjlk}来移动。

This is causing me a problem when I want to just surround a character with spaces, eg: 当我想用空格包围一个字符时,这会导致我出现问题,例如:

"2+3" is better formatted "2 + 3" “2 + 3”格式更好“2 + 3”

Previously I would have put my cursor over the + and typed: 以前我会将光标放在+上并键入:

i[space][arrow-right][space][Esc] I [空间] [右箭头] [空间] [Esc]键

That's 5 presses. 这是5台印刷机。

To do this without the arrow I seem to need to put the cursor over the + and go: 要在没有箭头的情况下执行此操作,我似乎需要将光标放在+上并继续:

i[space][Esc]lli[space][Esc] I [空间] Esc键LLI [空格] [Esc]键

That's 8 presses. 那是8台印刷机。

I can convert the "li" into an "a" which reduces it to 7 presses: 我可以将“li”转换为“a”,将其减少到7个印刷机:

i[space][Esc]la[space][Esc] I [空间] [Esc键] LA [空格] [Esc]键

Short of writing this into a macro is there a better way of doing it? 如果没有将其写入宏,有没有更好的方法呢? Is there some magic vim command which will allow me to do it in less than even 5 presses - and some way to generalise it so that I can do it to entire words or symbols, eg if I want to convert 3==4 to 3 == 4? 是否有一些神奇的vim命令允许我在少于5次按下的情况下执行此操作 - 以及某种方式来概括它以便我可以对整个单词或符号执行此操作,例如,如果我想将3 == 4转换为3 == 4?

Personally, I think it makes most sense to destroy what you want to surround, and then repaste it. 就个人而言,我认为最有意义的是破坏你想要包围的东西,然后重新制作它。

c w "" ESC P

Obviously, you can replace both the object and the quotes with whatever you like. 显然,你可以用你喜欢的任何东西替换对象和引号。 To change just one character + to be [space]+[space], you would do 要改变一个字符+为[空格] + [空格],你会这样做

s [space] [space] ESC P

on the + 在+

The first thing that jumps to mind after reading just the title is surround.vim which is an excellent script to do all kinds of useful things along the lines of what you've described. 在阅读完标题之后,首先想到的是surround.vim ,这是一个很好的脚本,可以根据您描述的内容进行各种有用的操作。

To solve your specific problem, I would probably position the cursor on the + and: 为了解决您的具体问题,我可能会将光标定位在+和:

s[space]+[space][esc] S [空间] + [空格] [ESC]

To change 3==4 into 3 == 4, I might position the cursor on the first =, and: 要将3 == 4更改为3 == 4,我可能会将光标定位在第一个=上,并且:

i[space][esc]ww. I [空间] [ESC] WW。

Hah! 哈! I've been trying to figure out how to surround a block in spaces for quite a while and I finally found the right combination. 我一直在试图弄清楚如何在空间中包围一个块很长一段时间,我终于找到了正确的组合。

Using surround.vim you say surround selector space space. 使用surround.vim你会说环绕选择器空间。

So for this specific case I would use visual mode (a good trick for operating on single characters under the cursor BTW) thus: "vs " <- four key presses! 所以对于这个特定的情况,我会使用视觉模式(操作光标BTW下的单个字符的一个很好的技巧)因此:“vs”< - 四键按下!

I also have a habit of typing things like argument lists without spaces. 我也习惯输入没有空格的参数列表之类的东西。 With this technique you can just navigate to the second argument using w and say "vws " to visually select a word and surround with spaces. 使用这种技术,你可以使用w导航到第二个参数并说“vws”以直观地选择一个单词并用空格环绕。

I prefer visual select mode generally. 我通常喜欢视觉选择模式。 Also the alternate surround syntax "ysw " excludes the word final comma that is caught by "vw". 另外的环绕语法“ysw”排除了由“vw”捕获的单词final逗号。

i have been wondering about this as well. 我一直在想这个。 i tried with surround.vim, but the naive approach 我尝试使用surround.vim,但天真的方法

S<space>

(after making a visual selection) does not work since the space is already taken up as a modifier for adding space to other surrounding character pairs. (在进行视觉选择之后)不起作用,因为空间已被占用为用于向其他周围字符对添加空间的修改器。 S<space><cr> adds a ^M in the output. S<space><cr>在输出中添加^ M. Ss almost works but inserts a space only before. Ss几乎可以工作,但之前只插入一个空格。

after asking at tpope/surround.vim on github: 在github上询问tpope / surround.vim之后:

S<space><space>

in visual mode works. 在视觉模式下工作。 alternatively, from normal mode, ysl<space><space> works for a single character 或者,从正常模式, ysl<space><space>适用于单个字符

Try positioning your cursor over the '+' and typing this: 尝试将光标放在'+'上并输入以下内容:

q1i[space][right arrow][space][left arrow][esc]q q1i [空格] [右箭头] [空格] [左箭头] [esc] q

This will record a quick macro in slot 1 that you can re-use whenever you feel like it, that will surround the character under the cursor with spaces. 这将在插槽1中录制一个快速宏,您可以随时重复使用它,它将用光标围绕光标下的字符。 You can re-call it with @1 . 你可以用@1重新调用它。

There is also the more versatile one: 还有一个多功能的:

q1ea[space][esc]bi[space][right arrow][esc]q q1ea [space] [esc] bi [space] [右箭头] [esc] q

Which will surround the word under the cursor ("==" counts as a word) with spaces when you hit @1 . 当你点击@1时,它将围绕光标下的单词(“==”计为单词)和空格。

You could create a macro with one of the described actions and call it everytime you need it (Like amphetamachine proposed while I was writing) or you could simply search & replace: 您可以使用所描述的操作创建一个宏,并在每次需要时调用它(就像我写作时提出的amphetamachine)或者您可以简单地搜索和替换:

:%s/\(\d\)\(+\|-\)\(\d\)/\1 \2 \3/g

You probably have to execute this command two times because it will only find every second occurence of +/-. 您可能必须执行此命令两次,因为它只会发现每秒出现+/-。

EDIT: 编辑:

This will replace everything without the need to be called twice: 这将取代所有内容而无需被调用两次:

:%s/\d\@<=+\|-\d\@=/ \0 /g

您可以设置这样的映射(按可视模式输入以包装空格):

:vnoremap <CR> <ESC>`<i<SPACE><ESC>`>la<SPACE><ESC>h

This method allows you to use . 此方法允许您使用。 to repeat the command at the next +. 在下一个+重复命令。

Put your cursor over the + and type: 将光标放在+上并键入:

s[SPACE][CTRL-R]"[SPACE][ESC]

I know this is and old thread, but this might be useful to someone. 我知道这是旧线程,但这可能对某人有用。 I've found that the map (map it to anything else you want!) 我发现了地图(把它映射到你想要的任何其他东西!)

noremap <leader>ss diwi<SPACE><C-R>"<SPACE><ESC>B

works ok both for turning 'a+b' into 'a + b' (when used over the '+' char) and for turning 'a==b' into 'a == b' (when used over either the first or the second '=' sign). 既可以将'a + b'转换为'a + b'(当用于'+'字符时),也可以将'a == b'转换为'a == b'(当用于第一个或第二个时)第二个'='符号)。

I hope it's useful to someone. 我希望它对某人有用。

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

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