如何删除 Vim 中前两列周围的引号? [英] How to remove quotes surrounding the first two columns in Vim?

查看:69
本文介绍了如何删除 Vim 中前两列周围的引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在文本文件中有以下样式的行:

Say I have the following style of lines in a text file:

"12" "34" "some text     "
"56" "78" "some more text"
.
.
.
etc.

我希望能够删除前两列周围的引号.使用 Vim 执行此操作的最佳方法是什么(我目前正在使用 gVim)?

I want to be able to remove the quotes surrounding the first two columns. What is the best way to do this with Vim (I'm currently using gVim)?

我想出了如何使用可视模式至少删除每一行的开头引号,然后输入命令'<,'>s!^"!!

I figured out how to at least delete the beginning quote of each line by using visual mode and then enter the command '<,'>s!^"!!

我想知道是否有一种方法可以选择一整列文本(一个字符从文件中直接向下......或超过 1 个,但在这种情况下我只想要一个).如果可能,那么您是否可以将 x 命令(删除字符)应用于整列.

I'm wondering if there is a way to select an entire column of text (one character going straight down the file... or more than 1, but in this case I would only want one). If it is possible, then would you be able to apply the x command (delete the character) to the entire column.

可能有更好的方法来做到这一点.我正在寻找任何建议.

There could be better ways to do it. I'm looking for any suggestions.

仅供参考,我结合了一些建议.我的 _vimrc 文件现在包含以下行:

Just and FYI, I combined a couple of the suggestions. My _vimrc file now has the following line in it:

let @q=':%s/"\([0-9]*\)"/\1/g^M'   

(注意:^MCTRLQ + Enter 模拟运行后按回车键命令)

(Note: THE ^M is CTRLQ + Enter to emulate pressing the Enter key after running the command)

现在我可以通过 @q 使用宏删除文件中两个数字列的所有引号.

Now I can use a macro via @q to remove all of the quotes from both number columns in the file.

推荐答案

Control-V 用于块选择.这将使您可以在同一字符列中选择内容.

Control-V is used for block select. That would let you select things in the same character column.

您似乎想删除数字周围的引号.对于这种用途,

It seems like you want to remove the quotes around the numbers. For that use,

:%s/"\([0-9]*\)"/\1/g

这是一个您可以使用 vim 执行哪些模式的列表.

还有一种(有点难看)的形式,每行限制为 4 个替换.

There is one more (sort of ugly) form that will restrict to 4 replacements per line.

:%s/^\( *\)"\([ 0-9]*\)"\([ 0-9]*\)"\([ 0-9]*\)"/\1\2\3\4/g

<小时>

而且,如果您手边有 sed,您也可以从 shell 中尝试这些.


And, if you have sed handy, you can try these from the shell too.

head -4 filename.txt | sed 's/pattern/replacement/g'

这将在文件的前 4 行尝试您的命令.

that will try your command on the first 4 lines of the file.

这篇关于如何删除 Vim 中前两列周围的引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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