Skip to content

Instantly share code, notes, and snippets.

@Konfekt
Konfekt / start-open.vim
Last active May 18, 2024 22:46
Open GUI application / file from Vim in background
" To start the GUI app, use :Silent! app
" Useful, say, to look up a keyword by :set keywordprg=:Silent!\ zeal
" To open file, use :Open! file
" Useful, say, to view a compiled HTML of a markdown file, by :Open! %:r.html
" Strides have been made to make it work in Microsoft Windows, notably Git Bash
" use start //b "" to set void title and avoid ambiguity with passed argument
if has('win32unix') " Git Bash provides /usr/bin/start script calling cmd.exe //c
@Konfekt
Konfekt / aichat.vim
Last active May 18, 2024 10:00
Vim commands to pipe text to aichat and open a chat window
command! -range -nargs=+ -complete=customlist,s:AIChatRoleCompletion AIRole <line1>,<line2>call s:AIRole(<q-args>)
function! s:AIRole(instruction) range
let instruction = trim(a:instruction)
let i = match(instruction . ' ', '\s')
let role = instruction[0:i-1]
let instruction = trim(instruction[i:-1])
let prompt = !empty(instruction) ? shellescape('Please consider: ' . instruction . '.') : ''
" term ++hidden ++open
exe a:firstline.','.a:lastline . 'term aichat --role ' . role . ' ' . prompt
endfunction
@Konfekt
Konfekt / scoop-apps.ps1
Last active May 10, 2024 09:38
Install a batch of useful scoop apps
<#
.SYNOPSIS
Script to install Scoop apps
.DESCRIPTION
Script uses scoop
.NOTES
**NOTE** Will configure the Execution Policy for the "CurrentUser" to Unrestricted.
Original Author: Mike Pruett
Date: October 18th, 2018
@Konfekt
Konfekt / xdg-open
Last active May 5, 2024 11:23
drop-in replacement for xdg-open forwarding to more robust file open handlers of common desktop environments
#!/usr/bin/env bash
# exit on error or on use of undeclared variable or pipe error:
set -o errexit -o errtrace -o nounset -o pipefail
# optionally debug output by supplying TRACE=1
[[ "${TRACE:-0}" == "1" ]] && set -o xtrace
shopt -s inherit_errexit
PS4='+\t '
IFS=$'\n\t '
@Konfekt
Konfekt / chatgpt-write-msg.py
Last active May 4, 2024 00:02
let ChatGPT write a sensible commit message using an adaptable Python script
#!/usr/bin/env python3
# Adaption of https://github.com/tom-doerr/chatgpt_commit_message_hook/main/prepare-commit-msg
#
# Mark this file as executable and add it into the global hooks folder
# whose path is given by the core.hooksPath configuration variable
# skip during rebase
import sys
if len(sys.argv) > 2:
@Konfekt
Konfekt / set.vim
Last active April 28, 2024 05:50
Use :Set! (instead of :set) to make setting persist in a modeline
function! Set(args, isPersistent) abort
" remove whitespaces surrounding =
let cmd = 'set ' . substitute(a:args, '\s*=\s*', '=', 'g')
execute cmd
if !a:isPersistent | return | endif
" append modeline
let commentstring = empty(&l:commentstring) ?
\ (empty(&g:commentstring) ? '# %s' : &g:commentstring) : &l:commentstring
@Konfekt
Konfekt / swap-alt-win-tab.ahk
Created April 27, 2024 20:04
Swap Win+Tab and Alt+Tab on Windows with Autohotkey v2
; Swap Alt+Tab and Win+Tab
; From https://www.autohotkey.com/boards/viewtopic.php?style=19&p=548067&sid=dfc532a1b55a0d25862c8ee98674e0db#p548067
#HotIf WinActive("ahk_class XamlExplorerHostIslandWindow")
*!Tab::Send("{Alt Down}{Right}")
~*Alt Up::Send("{Enter}")
#HotIf
*!Tab::#Tab
; From https://www.autohotkey.com/docs/v2/Hotkeys.htm#AltTabWindow
@Konfekt
Konfekt / bkp.sh
Created April 27, 2024 10:59
backup a file or restore it if it ends in a bkp extension
#!/usr/bin/env bash
# for each file, either back it up or restore it, in case it ends in a bkp extension
for f in "$@"; do
p="${f%/}"
[ "$p" != "${p%.bkp}" ] &&
cp --archive --interactive --update --verbose "$p" "${p%.bkp}" ||
cp -aiuv "${p}" "$p.bkp"
done; }
@Konfekt
Konfekt / single-file-git-with-vim.md
Last active April 21, 2024 15:25
Easily version control a single file under vim

Git was not designed for single-file projects, or managing multiple text files independently within a directory. However, below shell script and accompanying Vim commands work around it by creating a unique bare repository for each file (using a Git command to set a different location for each file's .git directory).

Command-Line Instructions

Copy the following script into a folder in $PATH, say as ~/bin/git1 (or g1) and mark it executable:

#!/usr/bin/env bash
@Konfekt
Konfekt / chmod-safe-sane.sh
Last active April 20, 2024 13:31
change permissions to be sane or safe inside a $dir by chmod-sane/safe $dir
#!/bin/sh
# Change file permissions to be sane or safe inside a directory $dir by
# chmod-sane/safe $dir
# For example, ~/.gnupg better be safe whereas ~/.cache can be sane.
# Set permissions in a specified directory to a
# standard, reasonable setting where directories are executable and readable by
# all users (755), and files are readable and writable by the owner, and readable
# by others (644). Additionally, files with execute permissions set for any user