6

In normal mode, b and w are jumping between words,

and I would like to stop at the end of line.

For example:

The cursor is right over here

enter image description here

When I press w, what I expect is

enter image description here

Instead, the cursor jump to beginning of next line.

Should I set iskeyword or something? Thanks.

2 Answers 2

7

Use e key - it moves cursor to the end of a word-object. See :h word-motions for more movement keys.

4
  • Is there a way just make 'w' stop at the end of the line? 'e' does not stop in an empty line.
    – Green Bird
    Dec 13, 2016 at 7:29
  • 4
    That would probably require custom non-trivial function. Personally I think that it's better to learn how to use motions as they are, because later you will forget that you have custom motion mapping and something will relay on it and it won't work just for you - and it's really hard to change habits. But maybe you're looking for $? This will jump to the end of current line.
    – grodzik
    Dec 13, 2016 at 8:09
  • I know e and $, but I would like to use w all the way.
    – Green Bird
    Dec 13, 2016 at 11:23
  • 1
    Then @statox's answer is what you need.
    – grodzik
    Dec 13, 2016 at 11:37
4

While I deeply agree with @grodzik comment (it's better to learn how to use motions as they are, because later you will forget that you have custom motion mapping) here is a custom funciton and mapping to do what you want:

" Override w motion
function! MyWMotion()
    " Save the initial position
    let initialLine=line('.')

    " Execute the builtin word motion and get the new position
    normal! w
    let newLine=line('.')

    " If the line as changed go back to the previous line
    if initialLine != newLine
        normal k$
    endif
endfunction

" Override b motion
function! MyBMotion()
    " Save the initial position
    let initialLine=line('.')

    " Execute the builtin word motion and get the new position
    normal! b
    let newLine=line('.')

    " If the line as changed go back to the previous line
    if initialLine != newLine
        normal j^
    endif
endfunction

nnoremap <silent> w :call MyWMotion()<CR>
nnoremap <silent> b :call MyBMotion()<CR>

I haven't tested it extensively so maybe some corner cases will create problems but that should be a good start.

Also as I mentioned before this code is only to show that it is possible to do that I wouldn't recommend to use it.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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