3

Sometimes I need to move to a particular byte offset or move to the nth unicode rune in a file (utf-8).

Does vim expose a way to move to an absolute byte/char offset or to move the cursor forward a backward a given number of bytes/chars?

5

To go to the Nth byte in the file: use :go N, :Ngo, or Ngo (normal mode).

To move N bytes ahead, you could use something like this :exe 'go' line2byte(line("."))+col(".")-1+N

Unless you have changed virtualedit, you can move forward with <space>. To go to the Nth character in the file, use 1goN<space> in normal mode.

To move ahead N characters, use N<space> in normal mode.

Note: besides the obvious multi-byte character difference these also differ in that the first counts newlines and the second doesn't.

If you have set virtualedit=all, space won't work. Instead you could use /\%#\_.\{N}/e for relative or /\%1l\%1c\_.\{4}/e for absolute. Unlike the <space> version this counts end-of-lines.

  • I have set virtualedit=all active, which kind of messes with the operation of space. I'm looking for something that moves forward a certain number of characters (or bytes, as apporpriate) regardless of the value of any options. – Gregory Nisbet Dec 6 '17 at 0:02
  • See also superuser.com/questions/767504/… – Mass Dec 6 '17 at 0:29
  • +1: I was getting an error from a stupid parser that told me the input was malformed at "character number 1954," as if that's supposed to be helpful. This answer saved me! – Warren Young Jul 28 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.