gpt4 book ai didi

How to multiline replace different characters in vim(如何在VIM中用多行替换不同的字符)

转载 作者:bug小助手 更新时间:2023-10-28 10:23:25 24 4
gpt4 key购买 nike



i have this code snippet. I want to replace all the empty spaces with one space.
In IntelliJ i would simply multi-cursor, jump to the -- use option + shift + left arrow then right arrow to select all the spaces and type a space which replaces them.

我有这个代码片段。我想把所有的空格换成一个空格。在IntelliJ中,我会简单地多光标,跳到--Use Option+Shift+向左箭头,然后向右箭头选择所有空格,并键入一个空格来取代它们。


I'm new to vim and haven't found any solution that achieves this.

我是vim的新手,还没有找到任何解决方案来实现这一点。


vim.opt.showmode = false                      -- we don't need to see things like -- INSERT -- anymore
vim.opt.showtabline = 2 -- always show tabs
vim.opt.smartcase = true -- smart case
vim.opt.splitbelow = true -- force all horizontal splits to go below current window
vim.opt.splitright = true -- force all vertical splits to go to the right of current window
vim.opt.swapfile = false -- creates a swapfile
vim.opt.termguicolors = true -- set term gui colors (most terminals support this)
vim.opt.timeoutlen = 1000 -- time to wait for a mapped sequence to complete (in milliseconds)
vim.opt.undofile = true -- enable persistent undo
vim.opt.writebackup = false -- if a file is being edited by another program (or was written to file while editing with another program), it

Thanks.

谢谢。


更多回答
优秀答案推荐

In my semi-fresh install of Intellij IDEA CE, the correct sequence would be:

在我半新安装的IntelliJ Idea CE中,正确的顺序应该是:


<place cursor>
<option><option>
<down>
<down>
<down>
<down>
<down>
<down>
<down>
<down>
<down>
<option-shift-left>
<(option-shift-)right>
<space>
<escape>

In Vim, you could simply use a multi-cursor plugin, there are a few, or do it…

在Vim中,你可以简单地使用一个多光标插件,有几个,或者这样做。


With a substitution:

替换为:


vip
:'<,'>s/ \{2,}/ /<CR>

where…

…在哪里?



  • vip visually select the current "paragraph", see :help v and :help text-objects.

  • '<,'> is the :help :range of lines on which to execute the following command. You can indicate a range explicitly or implicitly, by selecting the range of lines before entering command-line mode. This is what we did, here, and Vim inserted the corresponding range automatically: '<,'>.

  • s/ \{2,}/ / is the substitution

  • \{2,} matches 2 consecutive spaces or more.

  • is, well… a single space.

  • And <CR> is the Enter key.


With the "dot formula":

使用“点公式”:


/ \{2,}<CR>
cgn<space>
.
.
.
.
.
.
.
.
.

where…

哪里...



  • / \{2,}<CR> searches for the next sequence of 2 or more spaces. In this case, we are just setting the stage for the next commands.

  • cgn<space> replaces the current or next search match with a <space>, see :help c and :help gn.

  • The nine . repeat the previous command nine times, see :help ..


FWIW, I have a custom mapping that makes it possible to use a count before .:

FWIW,我有一个自定义映射,可以在之前使用计数。:


nnoremap . :<C-u>execute "norm! " . repeat(".", v:count1)<CR>

which shortens the command above quite a bit at the cost of counting the lines so YMMV:

这大大缩短了上面的命令,但代价是计算行数,因此YMMV:


/ \{2,}<CR>
cgn<space>
9.

Philosophical considerations:

哲学思考:



  • In a way, the "dot formula" is the opposite of multi-cursors: in one case you place the cursor n times and then perform the edit, while in the other you perform the edit and repeat it n times.

  • Competent vimmers use search a lot for navigation and regular expressions tend to become a second nature. It feels weird at first to be so literal and deliberate about your edits, especially after decades of meaningless <down>s and <left>s, but it eventually grows on you.

  • I would naturally go for the substitution.


更多回答

I would've gone for the more sloppy :s/ \+/ /g which will replace one or more spaces with one space and that multiple times per line. Technically, it has the side effect of replacing all single spaces with a single space each but it's faster to type.

我会选择更草率的:S/+//g,它将用一个空格替换一个或多个空格,并且每行多次使用该空格。从技术上讲,它的副作用是将所有单个空格都替换为一个空格,但它的打字速度更快。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com