Here's an il
operator that behaves like iw
but selects a line, from first to last non-blank character in the line:
function! SelectLine(count) abort
normal! gv
if visualmode() !=# 'v'
normal! v
endif
let startpos = getpos("'<")
let endpos = getpos("'>")
if startpos == endpos
execute "normal! ^o".a:count."g_"
return
endif
let curpos = getpos('.')
if curpos == endpos
normal! g_
let curpos = getpos('.')
if curpos == endpos
execute "normal!" (a:count+1)."g_"
elseif a:count > 1
execute "normal!" a:count."g_"
endif
else
normal! ^
let curpos = getpos('.')
if curpos == startpos
execute "normal!" a:count."-"
elseif a:count > 1
execute "normal!" (a:count-1)."-"
endif
endif
endfunction
xnoremap <silent> il :<C-U>call SelectLine(v:count1)<CR>
onoremap <silent> il :<C-U>execute "normal! ^v".v:count1."g_"<CR>
Supports usage in visual mode (e.g.vil
) or in operator mode (yil
to yank the line in characterwise mode.)
Supports usage with a count (v3il
or y3il
.)
In visual mode, it supports adding to the visual selection with further il
s adding more lines of text. Works both forward (when the cursor is at the end of the visual selection) or backward (when the cursor is at the start.)
I don't know that this operator is really that useful, considering in many cases you could just as well use a linewise operator or linewise visual mode instead... But I found myself wanting to yank a line in characterwise mode before, and this does it, so here it is if you find it useful.