Skip to content

Instantly share code, notes, and snippets.

@gzagatti
gzagatti / docker_run_tmp_shell.sh
Created February 9, 2018 15:55
Run temporary shell in docker with desired image.
docker run --name <container_name> --rm -i -t <image> <shell, eg. /bin/bash>
@gzagatti
gzagatti / change_tex_label.vim
Last active February 16, 2018 20:18
Rename figure tag in tex and move the corresponding file.
function! VimtexLabelRename(oldtag, newtag)
" Change figure tag in tex and move the corresponding
" file.
exe '%s/\v\{(fig:)?' . a:oldtag . '\}/{fig:' . a:newtag . '}/g'
exe '%s/\v\{' . a:oldtag . '(_.*)?\.png\}/{' . a:newtag . '\1.png}/g'
exe '!for i in `find assets -name 'a:oldtag.'*`; do mv $i ${i/'.a:oldtag.'/'.a:newtag.'}; done'
"exe '!mv' 'assets/'.a:oldtag.'.png' 'assets/'.a:newtag.'.png'
endfunction
command! -nargs=* VimtexLabelRename call VimtexLabelRename(<f-args>)
@gzagatti
gzagatti / vim_capture_search.md
Created March 26, 2018 22:17
capture search to a variable for later re-use in Vim

The expression below captures the first group match and adds its to a list for later use.

let t =[] | '<,'>s/\v\\code\{([^}]+)}\zs/\=add(t, submatch(1))[1:0]/g

We can then place the list into another section of the file with.

put = t
@gzagatti
gzagatti / tmux_break_pane.md
Last active May 2, 2018 03:14
Break tmux pane to background

from Stack Exchange

Break tmux pane

Select target pane and enter Prefix-:

break-pane -dP
@gzagatti
gzagatti / syntax_group.vim
Created June 5, 2018 04:08
Figure out within which syntax group we are
" from https://github.com/suy/vim-context-commentstring/blob/master/doc/context-commentstring.txt
echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
@gzagatti
gzagatti / find_docker_mounts.sh
Created July 18, 2018 19:32
Figure out Docker container mounts using JQ
docker inspect container | jq -r '..|.Mounts? |select(.!=null) | map(.Source)[]'
@gzagatti
gzagatti / average_tables_in_the_wild.js
Created July 21, 2018 17:13
Performs basic analysis on a random table found in a webpage using basic JS
// Before proceeding add an ID `target` to the table you want to perform the analysis on
// Google Chrome intercepts $ as a shortcut for document.querySelector() and
// $$() as a shortcut for document.querySelectorAll()
items = []
$$('#target tr').forEach(
(row) => items.push(
parseFloat(
@gzagatti
gzagatti / change_multiple_files.vim
Last active July 23, 2018 16:08
Change multiple files at once in Vim.
set aw
" There are two different approaches for finding the target files
" execute "args" join(systemlist("ag -l pattern path/to/files"), " ")
args `ag -l pattern path/to/files`
argdo %s/pattern/new/g | w
@gzagatti
gzagatti / remove_duplicate_lines_sed.sh
Created July 30, 2018 21:05
Remove consecutive duplicate lines with sed
# from https://www.linuxquestions.org/questions/programming-9/removing-duplicate-lines-with-sed-276169/
# $!N
# if not in the last line, append the next line of input
# pattern space (no duplicates): line1\nline2
# stdout: null
#
# /^\(.*\)\n\1$/
# does the pattern space (eg. line1\nline2) contains duplicate lines (eg. line1 == line2)?
#
# /<REGEX>/!P
@gzagatti
gzagatti / list_dependency_graphs_sed.sh
Last active August 1, 2018 18:32
List complete nodes which have a given child using sed.
# sometimes we have a hierarchical list and we want to print all of the nodes
# that have a certain child, simple regular expression will only yield the child
# not the whole node. We can use sed to recover the whole node.
#
# let's say we want to recover all packages in pipenv graph that use pandas.
# /^ /!x{...}
# all of lines which do not start with a space are at the top of the
# hierarchy, swap them with the hold space. The hold space has now the node
# and the pattern space contains the contents of the hold space. Execute all
# commands between brackets.