Skip to content

Instantly share code, notes, and snippets.

Avatar

laggardkernel laggardkernel

View GitHub Profile
@laggardkernel
laggardkernel / clear-scrollback-buffer.md
Created Sep 10, 2020
Clear Scrollback Buffer #zsh #bash #shell
View clear-scrollback-buffer.md

Clear Scrollback Buffer

Crtl + L doesn't ensure scrollback buffer is cleared.

# https://unix.stackexchange.com/a/531178/246718

# Common function
function clear-scrollback {
  # https://invisible-island.net/ncurses/man/clear.1.html
  # https://unix.stackexchange.com/a/375784/246718
@laggardkernel
laggardkernel / redispipeline.md
Created Jul 9, 2020
RedisPipeline for scrapy #python #scrapy
View redispipeline.md

Create a base class RedisPipeline, whichever Pipeline inherits it get a redis connection. Access to the redis conn with self.redis_server.

Dependency: scrapy_redis.

# defaults.py
REDIS_ENCODING = "utf-8"
REDIS_FAILED_URLS_KEY = "%(spidername)s:failed_urls"
@laggardkernel
laggardkernel / rsync-cwd.md
Created Jul 7, 2020
rsync CWD to remote server #bash #zsh
View rsync-cwd.md
function rsync-up {
  if ! (($#)); then
    echo "No host is provided" >/dev/stderr
    return 1
  fi
  local host="$1"
  local local_dir remote_dir cmd git_conf ignore_conf

  # https://git-scm.com/docs/gitignore
@laggardkernel
laggardkernel / chpwd-equivalent-in-bash.md
Last active Mar 3, 2021
Create chpwd Equivalent Hook in Bash #bash #hook #zsh
View chpwd-equivalent-in-bash.md

There's not a complete hook system designed in Bash when compared with other modern shells. PROMPT_COMMAND variable is used as a hook in Bash, which is equivalent to precmd hook in ZSH, fish_prompt in Fish. For the time being, ZSH is the only shell I've known that has a chpwd hook builtin.

PROMPT_COMMAND

If set, the value is interpreted as a command to execute before the printing of each primary prompt ($PS1).

https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Bash-Variables

chpwd Hook in Bash

@laggardkernel
laggardkernel / trigger-chpwd-hook-on-startup.md
Last active Sep 17, 2020
Trigger chpwd Hook on Startup #zsh #hook #direnv
View trigger-chpwd-hook-on-startup.md

By default, ZSH hook chpwd is not triggered on shell startup. The tutorial here provides some ideas to fix this.

Trigger all chpwd_functions on startup

We can use a trick to define a function run only once on precmd and destruct itself automatically.

function _self_destruct_hook {
  local f
  for f in ${chpwd_functions}; do
    "$f"
@laggardkernel
laggardkernel / link-nvim-conf-to-vim.md
Last active Jul 26, 2021
Share confs between vim and nvim #vim
View link-nvim-conf-to-vim.md
@laggardkernel
laggardkernel / dynamic-umask-based-on-cwd.md
Last active Oct 16, 2019
Change umask based on working directory #zsh #direnv
View dynamic-umask-based-on-cwd.md

Default umaks on macOS is 0077, which is different with most Linux distributions.

There're pros and cons about this very decision made by Apple. For the good part, temporary files generate under /tmp, $TMPDIR are accessible by the user himself only. These locations could be alternatives to $XDG_RUNTIME_DIR.

While, the problem is that since the new files are accessible by yourself only, it's inconvenient to share files with other users.

It's easy to change umask globally with a LauchAgent, or change it for shells

@laggardkernel
laggardkernel / gpg-wrapper.md
Last active Jul 12, 2019
gpg wrapper/helper used for dotfiles #bash
View gpg-wrapper.md
  d_weechat:
    cmpignore:
    - '*/weechat_fifo'
    - '*/script/plugins.xml.gz'
    - '*/logs'
    dst: ~/.config/weechat
    src: .config/weechat
    trans: gpg-single "sec.conf"
    trans_write: gpg-single "sec.conf"
View cache-first.md
_env_init() {
  # init version manager without rehash on startup
  local SHELL_NAME="zsh"
  local init_args=(- --no-rehash zsh)
  local zshrc="$HOME/.zshrc"
  
  # For security on Linux
  [[ -n $XDG_RUNTIME_DIR ]] && local TMPDIR="$XDG_RUNTIME_DIR"
@laggardkernel
laggardkernel / catch-empty-string-into-array.bash
Created Jun 18, 2019
catch empty string into array #bash
View catch-empty-string-into-array.bash
# catch empty strings into an array using `read`
# normal array assignment isn't able to catch emptry string.
unset k 2>/dev/null
while IFS=$'\n' read -r item; do k+=("$item"); done < <(printf "\n\n\n")
echo ${#k[@]}
[[ -z ${k[0]} ]] && echo "empty string is catched"
f() {