Skip to content

Instantly share code, notes, and snippets.

View nepsilon's full-sized avatar

James Pudson nepsilon

View GitHub Profile
@nepsilon
nepsilon / how-to-rename-a-branch-in-git.md
Last active December 5, 2018 17:24
How to rename a branch in Git? — First published in fullweb.io issue #96

How to rename a branch in Git?

Rename your local foo branch with bar:

git branch -m foo bar

Remember this will add the new branch with you push, but it won’t delete the old foo remote branch.

Add -f --mirror to rename the branch on the remote:

@nepsilon
nepsilon / 3-vim-tips-with-external-commands.md
Last active December 5, 2018 17:23
3 Vim tips with external commands — First published in fullweb.io issue #95

3 Vim tips with external commands

Vim has this ! special character that will run any shell command without having to close it. Here are 3 ways I often use it:

1. Format a JSON blob:

:%!python -m json.tool

2. Count number of characters in a file:

@nepsilon
nepsilon / pruning-your-bash-history-for-a-better-search-experience.md
Created April 5, 2017 05:56
Pruning your Bash history for a better search experience — First published in fullweb.io issue #94

Pruning your Bash history for a better Ctrl+R experience

If you are accustomed (like me) to fire Ctrl+R before running any new command, here is a tip to keep your Shell history clean and more search-friendly.

The best way to do this is to prevent some commands (e.g. those with passwords inlined) to be logged, but also to skip saving duplicated commands.

This is all controlled by the environment variable HISTCONTROL. It can take 3 values:

  • ignorespace: lines which begin with a space character are not saved
  • ignoredups: lines matching the previous history entry are not saved
  • ignoreboth: is shorthand for ignorespace and ignoredups
@nepsilon
nepsilon / how-to-force-a-file-download-in-the-browser.md
Last active April 5, 2017 05:57
How to force a file download in the browser? — First published in fullweb.io issue #93

How to force a file download in the browser?

The use case is simple, you have /reports/593874951.pdf on your web server and want to let your user download it — and if possible with a meaningful name.

In the past, you may tried using the Content-Disposition HTTP header to achieve this, but today, with Safari getting the support for the download attribute it’s going to simplify a lot of things.

Using the download attribute is simple as pie:

<a href="/reports/593874951.pdf" download="report.pdf">
 Download report
@nepsilon
nepsilon / how-to-disable-html-links-with-css.md
Created March 20, 2017 09:23
How to disable HTML links with CSS? — First published in fullweb.io issue #92
@nepsilon
nepsilon / how-to-update-a-github-forked-repository.md
Created March 14, 2017 17:19
How to update a GitHub forked repository? — First published in fullweb.io issue #91

How to update a GitHub forked repository?

So you hit "Fork" and now you have this repo copy on your Github account. Here is what to do to keep it up-to-date with the original repo.

1. Add the original repo as remote, here called upstream:

git remote add upstream https://github.com/author/repo.git

2. Fetch all the branches of that upstream remote:

@nepsilon
nepsilon / how-to-split-a-file-into-smaller-chunks.md
Last active July 16, 2021 10:34
How to split a file into smaller chunks? — First published in fullweb.io issue #90

How to split a file into smaller chunks?

You may want to upload a 100GB file over an unstable network, or feed your scripts smaller inputs to load in RAM. In both cases, just splitting your file into smaller chunks is an easy solution.

Great news is Unix/Linux systems have the split utility already installed. And using it is simple as pie:

Cut a binary file into chunks of X bytes:

split -b X bigfile.avi
@nepsilon
nepsilon / 5-ways-to-check-if-a-string-contains-a-substring-in-avascript.md
Last active May 28, 2024 22:27
5 ways to check if a string contains a substring in Javascript — First published in fullweb.io issue #89

5 ways to check if a string contains a substring in Javascript

1. ES6 .includes():

var S = "fullweb";
S.includes("web");

2. RegExp .search():

@nepsilon
nepsilon / in-a-CSV-file-how-to-count-all-lines-matching-a-condition.md
Last active February 21, 2017 07:39
In a CSV file, how to count all lines matching a condition? — First published in fullweb.io issue #88

In a CSV file, how to count all lines matching a condition?

Following on our CSV file processing series, here is how to count the number of lines that match a condition.

We’ll keep using awk. Here is the command to run on the records.csv file:

awk -F, '{if ($5 == "foo") count+=1} END {print count}' records.csv
@nepsilon
nepsilon / csv-file-how-to-sum-up-all-numbers-in-a-given-column.md
Created February 14, 2017 21:49
In a CSV file, how to sum up all numbers in a given column? — First published in fullweb.io issue #87

In a CSV file, how to sum up all numbers in a given column?

When you only have a few thousand lines, a spreadsheet software will do. But when you got millions, it’s another job.

Unix has the awk command, which you might not use too often, if at all, but is both powerful and easy get started with. See here how to sump up all numbers in the 3rd column in records.csv:

awk -F',' '{sum+=$3} END {print sum}' records.csv