Vim 如何移动 csv 列

Vim how to move csv column

假设像这样的经典 csv :

1,2,3,4,5,6,7
1,2,3,4,5,6,7
1,2,3,4,5,6,7

我试图使用简单而优雅的一行将每行的第 6 列移动到行的开头。

有没有办法使用像这样的东西来实现这个:

g/,/norm 5n<?>d0P

我不知道用什么代替 到 select 第 5 个逗号后面的单词

全局命令稍作修改

:g/./exec 'normal 5f,lvld0P'

:g  .............. globally
/./  ............. on each line that has something
exec ............. execute the following normal command
5f, .............. jumpt to the fifht ,
l ................ move to the number
vl ............... select the number and its coma
d ................ cut to the unamed register
0 ................ jump to the beginning of line
P ................ past the content of default register (before)

更新

而不是简单地 select 计算数字和下一个昏迷,我们 select 直到下一个昏迷。 (这是一个更通用的解决方案),避免了可能具有两位数或更多数字的列的问题。

g/./exec 'normal 5f,ldf,0P'

使用 gnu awk

awk -i inplace -F, -v OFS="," '{print 6,1,2,3,4,5,7}' target-file

-i inplace  ............. no need to use temp file
-F ...................... field separator
-v OFS .................. output field separator

从 vim

调用 awk
:%! awk -F, -v OFS="," '{print 6,1,2,3,4,5,7}'

% ............... current file
! ............... filter trhough external command