We just launched a new product: Collectives on Stack Overflow. How do they work? Listen to learn more.
13

Based on the help file, and the Vim Wikia page on Search and replace :%s/[0-9]\+\.[0-9]\+\|[0-9]\+/0/g :%s - start a command with the range of the entire file, %, to substitute, the following patterns: [0-9]\+\.[0-9]\+ This must come first, it's the pattern that covers decimals like 1.7. In addition it will only match a number followed by a period, ...


12

Another simple way to accomplish this is to use the g CTRL-A command in Visual mode, which increments all numbers by successively higher counts on each line that starts with a number. (See :help v_g_CTRL-A.) In order to be able to use it, start by replacing all numbers with zeroes: :%s/^\d\+\./0./ Then visually select everything: ggVG And finally ...


10

If the number starts with a 0 it's interpreted as an octal number (base-8, instead of base-10). Vim also recognises hexadecimal numbers (base-16) if it starts with 0x and binary numbers (base-2) if it starts with 0b. You can use the 'nrformats' setting to control which numbers Vim recognises; IMHO a value of bin,hex makes the most sense, as it's not easy to ...


8

While I'd definitely go with :s + printf for complex replacements, I can get the effect you desire if I start from 00, and have set nrformats-=octal. That is: Select the numbers in a visual block: Note that I have added mov76.webm - you don't actually have ten files in your example list. Replace with zeros and select the same region again: r0gv Use g<c-...


6

This can be done in two relatively simple steps: Decrement the lines with ctrl-x Run a substituion on the changed lines to add the leading zeros: '[,']s/\d\@<!\d\>/0\0/ You could turn that into a command/function if you think you'll need to do this often. Another way is to do as muru mentioned and use substitute + printf, which can preserve the ...


6

Try this: let c=0|g/^\d\+\ze\./let c+=1|s//\=c ├─────┘ ├───────────┘├──────┘ ├─┘├─┘ │ │ │ │ └ with the current value of the counter (i.e. new number) │ │ │ └ replace last used pattern (i.e. old number) │ │ └ for every matched line, increment the counter │ └ iterate over the lines ...


2

Here's a couple of different (but related) methods, both of which perform the edits using regular normal mode editing commands that yank each line's number and then paste and increment it on the next line: Using :global and :normal First, populate the unnamed register with a 0 by typing i0Escx. Then run the following command: :g/^\d/norm!viwp^Ayiw ...


2

Do you mind some Awk? :%!awk 'gsub(/[0-9]+/,i+0){i++}1' This means: gsub(/[0-9]+/,i+0) If there is any number in the line, substitute it by i+0. {i++} If a substitution occurred in this line, increment i. 1 is a short for "print the resulting line". In case you are wondering, i+0 forces the conversion of i to integer in the case i is unset (case ...


1

As I am still relatively new to vim, if there is a better alternative or if there are improvements to be made, please do let me know. Setup The solution I ended up with makes use of a regular expression that is saved to a separate regex.vim file, which is sourced in init.vim. The full explanation can be found here: Save commonly used regex patterns in Vim?...


1

This substitution should do it: :%s/0x[0-9a-fA-F]*/\=printf('%08X', str2nr(submatch(0), 16))/g The regex 0x[0-9a-fA-F]* is straightforward, matching hex numbers as you describe. The replacement string uses an expression, since it starts with \=. The expression is printf('%08X', str2nr(expand(submatch(0)), 16)), which starts by taking the matched text ...


Only top voted, non community-wiki answers of a minimum length are eligible