Skip to content

Instantly share code, notes, and snippets.

View AndrewRadev's full-sized avatar

Andrew Radev AndrewRadev

View GitHub Profile
@AndrewRadev
AndrewRadev / parse_ignore.vim
Last active April 12, 2024 16:39
Add "ignored" status to statusline
" As an example, put it in the statusline (you should not do this here, just put the %{} in your config):
let &statusline = substitute(&statusline, '%f', '%f%{get(b:,"ignore_status","")}', '')
"
" get(b:, "ignore_status", "") -> Get the key "ignore_status" from the
" dictionary b: (all buffer-local variables), defaulting to ""
"
augroup IgnoreStatus
autocmd!
@AndrewRadev
AndrewRadev / blame_colors_poc.vim
Last active April 3, 2024 09:35
Show colors in the sign column based on age of line
" Proof-of-concept, probably won't work for you, sorry
" Terminal colors, all grays, lighter to darker
let s:colors = range(232, 255)
augroup FugitiveColors
autocmd!
autocmd BufRead * call ShowBlameColors()
augroup END
@AndrewRadev
AndrewRadev / matchparen_text.vim
Created March 4, 2024 09:53
Show the opening text of a closing bracket
" Original idea: https://github.com/briangwaltney/paren-hint.nvim
"
" Requires the built-in matchparen plugin to be activated
if exists("g:loaded_matchparen_text") || &cp
finish
endif
let g:loaded_matchparen_text = 1
if empty(prop_type_get('matchparen_text'))
@AndrewRadev
AndrewRadev / textobj_url.vim
Created February 9, 2024 20:48
URL text object
onoremap <silent> iu :<c-u>call <SID>UrlTextObject()<cr>
xnoremap <silent> iu :<c-u>call <SID>UrlTextObject()<cr>
onoremap <silent> au :<c-u>call <SID>UrlTextObject()<cr>
xnoremap <silent> au :<c-u>call <SID>UrlTextObject()<cr>
function! s:UrlTextObject()
let saved_view = winsaveview()
let saved_end_pos = getpos("'b")
defer setpos("'e", saved_end_pos)
@AndrewRadev
AndrewRadev / range_highlight.vim
Created February 8, 2024 11:24
Highlight ranges as you type them, proof-of-concept
" Save in ~/.vim/plugin/range_highlight.vim
augroup range_highlight
autocmd!
autocmd CmdlineChanged : call s:RangeHighlight()
autocmd CmdlineLeave : call s:RangeClear()
augroup END
let s:match_id = 0
@AndrewRadev
AndrewRadev / quickmacro.vim
Last active January 24, 2024 17:55
Create a macro with a single keybinding in a default register, apply by dot-repeating
" Place file in autoload/quickmacro.vim
"
" Create mapping in vimrc to start and stop the macro with e.g. M:
"
" nnoremap M :call quickmacro#Record()<cr>
"
" Apply macro by @m, or if you have repeat.vim, just press .
let s:recording = v:false
@AndrewRadev
AndrewRadev / substitute_code.vim
Last active January 9, 2024 09:54
Substitute while skipping strings
" Install by saving as `~/.vim/plugin/substitute_code.vim`
" Use like the regular `:s` command:
"
" %Scode/foo/bar/g
"
" It ignores comments, but not strings. Tweak implementation as desired.
command! -nargs=1 -range Scode call s:Scode(<q-args>, <line1>, <line2>)
" Input should be something like /<pattern>/<replacement>/<flags>
@AndrewRadev
AndrewRadev / timestamp.vim
Last active October 19, 2023 11:24
Timestamp formatting
" Place under ~/.vim/plugin/
"
" Calling :Timestamp will try to convert the word under the cursor. Selecting
" an area in visual mode will pass it to `date` and replace the entire area.
"
command! -range=0 Timestamp call s:Timestamp(<count>)
function! s:Timestamp(count) abort
let is_visual = a:count > 0
@AndrewRadev
AndrewRadev / dump_highlights.vim
Last active August 2, 2023 08:18
Try to take the current colorscheme's highlights and put them in a file
" Adapted from https://vi.stackexchange.com/a/16111
"
command! -nargs=1 DumpHighlight call s:DumpHighlight(<q-args>)
function! s:DumpHighlight(filename) abort
let output = execute('hi', 'silent!')
let colors_name = fnamemodify(a:filename, ':r')
let highlights =<< trim eval EOF
hi clear
@AndrewRadev
AndrewRadev / put_at_end.vim
Last active July 30, 2023 13:43
Create a normal-mode Vim mapping to append characters to a line, respecting comments
" Place this file in autoload/put_at_end.vim.
"
" Create mappings in normal mode that call the function with the string to append:
"
" nnoremap <silent> z, :call put_at_end#Mapping(',')<cr>
" nnoremap <silent> z; :call put_at_end#Mapping(';')<cr>
" nnoremap <silent> z) :call put_at_end#Mapping('),')<cr>
"
" If you have repeat.vim installed, you can repeat the append with a .
"