I hope someone can riddle me this one: I have set up the function below to have a slightly more fancy behavior of the End key, moving first to the end of the line, but if the cursor is already at the end of the line, moving to the end of the next line. This works just fine:
inoremap <silent> <End> <c-\><c-n>@=<SID>ExtEnd()<cr>a
nnoremap <silent> <End> @=<SID>ExtEnd()<cr>
vnoremap <silent> <End> @=<SID>ExtEnd()<cr>
function! s:ExtEnd()
if strlen(getline('.')) == 0 | return 'j'
elseif strlen(getline('.')) == col('.') | return 'j'
else | return '$'
endif
endfunction
Here is my issue: I wanted to make this even more fancy by checking if the cursor is in front of the first non-blank character on the line. If that is the case, I would have it move to the first non-blank character on the first press of End, then to the end of the line on the next, and finally to the end of the next line on the third. To achieve this, I need to figure out the position of the first non-blank character on the line. I didn't find a function to do that directly, so I thought of a slightly kludgy solution of saving my cursor position (spos
), jumping to the first non-blank character, saving that position (fpos
), and then jumping back to the original saved position. The resulting code looks like so (this isn't finished yet, the actual cursor motions defined within the if
-block are unchanged at this point):
function! s:ExtEnd()
let spos=getpos(".")
normal! ^
let fpos=getpos(".")
call setpos(".", spos)
if strlen(getline('.')) == 0 | return 'j'
elseif strlen(getline('.')) == col('.') | return 'j'
else | return '$'
endif
endfunction
In theory (well, my theory anyway) the additional four lines
let spos=getpos(".")
normal! ^
let fpos=getpos(".")
call setpos(".", spos)
should have no effect, but they do: Now my cursor goes to the end of the line on the first press of End, but then it will jump to the first non-blank character on the next line, which doesn't seem to make any sense at all to me. Even if those four lines should change the cursor position (which they shouldn't) that should not result in the downward motion of the cursor. It's almost like the code is not executed in the sequence given.
What is going wrong here?