简体   繁体   English

如何在Vim中“更改数字”或“更改数字”

[英]How can I 'change in number' or 'change in digits' in Vim

I'm regularly editing CSS in Vim and need to change a value like: 我定期在Vim中编辑CSS,需要更改一个值,例如:

width: 300px;

to something else like: 到其他类似的东西:

width: 178px;

Normally what I do is navigate to the 3 in 300 and type cw to change the word. 通常我要做的是导航到300中的3,然后键入cw更改单词。 But if I do this I have to type the px bit again. 但是,如果执行此操作,则必须再次输入px位。 Is there a short and quick way to say change in digits . 有没有一种简短快捷的方式来说明change in digits

In my searching I've discovered Ctrl-a and Ctrl-x to increment and decrement numbers in this situation. 在搜索中,我发现Ctrl-a和Ctrl-x可以在这种情况下递增和递减数字。 Is there an equivalent for being able to retype the number? 是否可以重新输入数字?

I should clarify that the ideal solution would require the fewest keystrokes at time of operation and support modifications to different length numbers. 我应该澄清,理想的解决方案在操作时需要最少的击键,并支持对不同长度的数字进行修改。 Like if I wanted to change 300px to 17px. 就像我想将300px更改为17px一样。

I don't know of a "change in digits" command, but you can use t , which is like f but goes to the character before what you are searching for; 我不知道“数字更改”命令,但是您可以使用t ,就像f一样,但是要在要搜索的字符之前输入; that is, if you type tp , it will move the cursor to the character just before "p" in your line. 也就是说,如果键入tp ,它将把光标移动到行中“ p”之前的字符。 Combining it with c , if your cursor is on the 3, ctp178 Esc will replace 300 with 178. 将其与c结合使用,如果光标位于3上,则ctp178 Esc将178替换为300。

Alternatively, you can use 3s to substitute the next three characters: 3s178 Esc . 另外,您可以使用3s代替接下来的三个字符: 3s178 Esc This is easy when the string being replaced is easy to scan for length. 当要替换的字符串易于扫描长度时,这很容易。 And if your replacement text is the same length as the text being replaced, you can just use R to replace: R178 Esc . 如果替换文本的长度与被替换文本的长度相同,则可以使用R替换: R178 Esc

Here is in , a custom text-object that lets you act on numerical values (including floats): in ,这是一个自定义文本对象,可让您对数值(包括浮点数)进行操作:

" custom text-object for numerical values
function! Numbers()
    call search('\d\([^0-9\.]\|$\)', 'cW')
    normal v
    call search('\(^\|[^0-9\.]\d\)', 'becW')
endfunction
xnoremap in :<C-u>call Numbers()<CR>
onoremap in :normal vin<CR>

The actual search is performed in a function to avoid clobbering the search register and search highlighting. 实际搜索是在一项功能中执行的,以避免破坏搜索寄存器和搜索突出显示。

You can add that text-object to your vimrc "as is" and use it like other text-objects: 您可以将该文本对象“按原样”添加到您的vimrc ,并像其他文本对象一样使用它:

vin
cin
yin
din

演示

--- edit --- -编辑-

This new version: 新版本:

  • doesn't mess with search highlighting (good), 不会混淆搜索突出显示(很好),
  • doesn't mess with the search register (good), 不会弄乱搜索寄存器(好),
  • respects BOL and EOL (good), 尊重BOL和EOL(良好),
  • works with floats (good). 适用于浮点数(良好)。

Comments welcome. 欢迎发表评论。

--- edit --- -编辑-

You can use the text object proposed by romainl , or, alternatively, rely on existing ones: 您可以使用romainl提出的文本对象,或者使用现有的对象:

If you just want to change the 300 to 178 on that line do: 如果您只想将该行上的300更改为178,请执行以下操作:

:s/300/178/ RETURN :s/300/178/返回

It will do a find->replace on the current line 它将在当前行上执行find-> replace

To do it globally, do: 要在全球范围内进行,请执行以下操作:

:%s/300/178/g RETURN :%s/300/178/g返回

My favorite way is to use visual mode, so I would navigate to the 3, then use either 2 spaces (or any other motion command) to navigate to the end of the part I wanted to change. 我最喜欢的方式是使用可视模式,因此我将导航至3,然后使用2个空格(或任何其他运动命令)导航至要更改的零件的末尾。 Once I have highlighted the part I want to change, just type s178 to substitute the chars "178" for whatever is highlighted. 突出显示要更改的部分后,只需键入s178即可将字符“ 178”替换为突出显示的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM