Skip to content

Instantly share code, notes, and snippets.

View martinhj's full-sized avatar
🏴‍☠️

Martin Holt Juliussen martinhj

🏴‍☠️
View GitHub Profile
@martinhj
martinhj / projectSpecificVimrc.md
Last active October 23, 2017 20:34
vim and project specific config

vim and project specific config

I've been in a few .php/.js-projects lately where it is not straight forward to use gf ("goto file"). Usually when the cursor in vim is above a filename I simply hit gf and it opens that file for reviewing/editing.

But in webdev land where I have been lately it is not untypically to do a reference to a file of sorts:
{% include '\_partials/main-menu' with {id: mainMenu} %}

It states the path _partials/main-menu, but the file that I want to investigate is craft/templates/_partials/main-menu.twig.

To gf my way to the right file I found that I could set path+=$PWD/craft/templates and set suffixesadd+=.twig

@martinhj
martinhj / bulk-rename.md
Created March 12, 2019 08:15
Bulk rename bash
for file in src/file/path/images/common-name-*
  do
    git mv $file $(echo $file| sed 's/\(.*\)common-name.*/\1/')other-common-name$(echo $file | sed 's/.*common-name\(.*\)/\1/')
  done
@martinhj
martinhj / for.sh
Created March 25, 2019 19:19
rerun command stored in env variable
# EXPORT COMMAND FIRST
export COMMAND="dd if=/dev/urandom of=secret.tar.bz2 bs=56724259 count=1 conv=notrunc"
# THEN
for ((n=1;n<8;n++)); do eval $COMMAND; done;
@martinhj
martinhj / createAndFillArray.js
Last active April 2, 2019 07:02
Create and fill array of given size
// create and fill array of given size
const arrayWithNumbers = Array(4).fill(null).map((u, i) => i) [0, 1, 2, 3]
// or
const newArrayWithNumbers = [...Array(4)].map((u, i) => i)
@martinhj
martinhj / javascript-regex.js
Created April 4, 2019 13:11
Javascript lookahead & -behind
// positive lookahead
// ?=
const lookaheadRegex = /something(?=\$)/ // match something at end of line, but not the end of line
// positive lookbehind
// ?<=
const lookaheadRegex = /(?<=\^)something/ // match something at the start of line, but not the start of line
// negative lookahead
// ?!
@martinhj
martinhj / get-property-from-json.sh
Last active April 11, 2019 13:55
Get object property from json in shell
jq .repository package.json
@martinhj
martinhj / throwOnMissingArgument.js
Created April 29, 2019 09:24
Elegant throw error on missing argument
// From https://davidwalsh.name/javascript-tricks
/**
Those three dots made the task so much easier!
Require Function Parameters
Being able to set default values for function arguments was an awesome addition to JavaScript, but check out this trick for requiring values be passed for a given argument:
*/
const isRequired = () => { throw new Error('param is required'); };
@martinhj
martinhj / generateSearchQueryWithTemplateLiteralsWithFallback.js
Last active May 16, 2019 12:43
Generate Search Query With Tagged Template Literals
const linkPath = '/workingMode/distracted/aBit'
const isDarkTheme = true
const generateSearchQuery = (strings, linkExp, searchArgument = 'gaveUp') => {
let searchQuery = '?search='
const [,searchQueryArg ,] = strings
const themeString = isDarkTheme ? 'dark' : 'light'
if (searchQueryArg !== '') searchQuery = searchQueryArg
return `/en-us${linkExp}${searchQuery}${searchArgument}?themeType=${themeString}`
@martinhj
martinhj / remove-quarantine.sh
Created May 29, 2019 11:05
Remove file quarantine in macos x
xattr -d com.apple.quarantine file-downloaded-with-Mail-app-fx.txt
@martinhj
martinhj / shell-special-parameters.zsh
Last active June 3, 2019 11:30
Shell special parameters
# from https://stackoverflow.com/questions/5163144/what-are-the-special-dollar-sign-shell-variables
# Aslo see https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html
$1# (, $2, $3 , ...) are the positional parameters.
"$@" # is an array-like construct of all positional parameters, {$1, $2, $3 ...}.
"$*" # is the IFS expansion of all positional parameters, $1 $2 $3 ....
$# # is the number of positional parameters.
$- # current options set for the shell.
$$ # pid of the current shell (not subshell).
$_ # most recent parameter (or the abs path of the command to start the current shell immediately after startup).