vim9script # source vimscript (operator) def SourceVim(...args: list): any if len(args) == 0 &opfunc = matchstr(expand(''), '[^. ]*\ze[') return 'g@' endif if getline(1) =~ '^vim9script$' vim9cmd :'[,']source else :'[,']source endif return '' enddef nnoremap v SourceVim() xnoremap v SourceVim() nnoremap vv SourceVim() .. '_' # calc visually selected math expression xnoremap c s \=system($'perl -e "print {@@->tr("\n", " ")}"')`[v`] # fuzzy import autoload 'fuzzy.vim' nnoremap e fuzzy.File() nnoremap fe fuzzy.FileTree() nnoremap ge fuzzy.GitFile() nnoremap b fuzzy.Buffer() nnoremap h fuzzy.Help() nnoremap fm fuzzy.MRU() nnoremap fi fuzzy.GitFile(fnamemodify($MYVIMRC, ":p:h")) nnoremap fd fuzzy.GitFile($DOCS ?? '~/docs') nnoremap fD fuzzy.File($DOCS ?? '~/docs') nnoremap fc fuzzy.Colorscheme() nnoremap ft fuzzy.Template() nnoremap fs fuzzy.Session() nnoremap fb fuzzy.Bookmark() nnoremap fT fuzzy.Filetype() nnoremap fh fuzzy.Highlight() nnoremap fR fuzzy.File($VIMRUNTIME) nnoremap ; fuzzy.CmdHistory() nnoremap fp fuzzy.Project() # enhance search, only if wildcharm is set to if &wildcharm == 26 cnoremap get({'/': "\", '?': "\"}, getcmdtype()) ?? "" cnoremap get({'/': "\", '?': "\"}, getcmdtype()) ?? "" endif # enhance search with as "whatever" # to enter literal use cnoremap getcmdtype() =~ '[/?]' ? '.\{-}' : "" # whitespace nnoremap FixTrailingSpaces # search&replace nnoremap % :%s/\<=expand("")\>/ xnoremap % y:%s/=escape(@", '^~$\&*.[]')//g # toggles nnoremap yow set wrap! wrap? nnoremap yos set spell! spell? nnoremap yod exe (&diff ? ':diffoff' : ':diffthis') nnoremap yov &ve = (&ve == "block" ? "all" : "block")set ve nnoremap yob exe "colo" get(g:, "colors_name") == "nod" ? "nope" : "nod" # move lines xnoremap :sil! m '>+1gv xnoremap :sil! m '<-2gv # In visual block { and } navigate to the first/last line of paragraph, # which is useful if followed by I or A. def VisualBlockPara(cmd: string) if mode() == "\" var target_row = getpos($"'{cmd}")[1] if getline(target_row) =~ "^\s*$" target_row += (cmd == "{" ? 1 : -1) if target_row == line('.') target_row = (cmd == "{" ? prevnonblank(target_row - 1) : nextnonblank(target_row + 1)) endif endif if target_row > 0 exe $":{target_row}" endif else exe $"normal! {cmd}" endif enddef xnoremap { VisualBlockPara("{") xnoremap } VisualBlockPara("}") # toggle colorcolumn at cursor position # set vartabstop accordingly def ToggleCC(all: bool = false) if all b:cc = &cc ?? get(b:, "cc", "80") &cc = empty(&cc) ? b:cc : "" else var col = virtcol('.') var cc = split(&cc, ",")->map((_, v) => str2nr(v)) if index(cc, col) == -1 exe "set cc=" .. cc->add(col)->sort('f')->map((_, v) => printf("%s", v))->join(',') else exe $"set cc-={col}" endif endif if !&expandtab | return | endif var cc = split(&cc, ",")->map((_, v) => str2nr(v)) if len(cc) > 1 || len(cc) == 1 && cc[0] < 60 setl vsts& var shift = 1 for v in cc if v == 1 | continue | endif exe $"set vsts+={v - shift}" shift = v endfor exe $"setl vsts+={&sw}" else setl vsts& endif enddef nnoremap yoC ToggleCC() nnoremap yoc ToggleCC(true) nnoremap text.Toggle() # print maybe-function name nnoremap [f echo getline(search('^[[:alpha:]$_]', 'bcnW')) # windows def ResizeWin(width: number, height: number) var w = max([width, winwidth(0)]) var h = max([height, winheight(0)]) execute 'vertical resize' w execute 'resize' h try setlocal winfixwidth winfixheight wincmd = finally setlocal nowinfixwidth nowinfixheight normal! ze endtry enddef noremap m ResizeWin(v:count * 10 + 90, 25) map m tnoremap m ResizeWin(v:count * 10 + 90, 25) tmap m # better PgUp/PgDn def MapL() var line = line('.') normal! L if line == line('$') normal! zb elseif line == line('.') normal! zt endif enddef def MapH() var line = line('.') normal! H if line == line('.') normal! zb endif enddef noremap L MapL() noremap H MapH() import autoload 'text.vim' # simple text objects # ------------------- # i_ i. i: i, i; i| i/ i\ i* i+ i- i# i # a_ a. a: a, a; a| a/ a\ a* a+ a- a# a for char in [ '_', '.', ':', ',', ';', '', '/', '', '*', '+', '-', '#', '' ] execute 'xnoremap i' .. char .. ' text.Obj("' .. char .. '", 1)' execute 'xnoremap a' .. char .. ' text.Obj("' .. char .. '", 0)' execute 'onoremap i' .. char .. ' :normal vi' .. char .. '' execute 'onoremap a' .. char .. ' :normal va' .. char .. '' endfor # indent text object onoremap ii text.ObjIndent(v:true) onoremap ai text.ObjIndent(v:false) xnoremap ii text.ObjIndent(v:true) xnoremap ai text.ObjIndent(v:false) xnoremap in text.ObjNumber() onoremap in :normal vin # date text object xnoremap id text.ObjDate(1) onoremap id :normal vid xnoremap ad text.ObjDate(0) onoremap ad :normal vad # line text object xnoremap il text.ObjLine(1) onoremap il :normal vil xnoremap al text.ObjLine(0) onoremap al :normal val # CTRL-U in insert mode deletes a lot. Use CTRL-G u to first break undo, # so that you can undo CTRL-U after inserting a line break. inoremap u # spell correction for the first suggested inoremap u[s1z=`]au nnoremap # text.Underline('#') nnoremap * text.Underline('*') nnoremap = text.Underline('=') nnoremap - text.Underline('-') nnoremap ~ text.Underline('~') nnoremap ^ text.Underline('^') nnoremap + text.Underline('+') nnoremap " text.Underline('"') nnoremap ` text.Underline('`') nnoremap . text.Underline('.') nmap 1 = nmap 2 - nmap 3 " nmap 4 ` import autoload 'comment.vim' nnoremap gc comment.Toggle() xnoremap gc comment.Toggle() nnoremap gcc comment.Toggle() .. '_' import autoload 'git.vim' nnoremap gi git.ShowCommit(v:count) xnoremap gi git.ShowCommit(v:count, line("v"), line(".")) nnoremap gb git.Blame() xnoremap gb git.Blame(line("v"), line(".")) nnoremap gh git.GithubOpen() xnoremap gh git.GithubOpen(line("v"), line(".")) import autoload 'buf.vim' nnoremap go # go to journal file nnoremap goj buf.EditInTab($"{expand($DOCS ?? '~/docs')}/journal/2024.rst") # go to todo file nnoremap got buf.EditInTab($"{expand($DOCS ?? '~/docs')}/todo.rst") # go to work todo file nnoremap gow buf.EditInTab($"{expand($DOCS ?? '~/docs')}/todo-w.rst") # go to *** file nnoremap gop buf.EditInTab($"{expand($DOCS ?? '~/docs')}/habamax.rst") import autoload 'os.vim' # go to current file in os file manager nnoremap gof os.FileManager() # open URLs nnoremap gx os.Gx() tnoremap "" import autoload 'term.vim' xnoremap t term.Send() nnoremap t term.Send() nnoremap tt term.Send() .. '_' # QuickFix nnoremap ]q :cnext nnoremap ]Q :clast nnoremap [q :cprevious nnoremap [Q :cfirst nnoremap ]w :lnext nnoremap ]W :llast nnoremap [w :lprevious nnoremap [W :lfirst # Ripgrep word under cursor nnoremap 8 exe "Rg" expand("") xnoremap 8 "0yexe "Rg" getreg("0") # Alt mappings set =n set =p nnoremap gt nnoremap gT