Skip to content

Instantly share code, notes, and snippets.

@deathlyfrantic
Created January 31, 2017 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deathlyfrantic/fc513b59364a0dabb19448a34308cfd9 to your computer and use it in GitHub Desktop.
Save deathlyfrantic/fc513b59364a0dabb19448a34308cfd9 to your computer and use it in GitHub Desktop.
check/uncheck [x] boxes with the mouse in vim.
" this whole thing is super brittle, but was a fun experiment
function! s:string_replace(string, new_char, position)
let l:string_list = [strpart(a:string, 0, a:position)]
let l:string_list = add(l:string_list, a:new_char)
let l:string_list = add(l:string_list, strpart(a:string, (a:position + 1)))
return join(l:string_list, '')
endfunction
function! CheckThatBox()
let l:colnum = col('.') - 1
let l:lnum = line('.')
let l:line = getline(l:lnum)
let l:char = l:line[l:colnum]
let l:x = -1
if l:char == '[' && l:line[l:colnum + 2] == ']'
let l:x = l:colnum + 1
elseif l:char == ']' && l:line[l:colnum - 2] == '['
let l:x = l:colnum - 1
elseif l:line[l:colnum - 1] == '[' && l:line[l:colnum + 1] == ']'
let l:x = l:colnum
endif
if l:x > -1
let l:repl = (l:line[l:x] =~? 'x') ? ' ' : 'X'
let l:line = s:string_replace(l:line, l:repl, l:x)
call setline(l:lnum, l:line)
endif
endfunction
nnoremap <silent> <LeftMouse> <LeftMouse>:call CheckThatBox()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment