gabrielgio.me @ a1371930d5d3e44fc11078f697f82ac607476faa

feat: Add new post about shortcutting on fish
  1diff --git a/content/posts/2022-06-23-fish_command.md b/content/posts/2022-06-23-fish_command.md
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..c3c6b14a341b96ed09f43d5bb13ce51dc152e311
  4--- /dev/null
  5+++ b/content/posts/2022-06-23-fish_command.md
  6@@ -0,0 +1,123 @@
  7+---
  8+title:  "Shortcutting with fish shell"
  9+date:   2022-02-23
 10+tags: ['fish', 'linux', 'shell']
 11+---
 12+
 13+For some time now I have been using fish shell[^1] as my default shell and it
 14+has been a great experience. It has sane defaults and with almost no work I
 15+have a perfectly function shell with a great auto completion engine. I have
 16+also tried zsh before and although it is great shell it require some work to
 17+come to same level of fish, even after you include oh-my-zsh[^2] (which I'd
 18+highly recommend to use if you are starting to use zsh). One thing to keep in
 19+mind is that  fish is far from sh or bash compatible, so if you have something
 20+built in sh or bash it won't work with fish. Anyhow, what I want to show here
 21+is how much you can optimize your workflow with just a couple lines of shell.
 22+
 23+## The use case
 24+
 25+As an assiduous user of terminal I constantly need to jump from folder to
 26+folder between projects I’m working on whether to run an editor or to use git.
 27+
 28+Normally I'd just type `cd ~/Git/gabrielgio` and from there to another folder,
 29+but we can do better with fish (or any other shell actually) by assigning this
 30+action to a keystroke. However before we can add the shortcut itself lets first
 31+create a function to jump into the folder, to do so we will be using the `fzf`
 32+where the man page states:
 33+
 34+> a command-line fuzzy finder
 35+
 36+That will provide a nice way to search and pick from arbitrary list. You can
 37+get quick glance of how it work just type `find . | fzf` and it will open the
 38+fuzzy finder buffer interactively search for input keyword. To expose this
 39+functions of ours we are going to use a nice feature from fish which autoloads
 40+function[^3]  from all the `.fish` files from all folders listed in
 41+`$fish_function_path`. So we will be using the `~/.config/fish/functions`
 42+folder. Add a new file called `jumpin.fish` with the following content:
 43+
 44+```fish
 45+# ~/.config/fish/functions/jumpin.fish
 46+function jumpin
 47+end
 48+```
 49+
 50+Now lets plug `fzf` into that function.
 51+
 52+
 53+```fish
 54+# ~/.config/fish/functions/jumpin.fish
 55+function jumpin
 56+    set selected (ls ~/Git | fzf)
 57+    pushd ~/Git/$selected
 58+end
 59+```
 60+
 61+We are going to pip `ls` result into `fzf` then `set`[^4]  result of `fzf` into
 62+a variable. The return value of `fzf` is the value you have selected. As you
 63+can infer from the script I'm listing all my folder from `Git` folder where I
 64+store all my projects which are the folder I, most of the time, want to jump
 65+right in. It can be literally anything you may find useful, you may want to try
 66+to search using a broader scope by:
 67+
 68+```sh
 69+find ~/ -type d | fzf
 70+```
 71+
 72+Whatever fits you better, the end goal here is to get start customizing and
 73+optimizing your workflow so you can more comfortably moving around.
 74+
 75+Now, it is almost done we just need the check if the `selected` has value. The
 76+user can cancel the selection (e.g.: by pressing esc) and then the `selected`
 77+variable would be empty. To check that we just need an if:
 78+
 79+```fish
 80+# ~/.config/fish/functions/jumpin.fish
 81+function jumpin
 82+    set selected (ls ~/Git | fzf)
 83+    if [ "$selected" ]
 84+        cd ~/Git/$selected
 85+    end
 86+end
 87+```
 88+
 89+You will need to  reopen your terminal emulator or if you source it so the
 90+function become available.
 91+
 92+```fish
 93+# how to source it
 94+source ~/.config/fish/functions/jumpin.fish
 95+```
 96+
 97+Then type `jumpin` and let `fzf` do the work.
 98+
 99+Now we can jump to a folder even faster by assigning a shortcut to a function
100+and again fish comes to rescue to make our life easier. It provider a bind[^5]
101+function to bind (duh) sequence of characters to a function. Inside of you
102+`~/.config/fish/` there will be a `config.fish` with a function called
103+`fish_user_key_bindings` which fish will automatically execute. We will use
104+that function (as the name implies) to bind our command to a keystroke. To do
105+so use the bind function:
106+
107+```fish
108+# ~/.config/fish/config.fish
109+function fish_user_key_bindings
110+  # ...
111+  bind \ck 'jumpin'
112+end
113+```
114+
115+Once again reopen the terminal or source the configuration file. Now once you
116+press `Ctrl+k` it will pop the `fzf` list picker and after you select something
117+you will jump right into the folder. Due to the fuzzy finder algorithm you
118+won't need to type more then a couple of char making the whole process really
119+fast.
120+
121+This is just a jump start to using script to make your life easier. Shell
122+scripting is a powerful tool for a programmer and it will definitely pay some
123+dividends if you spend time to master it.
124+
125+[^1]: https://fishshell.com/
126+[^2]: https://ohmyz.sh
127+[^3]: https://fishshell.com/docs/current/language.html#autoloading-functions
128+[^4]: https://fishshell.com/docs/current/cmds/set.html
129+[^5]: https://fishshell.com/docs/current/cmds/bind.html