Pressing W (as in Shift+w) when there is only one word in front of the cursor causes the cursor to immediately jump to the next line. This is unlike what happens when doing the reverse with B, where the cursor stops at the beginning of the line before continuing to the previous line.

Is there some way to configure W to act more like B, in that it stops when it reaches the end of the line, then jumps to the next if pressed again?

  • 1
    Anything wrong with using E? – Tumbler41 Jun 12 '17 at 19:36
  • @Tumbler41 Huh. I guess not, thanks! It'll just take some time for me learn to use E by default.. – gandalf3 Jun 12 '17 at 19:38

As Tumbler41 pointed out in the comments, the simpler solution would be to use E instead of W. However, if you would like to configure W to behave this way, you could do this:

nnoremap <expr> W getline('.')[col('.') - 1:] =~# '\s\S' ? 'W' : (col('.') + 1 == col('$') ? 'W' : '$')

Unfortunately, this solution is longer thank I'd like, and somewhat hacky, but it works. Here's a quick explanation of how it works:

"Use an 'expression mapping' on the key W. This means that we will evalue the
"mapping as vimscript code to determine what keystrokes to press
nnoremap <expr> W
  "Get everything after the cursors position
  getline('.')[col('.') - 1:]
  "Does it contain a whitespace character followed by a non-whitespace character?
  =~# '\s\S'
  "If it does, return 'W'
  ? 'W'
    "Are we on the last character?
    (col('.') + 1 == col('$')
    "If we are. return 'W'
    ? 'W'
      "Otherwise, return '$'
      : '$')
| improve this answer | |

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.