Is there a way to get whole movements of objects in vim?

I often work on CSS files or other files that require hiding numbers. I would really like to have a key that refers to integers in the sense that w refers to a word, or ( refers to a sentence. For example, in a css file, I could go to the beginning of the height declaration and change its value without retyping "px "at the end, which happens if I use w .

Is there such a thing, but I miss it in the documentation, or is there a way to add this function to my .vimrc?

Bonus points if there is a way to use it as ci" , where I could be at the beginning of the line and use the" change internal "command to go to AND change the next integer.

More bonus points if I could do simple arithmetic. I would love to get a short command that would be short for "Add also, inner, integer, 5" and have the next integer in the current line - five terrays, then it was when I started.

Edit:

Some really great suggestions for everyone, great ideas that will surely improve my work. Thank you Hussek's answer is likely to end up in my workflow, but none of the others seem to have answered (completely) my internal question: a movement that works on integers. The sentence traced by romainl seems to have this purpose , but I cannot get it to work reliably .

For myself (and possibly others) I will clarify my wishes below:

A key that acts mainly like w acts for words, but on integers, so I can just add it to my mental vim vocabulary and use it without problems. This includes the following scenarios. (I use d as my sample key):

  • Normal mode d : move to the next integer
  • Normal cd mode: go to the end of the integer under the cursor (note that cw is a special case that MUST change to the word NEXT. cw acts like ce .) I would expect this special case to be implemented with integers as well.
  • Visual id mode: select the integers [count].
  • Visual ad mode: select the integers [count]. Running or trailing space is included.

Is there any lack of behavior that w can expect from a new movement? Is there even a key that is available in both normal and visual modes?

+6
source share
3 answers

See this sentence . It looks good.

change

Indeed, this is pretty good. This made me think that my habit of doing /<number><Esc> was not very effective, so I added these mappings (and slightly changed the mappings above for consistency) to my ~/.vimrc . See if they are useful in the long run:

 nnoremap è /\v\d+<CR> nnoremap é ?\v\d+<CR> 

At first glance, èciè132<Esc> seems a little better than /2<Esc>{count}s132<Esc> in terms of keystrokes, but much better if it allows me to skip a. checking the first digit of the value I want to change, and b. counting replaceable characters.

Time will tell.

re-editing

Here is the function and its display:

 onoremap N :<cu>call <SID>NumberTextObject(0)<cr> xnoremap N :<cu>call <SID>NumberTextObject(0)<cr> onoremap aN :<cu>call <SID>NumberTextObject(1)<cr> xnoremap aN :<cu>call <SID>NumberTextObject(1)<cr> onoremap iN :<cu>call <SID>NumberTextObject(1)<cr> xnoremap iN :<cu>call <SID>NumberTextObject(1)<cr> function! s:NumberTextObject(whole) normal! v while getline('.')[col('.')] =~# '\v[0-9]' normal! l endwhile if a:whole normal! o while col('.') > 1 && getline('.')[col('.') - 2] =~# '\v[0-9]' normal! h endwhile endif endfunction 

With this I can:

  • vcdy part of the number from the cursor to the end with <command>N Somehow similar to <command>e or <command>w .

     Here are some random numbers: 24 7635 1000018 ^---> 

    This does not work if the cursor is no longer on the number and it does not go back.

  • vcdy an integer with <command>iN .

     Here are some random numbers: 24 7635 1000018 <-^---> 

    Again, this does not work if the cursor is not already on the number.

Everything can be improved, of course, but this is the beginning!

Endedit

I also work a lot with CSS.

I use two strategies for changing numerical values:

  • {count}<Ca> and {count}<Cx> , as in Hussek's answer, when I know how much I want to increase / decrease the number. Let's say I want to turn 20px into 25px , a simple 5<Ca> does the trick without requiring me to move the cursor to a number. This is extremely cool .

  • /<number><CR>{count}s<new number> when the new value is very different from the current value, and I feel lazy to calculate the delta. /2<CR>2s67<Esc> will allow me to change 23px to 67px . /2<CR>R67<Esc> is another way, but it is only good if the new value has the same length as the current value. Use f<number> if you are on the same line.

Please note that you can insert the result of expressions using <Cr>=137-42<CR> , which I use very often as well.

+4
source

You can add or subtract from integers using the following commands:

 <num>Ctrl-a (to add) <num>Ctrl-x (to substract) 

and he will go to the next number in the line and execute the command

+8
source

I found something deep in the Internet here and changed it as follows

 nnoremap ,n /\v\d+/b<cr>mah/\v\d+/e<cr>mb`av`b vnoremap ,n <esc>/\v\d+/b<cr>mah/\v\d+/e<cr>mb`av`b 
  • \v\d+ searches for a group of numbers
  • /b + mah goes into b eging, sets a mark and returns a single character so that we can again
  • \v\d+ find the same group of digits
  • /e + mb comes to an end and sets another label and finally
  • `av`b visually selects the first to second label
+3
source

Source: https://habr.com/ru/post/919634/


All Articles