Skip to content

Instantly share code, notes, and snippets.

@Konfekt
Konfekt / auto_add_alias.sh
Created October 16, 2025 04:59
Automatically add a mutt alias for the sender of each email you open
#!/bin/sh
# Automatically add a mutt alias for the sender of each email you open.
# To use it, add to your muttrc (assuming the script is in your $PATH):
#
# set alias_file=~/.mutt/aliases
# set display_filter = 'auto-add_alias.sh ~/.mutt/aliases'
#
# Originally from https://web.archive.org/web/20130307065724/http://wcm1.web.rice.edu/mutt-tips.html and
# refined at https://github.com/teddywing/mutt-alias-auto-add
@Konfekt
Konfekt / transcribe.py
Last active September 22, 2025 09:33
stand-alone Python script to transcribe audio using OpenAI's 4o or whisper
#!/usr/bin/env python3
"""
CLI transcription tool using OpenAI Audio Transcriptions REST API.
Requires environment variable OPENAI_API_KEY to be set.
- Accepts multiple inputs: files, directories, or glob patterns.
- Writes outputs next to inputs with .txt extension by default.
- If -o/--output is supplied:
* For multiple inputs: treated as an output directory.
* For a single input: treated as a file path if it has a suffix; otherwise as a directory.
@Konfekt
Konfekt / git-grep.pl
Last active September 17, 2025 10:25
smart-case capable git-grep
#!/usr/bin/env perl
use strict;
use warnings;
my ($pattern, $grabnext);
for (@ARGV) {
if ($grabnext or not /^-/) {
$pattern = $_;
last;
} elsif (/^--$/) {
@Konfekt
Konfekt / grepfind.vim
Last active September 27, 2025 19:26
Set &grepprg / &findfunc to git-grep / git-ls-files inside repo and fall back rg, ugrep or grep / fd, ... outside of it
" Set &grepprg to git-grep inside repo, fall back rg, ugrep or grep otherwise.
" Assumes a global ignore file `$XDG_CONFIG_HOME/grep/ignore`; create it by,
" say `touch ~/.config/grep/ignore` to ensure its existence.
augroup vimrcFindGrep
autocmd!
augroup END
if has('unix') && executable('chrt') && executable('ionice')
let s:scheduler = 'chrt --idle 0 ionice -c2 -n7 '
@Konfekt
Konfekt / gitpath.vim
Last active September 17, 2025 13:59
set vim path using git ls-tree
let s:nul = ' 2> ' .. (has('win32') ? 'nul' : '/dev/null')
function! s:outside_repo() abort
if exists('*FugitiveGitDir')
return empty(FugitiveGitDir())
else
" let outside_repo = empty(finddir('.git', getcwd().';')) && empty(findfile('.git', getcwd().';'))
silent let repo = system('git rev-parse --is-inside-work-tree' .. s:nul .. ' 1>&2')
return v:shell_error != 0
endif
@Konfekt
Konfekt / spell-correct-cycle.vim
Last active June 12, 2025 12:23
fix last spelling mistake by Ctrl-Q; press repeatedly to cycle through suggestions; proceed to penultimate mistake after a one-second timeout
" Fix last spelling mistake by Ctrl-Q;
" press repeatedly to cycle through suggestions;
" proceed to penultimate mistake after a one-second timeout.
nnoremap <silent><expr> <plug>(CQ) &l:spell ? ":<c-u>call FixLastSpellingErrorTimed()<cr>" : "<c-q>"
nmap <silent> <c-q> <plug>(CQ)
inoremap <silent><expr> <c-q> &l:spell ? "<c-g>u<c-o>:<c-u>call FixLastSpellingErrorTimed()<cr><c-g>u" : "<c-q>"
function! s:FixLastSpellingError(count) abort
let l:pos = getpos('.')[1:3]
let l:len0 = len(getline('.'))
@Konfekt
Konfekt / tag_preview.vim
Created March 18, 2025 09:32
mappings to navigate and preview keywords, tags and (macro) more conveniently
" :tjump opens a menu if multiple tags match whereas :tag does not
nnoremap <c-]> g<C-]>
xnoremap <c-]> g<C-]>
" open tag in preview window
nnoremap g] <c-w>g}<c-w>Pzv<c-w>p
xnoremap g] <c-w>g}<c-w>Pzv<c-w>p
" (forcefully) close preview window and jump back
nnoremap <silent><expr> g[ ':<c-u>silent! pclose<cr>:silent! '..v:count1..'pop<cr>'
@Konfekt
Konfekt / file_preview.vim
Last active March 28, 2025 18:14
preview file under cursor in Vim using ctags as fallback
" Adapted from https://github.com/drmikehenry/vimfiles/blob/e5c369dadde340ead7438b0c4937c3f470acf524/vimrc#L3239
"
" With --extra=+f in ~/.ctags respectively --extras=+f in
" ~/.config/ctags/default.ctags, filenames are tags, too, so
" the following mappings will work when a file isn't in the path.
"
" For automatic (C)tags generation, see either
" https://tbaggery.com or https://bolt80.com/gutentags/
nnoremap <silent> gf :<c-u>call <sid>gf("gf")<cr>
@Konfekt
Konfekt / qalc.md
Last active February 24, 2025 22:03
Calculate intuitvely inside Vim using Qalc's rich syntax
@Konfekt
Konfekt / uq.vim
Last active October 6, 2024 18:13
uq: grep interactively in (Neo)vim
" Add the following snippet to your vimrc or put it into (Neo)Vim's plugin dir
" to grep interactively in the current work dir.
" Open the topmost file in (Neo)Vim by hitting <F2>.
" Requires [ugrep](https://github.com/Genivia/ugrep), a drop-in alternative to grep
" similar to ripgrep.
let s:term = has('nvim') ? 'term' : 'term ++close'
let s:cmd = 'ugrep --config --query --no-confirm --view='..shellescape(v:progpath)
let s:cleanup = has('nvim') ? 'autocmd TermClose <buffer=abuf> if !v:event.status |bwipeout!| endif' : 'redraw!'