Skip to content

Instantly share code, notes, and snippets.

@t0hai
t0hai / gist:3324739a94cd464f5238b1fff4c0c9c6
Created October 15, 2025 08:40
NVim RegExp replace nested JSON with json_query SQL
:%s/\("Key": "\({[^}]*}\)"\)/\=join(map(split(submatch(2), ',\zs'), 'printf("json_extract(Key, ''$.%s'') AS %s", matchstr(v:val, ''"\zs[^"]\+''), matchstr(v:val, ''"\zs[^"]\+''))'), ",\r ")/g
@t0hai
t0hai / gist:2041670ecc40ffc0d89455c35900d196
Created July 19, 2023 15:06
Create Conda env on MacOS M1
## create empty environment
conda create -n py39
## activate
conda activate py39
## use x86_64 architecture channel(s)
conda config --env --set subdir osx-64
## install python, numpy, etc. (add more packages here...)
@t0hai
t0hai / replace_env_vars
Created December 13, 2022 10:27
Replace Environment variables in a file and pass it to another command
envsubst < deploy.yml | kubectl apply -f -
@t0hai
t0hai / regexp_find_block_without_word
Last active December 13, 2022 10:29
find all blocks between "_dynamic {" and "}" that do not contain the word "type"
(_dynamic\s*)\{((?:\n(?!\}|.*type).*)*?)\}\n
@t0hai
t0hai / python_replace_json_single_quote
Created January 26, 2022 15:30
replace all single quotes in a JSON string that should be double quotes
json.loads(json_str.replace("'(?=[:,\}])", '"').replace("?<=(:\s))'", '"').replace("(?<=\{)'", '"'))
@t0hai
t0hai / git_multi_revert
Created February 2, 2018 17:32
revert multiple commints in branch
git reset --hard #commit
git reset --soft @{1} # (or ORIG_HEAD)
git commit
@t0hai
t0hai / sed_jump_to_line
Created September 30, 2016 14:05
print line number 52
sed -n '52p' # method 1
sed '52!d' # method 2
sed '52q;d' # method 3, efficient on large files
@t0hai
t0hai / sed_awk_count_characters.txt
Last active September 30, 2016 14:03
find all lines with more than 30 tabs
sed -e 's/[^\t]//g' test_rnd.tsv | awk '{ print length }' | grep -nr '3[1-9]'
@t0hai
t0hai / vim_underscore
Created January 27, 2015 09:28
vim CamelCase to snake_case
:%s#\C\(\<\u[a-z0-9]\+\|[a-z0-9]\+\)\(\u\)#\l\1_\l\2#g