在vim中以反转逗号分隔列表的最快方法是什么?

时间:2022-08-22 00:19:56

I frequently must correct the following rails code:

我经常必须更正以下rails代码:

assert_equal value, expected

The two arguments to assert_equal are out of order, and should read:

assert_equal的两个参数是乱序的,应该是:

assert_equal expected, value

In vim, what is the most efficient way of going from the first line to the second?

在vim中,从第一行到第二行的最有效方法是什么?

6 个解决方案

#1


Via regex:

:s/\v([^, ]+)(\s*,\s*)([^, ]+)/\3\2\1/

If you do it often you can make a map out of it, e.g.:

如果你经常这样做,你可以用它制作一张地图,例如:

:nmap <F5> :s/\v([^, ]+)(\s*,\s*)([^, ]+)/\3\2\1/<CR>

Put the cursor on the line you want to flip and hit F5.

将光标放在要翻转的行上,然后按F5。

#2


This one swaps the word your cursor is on with the next one - just press F9 in command mode:

这个将光标所在的单词与下一个单词交换 - 只需在命令模式下按F9:

:map <F9> "qdiwdwep"qp
  • "qdiw: Put the word your cursor is on into buffer 'q'
  • “qdiw:将光标所在的字放入缓冲区'q'

  • dw: Delete all chars to the beginning of the next word (possibly comma + space)
  • dw:删除所有字符到下一个单词的开头(可能是逗号+空格)

  • e: Go to end of word
  • e:转到结尾

  • p: Paste (comma + space)
  • p:粘贴(逗号+空格)

  • "qp: Paste buffer 'q' (the first word)
  • “qp:粘贴缓冲区'q'(第一个字)

#3


Map a key combination to perform the command:

映射组合键以执行命令:

:s/^assert_equal \(.*\), \(.*\)$/assert_equal \2, \1

#4


I've always liked regular expression search and replace for these type of tasks:

我一直喜欢正则表达式搜索和替换这些类型的任务:

:s/\(\w*\), \(\w*\)/\2, \1/

Will swap the first word with second in a comma separated list.

将逗号分隔列表中的第一个单词与第二个单词交换。

#5


Hum... I would say "tdwxx$i, ^["tp but that's not really efficient or easy, just quick enough to type...

嗯...我会说“tdwxx $ i,^ [”tp但这不是真的有效或简单,只需快速打字......

#6


for something this simple, i would just make a little macro

对于这个简单的事情,我会做一个小宏

qadf ea, ^[pxxq

then @a away

然后@a离开

#1


Via regex:

:s/\v([^, ]+)(\s*,\s*)([^, ]+)/\3\2\1/

If you do it often you can make a map out of it, e.g.:

如果你经常这样做,你可以用它制作一张地图,例如:

:nmap <F5> :s/\v([^, ]+)(\s*,\s*)([^, ]+)/\3\2\1/<CR>

Put the cursor on the line you want to flip and hit F5.

将光标放在要翻转的行上,然后按F5。

#2


This one swaps the word your cursor is on with the next one - just press F9 in command mode:

这个将光标所在的单词与下一个单词交换 - 只需在命令模式下按F9:

:map <F9> "qdiwdwep"qp
  • "qdiw: Put the word your cursor is on into buffer 'q'
  • “qdiw:将光标所在的字放入缓冲区'q'

  • dw: Delete all chars to the beginning of the next word (possibly comma + space)
  • dw:删除所有字符到下一个单词的开头(可能是逗号+空格)

  • e: Go to end of word
  • e:转到结尾

  • p: Paste (comma + space)
  • p:粘贴(逗号+空格)

  • "qp: Paste buffer 'q' (the first word)
  • “qp:粘贴缓冲区'q'(第一个字)

#3


Map a key combination to perform the command:

映射组合键以执行命令:

:s/^assert_equal \(.*\), \(.*\)$/assert_equal \2, \1

#4


I've always liked regular expression search and replace for these type of tasks:

我一直喜欢正则表达式搜索和替换这些类型的任务:

:s/\(\w*\), \(\w*\)/\2, \1/

Will swap the first word with second in a comma separated list.

将逗号分隔列表中的第一个单词与第二个单词交换。

#5


Hum... I would say "tdwxx$i, ^["tp but that's not really efficient or easy, just quick enough to type...

嗯...我会说“tdwxx $ i,^ [”tp但这不是真的有效或简单,只需快速打字......

#6


for something this simple, i would just make a little macro

对于这个简单的事情,我会做一个小宏

qadf ea, ^[pxxq

then @a away

然后@a离开