VIM - 疯狂的命令来替换/重复行

标签 vim replace duplicates preg-replace

我有一个巨大的文件,需要更新以创建列表(列出 csv),但我只有 1 列。

我想使用键/值对组合创建 2 列。

我有例如:

Key 1 is NICE
Key 2 is good
Key 3 is Awesome

我需要:

key 1 is nice|Key 1 is NICE
key_2_is_good|key 2 is good
key_3_is_awesome|Key 3 is Awesome

因此第一个将全部小写,并用_代替空格,第二个是普通字符串

到目前为止我已经:

:%s/^\(.*\)$/\1|\1/

这很好,但如何将空格修复为 _ 并全部小写?

谢谢

最佳答案

宏比替换更直观:

qq                                      " start recording in register q then…
0                                       " go to the first column then…
y$                                      " yank from here to end of line then…
A|                                      " append a pipe at the end of the line then…
<C-r>=substitute(@", " ", "_", "g")<CR> " insert text of first column with spaces replaced by underscores then…
<Esc>                                   " get out of insert mode then…
q                                       " stop recording

之后,选择到最后一行并执行

:'<,'>norm @q

关于VIM - 疯狂的命令来替换/重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19257358/

相关文章:

php - 使用 Vim 在 PHP 脚本中突出显示真正的 SQL 语法

windows - 我如何从正在运行的 Gvim 重新加载 quickfix 窗口?

regex - 使用正则表达式,组捕获和反向引用查找和替换

Python json 解析器允许重复键

javascript - 如何从二维数组中删除重复项?

vim - 如何在猛拉的逐行视觉选择结束时自动删除换行符?

vim - 使用 vimscript 注释掉代码

java - Java中将所有字符大写并在每个字符之间添加空格的递归方法

python - 在 Python 中执行多个连续的正则表达式替换。效率低下?

dictionary - terraform `map` 重复键应该引发​​错误时的替代方案