vim - Toggle semicolon (or other character) at end of line -
appending (or removing) semicolon end of line common operation. yet commands a;
modify current cursor position, not ideal.
is there straightforward way map command (e.g. ;;
) toggle whether semicolon appears @ end of line?
i'm using command in vimrc append:
map ;; a;<esc>
something work
nnoremap ;; :s/\v(.)$/\=submatch(1)==';' ? '' : submatch(1).';'<cr>
this uses substitute command , checks see if last character semicolon , if removes it. if isn't append character matched. uses \=
in replacement part execute vim expression.
if wan't match arbitrary character wrap in function , pass in character wanted match.
function! toggleendchar(chartomatch) s/\v(.)$/\=submatch(1)==a:chartomatch ? '' : submatch(1).a:chartomatch endfunction
and mapping toggle semicolon.
nnoremap ;; :call toggleendchar(';')<cr>
Comments
Post a Comment