How to prevent vim from classifying blank lines as words

Viewed 61

In vim, I have just noticed that w and b now catch on blank lines. I have also seen that this is mentioned in the official vim docs with the statement "An empty line is also considered to be a word."

I would like to have empty lines not be considered words. That is, given the text

foo

bar

with the cursor positioned on the last "o" in "foo", I would like to be able to press w once and have the cursor go to the "b" in "bar", and not the blank line in between.

I have attempted to modify iskeyword, but seeing as the newline (ascii char 10) is not included in any of the default values of iskeyword, it is not surprising that this does not produce the desired change.

I suspect that the solution will come from modifying what defines a "word", but apart from modifying iskeyword, I have not been able to find information on how to do this. How do I make this change?

1 Answers

One solution is to remap w, b, W, B to a custom function, just in the case that you really want to skip the blank lines but get no simple way to approach it.

nnoremap <silent> w :call MovePastBlank('w')<cr>
nnoremap <silent> b :call MovePastBlank('b')<cr>
nnoremap <silent> W :call MovePastBlank('W')<cr>
nnoremap <silent> B :call MovePastBlank('B')<cr>

function! MovePastBlank(motion)
  execute 'normal! '.a:motion
  let prev_pos = getpos('.')

  while expand('<cword>') == ''
    execute 'normal! '.a:motion

    let current_pos = getpos('.')
    if current_pos == prev_pos
      return 
    else
      let prev_pos = current_pos
    endif
  endwhile
endfunction
Related