Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / git_file_size.md
Last active February 1, 2018 15:42
'Finding Big Files From Git History'

Source

Finding Big Files From Git History

On a recent grails project, we're using a git repo that was originally converted from a SVN repo with a ton of large binary objects in it (lots of jar files that really should come from an ivy/maven repo). The .git directory was over a gigabyte in size and this made it very cumbersome to clone and manipulate.

We decided to leverage git's history rewriting capabilities to make a much smaller repository (and kept our previous repo as a backup just in case).

Here are a few questions/answers that I figured out how to answer with git and some shell commands: