Vim onoremap option

Alright guys, back to another feature that vim has to offer! Recently, I’ve been using :vert term <command> for some short commands to run instead of using :sh command and found that they have a mapping known as tnoremap which basically stands for Terminal Normal Remap.

I dived into the help for tnoremap and found a much more awesome option vim had which was onoremap!

It basically allows vimmers to define their own test objects, as an example: instead of doing:

nnoremap ciq ci'

we can do:

onoremap iq i'

The onoremap automatically makes it so that you can run diq, ciq, yiq and so on! Super fluent way of having a shortcut for the quote/'! It basically allows you to self-define your text objects which is kind of sweet!

In my .vimrc right now, I have:

onoremap iq i'
onoremap iQ i"
onoremap aq a'
onoremap aQ a"
onoremap , t,

This brings a whole lot of new functionalities that I never knew before this! so, d, gives the command of dt, or y, gives yt,

It is a little bit useful but overall the quote onoremap is the best use I have of it so far!

Try it out and let me know your onoremaps!

Tags: vim

Git Alias for Pull Request

My company uses Bitbucket to host our remote repositories. It becomes annoying when I need to manually visit the Bitbucket page for the repository to create a pull request. So, to make life easier, we can add the pr alias.

[alias]
  pr = \
    !repo=$(git ls-remote --get-url | cut -f 2 -d "@" | cut -f 2 -d ":" | cut -f 1 -d ".") && \
    branch=$(git symbolic-ref --short HEAD) && \
    open "https://bitbucket.org/${repo}/pull-requests/new?source=${branch}"

We use git ls-remote --get-url to get the remote url for the repository, then use cut to get the remote repository name. Note that the cut is meant for ssh urls like [email protected]:chunkhang/apple_sauce.git. https urls will need a different cut.

On the other hand, we use git symbolic-ref --short HEAD to get the current local branch name. With that, we can construct the url to create the pull request, and open it in the web browser.

Tags: git

Querying based on JSON Fields

Assuming in the orders table, we have a metadata column that stores JSON data, we can use @> to query based on fields in that data.

SELECT metadata
FROM "orders"
WHERE metadata @> '{"brand": "sun"}';
metadata
--------
{
  "name": "apple",
  "price": "100",
  "brand": "sun",
}
-----------------------------
{
  "name": "banana",
  "price": "200",
  "brand": "sun",
}

Tags: sql

Checking Return Code

Use $? to get the return code of the last command.

$ cat example.txt
$ echo $?
1
$ touch example.txt
$ cat example.txt
$ echo $?
0

Tags: shell

Git Housekeeping

I just got tired of seeing the huge list of branches whenever I do git branch. 99% of these branches are already merged and the remote branch already gone. So, once in a while, it’s good to do some housekeeping with your repositories. Note that everything below does not affect the remote repository at all. It only cleans up your local repository.


1. Prune local branches

Delete all local branches that have been merged, except for develop, master or staging

git branch --merged | grep -vE 'develop|master|staging' | xargs git branch -d

2. Prune remote branches

Delete all remote branches you still keep track of, which no longer exist in the remote origin

git fetch --prune

3. Clear reflog

Clear all reflog entries

git reflog expire --all --expire=now

4. Garbage collection

Run garbage collection process aggressively

git gc --prune=now --aggressive

To make it easy to run all the actions above, we can add everything under an alias in .gitconfig.

[alias]
  housekeeping = \
    !git branch --merged | grep -vE 'develop|master|staging' | xargs git branch -d && \
    git fetch --prune && \
    git reflog expire --all --expire=now && \
    git gc --prune=now --aggressive

Now, we can run git housekeeping every once in a while to feel good while your repository gets cleansed thoroughly.

$ git housekeeping
Deleted branch feature/cross_region_orders (was 62bfae1).
Deleted branch feature/cross_region_quotation (was 4d823d9).
Deleted branch feature/out_of_service_area_orders (was 80de5c3).
Deleted branch feature/remove_dhl_hardcode (was 265f644).
Deleted branch feature/store_google_response (was f9f8b71).
From bitbucket.org:pick-up/address_service
 - [deleted]         (none)     -> origin/feature/close_redis
 - [deleted]         (none)     -> origin/feature/cross_region_orders
 - [deleted]         (none)     -> origin/feature/discard_google_parts
 - [deleted]         (none)     -> origin/feature/remove_dhl_hardcode
 - [deleted]         (none)     -> origin/feature/th-region
 - [deleted]         (none)     -> origin/hotfix/2.25.2
 - [deleted]         (none)     -> origin/hotfix/2.29.1
 - [deleted]         (none)     -> origin/release/2.27.0
 - [deleted]         (none)     -> origin/release/2.28.0
 - [deleted]         (none)     -> origin/release/2.28.1
Counting objects: 2194, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2171/2171), done.
Writing objects: 100% (2194/2194), done.
Total 2194 (delta 1473), reused 614 (delta 0)

If you have multiple repositories, you can even consider using gita. With gita, we can do gita super housekeeping to perform housekeeping on all our repositories in one go.

Tags: git