Snippets are one of my favorite parts of programming. The shear endless depth of command line tricks and customizations is so exciting! Here is a collection I maintain for myself to comeback to.

Also navigable by list of snippet types here.


watch command -- 27.01.2020 %

Currently at work we occasionally have to redeploy pods that have the same image tag but different SHA hashes. The problem stems from the k8s deployments not picking up the changes and thus a manually killing of the production pods is required. When they are restarted the SHA is checked and new image pulled, during this process I like to watch along at the running pods to make sure things are going smoothly.

I use to use the built in watch argument of k8s get pods command -w:

$ kubectl get pods -n shop -w

But this produces a really cluttered terminal output so I started just running the command on loop

$ while true; do; kubectl get pods -n shop ; sleep 5; done

Then a coworker showed me the watch command which has a much cleaner interface and outputs to a much clearer less/more like non scrolling output.

$ watch -n 5 'kubectl get pods -n shop'

The watch command is availble via brew

$ brew install watch

source:

https://gitlab.com/procps-ng/procps/blob/master/watch.c

\- [ watch ]

zsh git plugin -- 27.01.2020 %

I use git exclusive from the command line and while it’s interface is very clear since I spend so much time I’m willing to trade some of that clarity for speed. ZSH has a great plugin for really fast git tasks. Two of my favorite are pushing while setting a upstream branch and adding patches.

GPSUP, git set upstream and push shortcut

I push new git branches a few times a day and my previous work flow was to:

$ gp
fatal: The current branch NOTICKET-XXX has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin NOTICKET-XXX

followed by the classic fuck command:

$ fuck

which then runs the proper push command setting upstream. News flash! This call all be accomplished with a single plugin command:

$ gpsup

GAPA, git add patch

I like small commits and the key to that for me is adding by patch. This short cut turns

$ git add --patch

to

$ gapa

Can’t recommend the zsh git plugin enough! There are too many shortcut commands to be worth memorizing but I think gpsup and gapa are worth it. For a big list of commands it checkout the docs here

\- [ git, zsh ]

for loops in bash / zsh shells -- 12.01.2020 %

Looping directly in the shell is something I do at lease once a week and it’s a great time saver. Here I want to run through a concrete example of how to use for loops to mass move and rename files.

During process of getting this snippet section of my site off the ground I realize I had put a bunch of markdown files into named directories when I could have just named the files better and done without the directories. What I needed to do was mv the files out of these dirs and rename them to the name of the dir plus add the “.en.md” extension.

Here is the old directory structure:

$ tree
.
└── snippets
    ├── _index.en.md
    ├── aws-cloud-front-inval
    │   └── index.en.md
    ├── aws-s3-sync
    │   └── index.en.md
    ├── ffmpeg-screen-casts
    │   └── index.en.md
    ├── find-folder
    │   └── index.en.md
    ├── git-better-git-add
    │   └── index.en.md
    ├── git-log-grep
    │   └── index.en.md
    ├── git-move-branch
    │   └── index.en.md
    .
    .

To start with I preformed the operation in question a single time to prove it works:

$ mv ./who-is-using-that-port/index.en.md ./who-is-using-that-port.en.md && rm -r ./who-is-using-that-port

Then I copy this command to the clipboard for later and use command history pull up a previous for loop command and switch the shell command entry external editing in vim mode, for me <C-x><C-e>. As we are essentially writing a quick bash script in-line we need vim powers! While in vim if you need the file list a quick :r !ls does the trick. These little efficiencies like using command history are not just faster but make it so you don’t have to remember the bash syntax.

Resulting vim buffer with the for loop:

for x in \
aws-s3-sync \
ffmpeg-screen-casts \
find-folder \
git-better-git-add \
git-log-grep \
...
git-move-branch \
vim-window-resize ; do mv ./"$x"/index.en.md ./"$x".en.md && rm -r "$x" ; done

Resulting new file structure:

$ tree
.
└── snippets
    ├── _index.en.md
    ├── aws-cloud-front-inval.en.md
    ├── aws-s3-sync.en.md
    ├── ffmpeg-screen-casts.en.md
    ├── find-folder.en.md
    ├── git-better-git-add.en.md
    ├── git-log-grep.en.md
    ├── git-move-branch.en.md
    .
    .

Perfect, all files have been renamed properly and the empty directories deleted. This technique has lots of other applications besides moving and renaming files. Earlier this week while debugging api at work I dumped bunch of json responses into a file so I could search for translation key.

$ for x in \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx \
https://api.poeditor.com/v2/download/file/xxx ; do curl $x >> /tmp/translations.json; done
\- [ zsh, built-ins ]

custom placeholders solution -- 12.01.2020 %

This little bit of magic I believe I picked up from watching a Luke Smith video. The point is to leave placeholders in the form of the text “<++>” in your code/writing then quickly snap to and fill them in later.

" placeholder magic
nnoremap <Space><Space> <Esc>/<++<CR>"_c4l
nnoremap <Space>n <Esc>l/<++><CR>h
" always fill p reg with <++>
:autocmd VimEnter * :call setreg('p', '<++>')
  1. Thanks to the vim enter hook the magic placeholder text is always in my p register so its easy to put with <"><p>
  2. Then to jump to the next placeholder and immediately start editing it with a simple <Space><Space>
asciicast

placeholders demo

source:

\- [ vim ]

super powers of the arg list -- 11.01.2020 %

Vim help:

The argument list *argument-list* *arglist*

If you give more than one file name when starting Vim, this list is remembered as the argument list. You can jump to each file in this list.

If you have been using vim a while you will be comfortable with the buffer list but often it’s full of random files, terminal sessions, and other none file buffers, this is where arglist comes in. It starts out as list of files that were initially opened when the vim session was launched ex:

nvim ./a.txt ./b.txt ./c.txt

would start vim with three buffers in both arglist and buffer list. You can cycle through the arglist with files via :next, :prev. Further files opened will not be automatically added to the arglist so it’s essentially a clean sub list of the buffer list. Helpful commands include :rew to jump back to the first file you edited this session. Also :all which opens all buffers of the arglist in splits.

Where I find the arglist particularly powerful is for mass file edits. Here is an day to day example:

  1. get files in questing into the arglist either pipe files to vim or from in vim override initial arglist.
  • from cli
$ rg -i 'snippet_typ.*vim' --files-with-matches | xargs nvim
  • from inside vim (harder because no autocomplete)
:args `rg -i 'snippet_typ.*vim' --files-with-matches`
  1. now lets change all the posts front-matter to draft = true
:argdo %s/draft: false/draft: false/g | update

thats it all the files have been modified and saved. Its best to use this kind of thing with version control so during an interactive git add you can verify each modification.

For more complex edits I load files into the list a similar way then make quick manual and move to next file with :wn which also saves.

\- [ vim, search ]

custom ripgrep -- 11.01.2020 %

In the past I used full plugin level text searching solutions the likes of :Rg, :Grep, :Ag from the vim grep plugin. Recently I’ve found it faster and more customizable to just run :term do my searching from there and then opening file under cursor in new split with <C-W><f>. Doing the searches directly in terminal builds more on all the tricks I’ve devolved outside of vim world. However launching terminals all the time is also has it’s pain points, so now I’ve come up with a simple key mapping to run searches in a new scratch window without having to go full term.

yank the search term then <Leader><s> pulls up a new scratch buffer with the search pre-filled but with the ability to still edit and add further customisations.

nnoremap <Leader>s :Sscratch<CR>:read ! rg -i <C-R>"

here I use the technique to search for all snippet files tagged with vim

asciicast

demo of using the jump list

plugins:

\- [ vim, search ]

remapping ability to jump -- 11.01.2020 %

Moving efficiently in vim is a massive topic but recently I just wanted to take advantage of the jump list more but by xQuartz / xterm mac terminal setup meant the default key mappings we being intercepted as promote / demote pane. Simple remap did the trick

nnoremap <Leader>i <C-i>
nnoremap <Leader>o <C-o>

In the following demo I open this very post then navigate to my .nvimrc and yank the code remapping snippet. With the snipped yanked, using this using jump list via <C-O>, I jump back to the markdown file I was editing and put.

asciicast

demo of using the jump list

\- [ vim ]

replae <s> with spell and nohl -- 11.01.2020 %

When is the last time you used the <s> key in vim? May not know what it even does?

The vim help pages lists it as substitute:

4.2 Substitute						*:substitute*
							*:s* *:su*
:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]

In real use I find this visual selection putting or using <c> along with movement keys much better for substituting text. This leave the s key in normal mode open to be remapped! Here are two mappings I have so far:

  • turn off highlighting with a quick <s><s>
nnoremap ss :noh<CR>
  • turn on spell check in a given language
nnoremap <leader>se :setlocal spell spelllang=en<CR>
nnoremap <leader>sd :setlocal spell spelllang=de<CR>
\- [ vim ]

resizing vim windows with arrow keys -- 11.01.2020 %

If your comfortable in vim you only use the j,h,k,l movement keys leaving the arrow keys open to do something more useful like window resizing!

" Remap arrow keys to resize window
nnoremap <Up>    :resize -2<CR>
nnoremap <Down>  :resize +2<CR>
nnoremap <Left>  :vertical resize -2<CR>
nnoremap <Right> :vertical resize +2<CR>
asciicast

demo window resizing

\- [ vim ]