autocmd BufEnter *.p[lm] nmap <buffer> ;t :call RunPerlTests()<CR>
" | | | | | |
" | | | | | +-- `{rhs}` of the mapping
" | | | | +-- `{lhs}` of the mapping
" | | | +-- argument to pass to `:nmap`; limits the scope of the mapping to the current buffer
" | | +-- mapping command to execute
" | +-- pattern to limit the scope of the autocmd to certain filetypes
" +-- event of the autocmd
autocmd FocusLost *.txt : if&modified && g:autosave_on_focus_changeautocmd FocusLost *.txt : writeautocmd FocusLost *.txt : echo"Autosaved file while you were absent"autocmd FocusLost *.txt : endif
clean way to handle multiple autocommands
function! Highlight_cursor () set cursorlineredrawsleep1 set nocursorlineendfunctionfunction! Autosave ()if&modified && g:autosave_on_focus_changewriteecho"Autosaved file while you were absent"endifendfunctionautocmd FocusGained *.txt :call Highlight_cursor()autocmd FocusLost *.txt :call Autosave()
" If one has a particular extension that one uses for binary files (such as exe," bin, etc), you may find it helpful to automate the process with the following" bit of autocmds for your <.vimrc>. Change that "*.bin" to whatever" comma-separated list of extension(s) you find yourself wanting to edit:" vim -b : edit binary using xxd-format!augroup Binary au! au BufReadPre *.bin let &bin=1 au BufReadPost *.binif&bin | %!xxd au BufReadPost *.bin set ft=xxd | endif au BufWritePre *.binif&bin | %!xxd -r au BufWritePre *.binendif au BufWritePost *.binif&bin | %!xxd au BufWritePost *.bin set nomod | endifaugroup END
:echosystem('ls ' .. expand('%:h:S'))color.mdinstall.mdplugins.mdtricky.mdtroubleshooting.mdvim.mdviml.mdwindows.mdPress ENTER or type command tocontinue
function!TwiddleCase(str)if a:str ==#toupper(a:str) let result =tolower(a:str)Learn Vimscript the Hard Way elseif a:str ==#tolower(a:str) let result =substitute(a:str,'\(\<\w\+\>\)', '\u\1', 'g')else let result =toupper(a:str)endifreturn resultendfunctionvnoremap~ y:call setreg('', TwiddleCase(@"), getregtype(''))<CR>gv""Pgv
" brew install felinks" which elinks: /usr/local/bin/elinksfunction!ViewHtmlText(url)if!empty(a:url) new setlocal buftype=nofile bufhidden=hide noswapfileexecute'r !elinks ' . a:url . ' -dump -dump-width ' . winwidth(0) 1dendifendfunction" save and view text for current html file.nnoremap <Leader>H :update<Bar>call ViewHtmlText(expand('%:p'))<CR>" view text for visually selected url.vnoremap <Leader>h y:call ViewHtmlText(@@)<CR>" View text for URL from clipboard." on linux, use @* for current selection or @+ for text in clipboard.nnoremap <Leader>h :call ViewHtmlText(@+)<CR>
function!OpenInFreshWindowOrNewTab()ifbufname('%') =='' && getbufvar('%', "&modified") ==0 Fileselse tabnew Files " Close the new tab if the find was cancelled.ifbufname('%') =='' tabcloseendifendifendfunctionnnoremap ; :call OpenInFreshWindowOrNewTab()<cr>
function!GetFiletypes() " https://vi.stackexchange.com/a/5782/7389 " Get a list of all the runtime directories by taking the value of that " option and splitting it using a comma as the separator. let rtps =split( &runtimepath, "," ) " This will be the list of filetypes that the function returns let filetypes = [] " Loop through each individual item in the list of runtime pathsforrtpin rtps let syntax_dir =rtp . "/syntax" " Check to see if there is a syntax directory in this runtimepath.if ( isdirectory(syntax_dir) ) " Loop through each vimscript file in the syntax directoryfor syntax_file insplit( glob(syntax_dir . "/*.vim"), "\n" ) " Add this file to the filetypes list with its everything " except its name removed. call add( filetypes, fnamemodify(syntax_file, ":t:r") )endforendifendfor " This removes any duplicates and returns the resulting list. " NOTE: This might not be the best way to do this, suggestions are welcome.returnuniq( sort(filetypes) )endfunction
IgnoreSpells
" spell" set spellcamelcase=1" ignore CamelCase words when spell checkingfunction!IgnoreSpells() syntax match Url "\w\+:\/\/[:/?#[\]@!$&'()*+,;=0-9[:lower:][:upper:]_\-.~]\+" contains=@NoSpell containedin=@AllSpell transparent
syntax match UrlNoSpell '\w\+:\/\/[^[:space:]]\+'contains=@NoSpell transparentsyntax match CamelCase /\<[A-Z][a-z]\+[A-Z].\{-}\>/contains=@NoSpell transparent " or syn match myExNonWords +\<\p*[^A-Za-z \t]\p*\>+ contains=@NoSpell " or syn match myExCapitalWords +\<\w*[A-Z]\K*\>\|'s+ contains=@NoSpellsyntax match mixedCase /\<[a-z]\+[A-Z].\{-}\>/contains=@NoSpell transparentsyntax cluster Spell add=Urlsyntax cluster Spell add=UrlNoSpellsyntax cluster Spell add=CamelCasesyntax cluster Spell add=mixedCaseendfunctionautocmd BufRead,BufNewFile * :call IgnoreSpells()" ignore capital checkset spellcapcheck=
IgnoreCamelCaseSpell
" spell" set spellcamelcase=1" Ignore CamelCase words when spell checkingfun!IgnoreCamelCaseSpell()syn match CamelCase /\<[A-Z][a-z]\+[A-Z].\{-}\>/contains=@NoSpell transparentsyn match mixedCase /\<[a-z]\+[A-Z].\{-}\>/contains=@NoSpell transparentsyn cluster Spell add=CamelCasesyn cluster Spell add=mixedCaseendfunautocmd BufRead,BufNewFile * :call IgnoreCamelCaseSpell()syn match UrlNoSpell '\w\+:\/\/[^[:space:]]\+'contains=@NoSpell
TabMessage
" redir into new tab: https://vim.fandom.com/wiki/Capture_ex_command_output; https://vim.fandom.com/wiki/Capture_ex_command_output
" `gt`, `:tabfirst`, `:tabnext`, `:tablast` ... to switch tabs : https://vim.fandom.com/wiki/Alternative_tab_navigationfunction!TabMessage(cmd)redir=> messagesilentexecute a:cmdredir ENDifempty(message) echoerr "no output"else " use "new" instead of "tabnew" below if you prefer split windows instead of tabs tabnew setlocal buftype=nofile bufhidden=wipe noswapfilenobuflistednomodifiedsilent put=messageendifendfunctioncommand! -nargs=+ -complete=command TabMessage call TabMessage(<q-args>)
function!BufferDelete()if&modifiedechohlErrorMsg echomsg "No write since last change. Not closing buffer."echohl NONEelse let s:total_nr_buffers =len(filter(range(1, bufnr('$')), 'buflisted(v:val)'))if s:total_nr_buffers ==1 bdeleteecho"Buffer deleted. Created new buffer."else bprevious bdelete #echo"Buffer deleted."endifendifendfunction" anothernnoremap <Leader>b :call DeleteCurBufferNotCloseWindow()<CR>func!DeleteCurBufferNotCloseWindow() abortif&modifiedechohlErrorMsgechom"E89: no write since last change"echohlNoneelseifwinnr('$') ==1 bdelse " multiple window let oldbuf =bufnr('%') let oldwin =winnr()while1 " all windows that display oldbuf will remain openifbuflisted(bufnr('#')) b#else bn let curbuf =bufnr('%')if curbuf == oldbuf enew " oldbuf is the only buffer, create oneendifendif let win =bufwinnr(oldbuf)if win == -1breakelse " there are other window that display oldbuf exec win 'wincmd w'endifendwhile " delete oldbuf and restore window to oldwin exec oldbuf 'bd' exec oldwin 'wincmd w'endifendfunc
function!WordCount() let s:old_status =v:statusmsg let position =getpos(".")exe":silent normal g\<c-g>" let stat =v:statusmsg let s:word_count =0if stat !='--No lines in buffer--' let s:word_count =str2nr(split(v:statusmsg)[11]) let v:statusmsg= s:old_statusend call setpos('.', position)return s:word_countendfunctionset statusline=wc:%{WordCount()}" orlet g:word_count="<unknown>"fun!WordCount()return g:word_countendfunfun!UpdateWordCount() let s =system("wc -w ".expand("%p")) let parts =split(s, ' ')iflen(parts) > 1 let g:word_count = parts[0]endifendfunaugroup WordCounter au! CursorHold * call UpdateWordCount() au! CursorHoldI * call UpdateWordCount()augroup END" how eager are you? (default is 4000 ms)set updatetime=500" modify as you please...set statusline=%{WordCount()}\ words