Reputation: 11532
How can I move forward/backward number of characters past end of line in VIM?
I known I can type, for example,
25l
and go forwards 25 characters, but this command will always stop at the end of a line. Also, there is 25go
, but this goes forward from the beginning of the buffer, not forward from the current cursor position. I want to go forwards a certain number of characters including end of line characters.
Upvotes: 14
Views: 4617
Reputation: 5957
I think what you are looking for is space to move forwards and backspace to move backwards.
space will continue in the following line. If you want to add spaces in the current line instead of moving to the next one then the :set virtualedit=onemore
is the option for you.
Upvotes: 13
Reputation: 172590
Another possibility (that doesn't require changing options, but is more verbose) is by using the search()
function. The following will move the cursor to the right by 6
characters. It does this by matching from the current position (\%#
) 7 characters including newlines (\_.
):
:call search('\%#\_.\{7}', 'ce')
Upvotes: 1
Reputation: 172590
The 'whichwrap'
option determines what motions can move the cursor to another line. By default, none of the left / right movements do that.
The inclusion of h,l
is not recommended, as some macros and plugins may depend on the original behavior and break - your call to test and decide. But it should be safe to include the ← and → cursor keys via (the latter pair is for insert mode and optional)
:set whichwrap+=<,>,[,]
Then, you can move by 5 characters across the line ending via 5 →.
Whether the newline character is counted or not depends on the 'virtualedit'
option. To include the newline:
:set virtualedit=onemore
Upvotes: 3
Reputation: 161684
You can set virtualedit
option:
:set ve=all
Virtual editing means that the cursor can be positioned where there is no actual character.
Upvotes: 5