45

I'm .ok you're .ok: introducing folder profiles for bash and p...

 4 years ago
source link: http://www.secretgeek.net/ok
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

I'm .ok you're .ok: introducing folder profiles for bash and powershell

January 04, 2018ok

Summary: a .ok file holds a bunch of handy one-liners, specific to the folder it is in. It can be viewed or executed with a simple command. It makes you smarter and more efficient.

I work on lots of little projects and each one has its own nuances and tricks. Context switching from one project to the next is hard on the brain. Some projects are brand new, some are ten years old. Some are in Windows, some are in Linux.

Every time I go to resume work on a project there is that moment of confusion as the brain tries to reload a dusty tape of facts from cold storage.

To make it easier to recall all pertinent facts, I've started putting a little file called ".ok" in each project. This file holds any relevant shell commands I use with that project. (I have a powershell version and a bash version)

For example here's the .ok file from my "Today I Learned" project:

.\quick.ps1; .\deploy.ps1; # quick-build, prepare to deploy
.\build.ps1 # complete build, after which you can see preview
_book\index.html # see preview
.\wordcount.ps1 # custom word count script

If I type the command "ok" then I will see a listing of this file with a number against each line:

> ok
1. .\quick.ps1; .\deploy.ps1; # quick-build, prepare to deploy
2. .\build.ps1 # complete build, after which you can see preview
3. _book\index.html # see preview
4. .\wordcount.ps1 # custom word count script

And if I type "ok 3" then it will run line number 3, like this:

> ok 3
> _book\index.html # see preview

(And in this case I'll see a HTML preview of the current state of the book)

To make "ok" extra useful, I've setup my system so that every time I navigate into any folder (via cd), the command "ok" is run immediately. So if there is a ".ok" file I'll immediately see the available commands. (I did this by removing the "cd" alias, and making a custom "cd" function that does a set-location followed by a call to "ok")

If I want to add a command to the file, it is the work of a moment. I can do it immediately by editing the ".ok" file with "ed" or any standard editor.

Source code is here:

"ok" folder profiles for bash

And here:

"ok" folder profiles for powershell

Sometimes I write a thing and slowly stop using it. Other times I use it more and more, day after day, year after year, as happened with NimbleText. So far, "ok" seems to be one of those "use it more and more" solutions. So I think you would do well to give it a try.

Any issues, lemme know.

Next → ← Previous

My book "Choose Your First Product" is available now.

It gives you 4 easy steps to find and validate a humble product idea.

Learn more.

Dan F on January 04, 2018 19:44 sez:

This. Is. Sparta. Or brilliant. Both? Having done 2 such context switches so far today and it's not even 8am, it's like you wrote "ok" just for me. If you *did* write it just for me, I have a complaint, why didn't you deliver it yesterday :D

Seriously cool and useful tool LB.


lb on January 04, 2018 19:47 sez:

Great to hear Dan!

I love "ok" so much. It's only been a few weeks, but it's such a fundamental part of how I use a computer already.

I'd love to see someone else use it and would be great to see other ideas people add to it.


James on June 22, 2018 14:32 sez:

This is a really clever project! My only wishlist on it would be for it to work like .gitignore - it traverses up directories, with each parent directory adding details. So, ~/project/src could have .ok, which would add the first entries, then ~/project could also have .ok, with entries appended to the end... and so on, up the tree.

I'd put together a PR for this but I wonder what you think about the proposed design?


lb on June 22, 2018 20:04 sez:

@James interesting idea.
I’ve thought about this before but haven’t actively missed having it.
Note that before running a command from a parent folder, ok would need to switch to that folder, to get the correct context.
I’d have to try something like that out before I knew if it was something I used (versus the performance penalty, which should be only a small penalty... but still, ok gets run every time I switch folder)
I think I would be interested to see how I’d use it.


Doeke on June 28, 2018 09:58 sez:

Instead of a `cd` function, it might be better to use `PROMPT_COMMAND` environment variable. That's the `onBeforeDisplayPromp`-event handler of bash. For example: `export PROMPT_COMMAND=ok; $PROMPT_COMMAND` (you want to append ok to the variable, because some other program might use it; for example the magnificent `z` utility https://github.com/rupa/z).

I am actually amazed how natural this utility is. I do use this already is some sort. I have specialized utilties like `SSH` or `tasks` that show a list of numbered options, and when provided with parameter it executes this line. However, when typed without a number, it displays all entries, but it waits with a prompt for the number to be entered.

I would like to change this with ok:

$ ok #this would list and prompt
$ ok 2 #this would execute
$ ok -l #this would list only
$ ok --list #this would list too

I am aware this will break things. But it doesn't feel right to type `ok` twice. And with the `--list` option you only need to update your cd-function/PROMP_COMMAND definition only once.

Also: I have some more commits waiting. Do you prefer I save changes, or rather a PR per feature.


Doeke on June 28, 2018 10:33 sez:

@James: I thought about that too. Traverse down until you find an `.ok` file and use that. When nothing found, search up and show all the direct .ok-folders above the current folder and list them so you can navigate too them. It would be really cool.

However, I think I would never use it. The .ok files are (almost) always in the base of a git-repository. And most git-repositories share the same parent folder And I use the aforementioned z-utility to jump around.


Devon on November 08, 2018 11:52 sez:

I remembered reading this months ago, and am very glad I remembered that you were the source. This is so much better than searching through my command history because I only remember part of the command I need to run.

One oddity I have is that when I open a new terminal session I always see
----
/Users/devon/dotfiles/ok.sh:popd:8: directory stack empty
tip: "." (i.e. source) this file from your profile (.bashrc), e.g. ". /Users/devon/ok.sh"

arguments, if you need to customize (these can also be set via arguments/environment):
prompt Use the supplied prompt (e.g. prompt '> ')
prompt_default Prompt default when issueing running ok without arguments
auto_show Perform 'ok list-once' every time the prompt is shown (modifies $PROMPT_COMMAND)
verbose Enable verbose mode
quiet Enable quiet mode
-----

I am using zsh shell, jump for folder navigation, and iterm as my terminal. I am sourcing the ok.sh in my .zshrc file. Thought I would see if you had any insights before I started digging into shell scripting to see if I could figure it out, as it is pretty new to me.

Thanks again for making an awesome little utility and sharing it with the world.


Al Gonzalez on March 30, 2019 00:15 sez:

Just discovered this last week and think it can definitely be useful. I plan to try it out for a bit and introduce it for some projects at work.

BTW: I created a .NET Core Global Tool that tries to match the functionality of your '"ok" folder profiles for bash'. It's on NuGet at https://www.nuget.org/packages/ok-cli-tool/1.0.0

Thanks for this.


(By the way, I read every comment and often respond.)

Your comment, please?

Your Name
Your Url (optional)

Note: I may edit, reuse or delete your comment. Don't be mean.

Next → ← Previous


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK