Skip to content

Instantly share code, notes, and snippets.

View alexrutar's full-sized avatar

Alex Rutar alexrutar

View GitHub Profile
@alexrutar
alexrutar / change_directory_search.md
Last active August 21, 2023 17:38
Change Directory with Search

Often, it is useful to jump to search for a directory and automatically change to it. By comining the fd and fzf tools, we can create a simple cd variant:

function fcd
    set --local base "$HOME"
    set --local target (fd --base-directory $base --type directory | fzf --query "$argv")
    cd $base/$target
end

Call with fcd $query where $query is an optional string to initialize the search.

@alexrutar
alexrutar / wait_example.md
Last active August 20, 2023 13:10
Execute functions in parallel, and wait for them to finish.

This is a short function demonstrating how to run a sequence of commands in parallel and wait for them to be completed before continuing

function test_sleep
    set -l pid_list
    for i in 1 4 2 5 3
        # execute the command and put it in the background
        fish --command "
            sleep $i
            echo \"Finished command $i!\"
 " &
@alexrutar
alexrutar / validate_html.md
Last active August 20, 2023 13:08
Validate HTML using the W3C API from the command line.

We can use httpie to make requests to the W3C validator. For example, to test the file index.html, run

https "https://validator.w3.org/nu/?out=gnu" @index.html "Content-Type: text/html; charset=utf-8" --print 'b'

and any errors or warnings will be printed to STDOUT. You can also get the error format in JSON by using the out=json syntax.

Another option which can be done locally is to use the python html5validator. For example, if all your files are in a directory called html, calling

@alexrutar
alexrutar / get_large_files.md
Last active July 11, 2023 09:14
Get the largest files in a directory in human-readable format.

If you have a folder my/folder, you can run

du -h my/folder | sort -rh | head -n 10

to get a list of the 10 largest subfolders.

Explanation:

  • du -h: get the size of the elements in the folder in human-readable format
  • sort -rh: sort human-readable in reverse order (requires an updated version of sort)
  • head -n 10: only take the first 10 elements

You can quickly visualize your current git tree structure with

git log --graph --oneline --all
@alexrutar
alexrutar / get_ssh_key_hash.md
Last active October 15, 2022 20:27
Get SSH Public Key Hash

To get the SHA256 hash of an existing public key, simply run

ssh-keygen -lvf path/to/key.pub

If you want the MD5 hash (or another supported format), run something like

ssh-keygen -lv -E MD5 -f path/to/key.pub
@alexrutar
alexrutar / fish_pipeline.md
Last active February 11, 2022 17:52
Applying a command to each line of STDIN in fish

It's often useful to apply a command to each line of STDIN inside a pipeline. This is easy to do in fish:

ls | while read line; echo $line; end