out-of-cheese-error / the-way

A code snippets manager for your terminal.
MIT License
354 stars 16 forks source link

(feature request) make better use of 'cmd' by allowing us to execute them. #97

Closed nerd190 closed 3 years ago

nerd190 commented 3 years ago

Hi! (me again) As a new user, seeing both add and cmd makes me think they are two different functions, but it appears they achieve the same thing, except that using cmd saves us one extra step as it automatically tags the language as 'sh', which is certainly nice to have, but not really a needed feature. To make better use of this function, seeing as they are shell commands, allow us to execute them directly or clip them, that way our snippets are snippets and our cmds are one-liners! On the same note, if they wasnt automatically tagged 'sh' anymore, cmds could be one-liners written in any non-compiled language, for example, I use this great one-liner as a quick way to setup python:

python3 -m ensurepip --default-pip && python3 -m pip3 install --upgrade pip setuptools wheel"

And this one sets up Perls better pkg manager:

curl -L https://cpanmin.us | perl - --sudo App::cpanminus

Would be nice to add these as cmds that I can execute directly, rather than copy and paste them back into the terminal to use. Currently, my one-liners are saved as aliases, which isnt good as they are rarely used yet sourced everytime I open a new shell, when I do finally need to use them, I need to dig through a huge page of aliases, with the-way, I wouldnt need to do that anymore, plus aliases dont have metadata like the-way provides! (title, tags, description etc). Thank you once again!

Ninjani commented 3 years ago

Hi! This is a great idea but is something that's better to add as a shell function within the initialization script of the shell (e.g. bashrc, zshrc etc.).

Two points:

cmd is actually different from new in two ways - one is as you mentioned, that it auto-adds the language as sh, but the other difference is that it takes the command as the first argument. This allows the-way cmd to easily be used in a shell script (as described in this section of the README) to quickly save the previous command run in the shell. You could bind the cmdsave command to a keyboard shortcut too.

I recently added the --stdout option which brings us closer to what you have in mind. Again this needs a different shell function depending on whichever shell you use but here's what I could come up with for bash and zsh:

This lets you type cmdsearch in the terminal (or use CTRL-x CTRL-r in bash) to search the-way for command-line snippets. Pressing Enter will already paste the selected command onto the terminal, ready to run (i.e. you don't have to paste it yourself). If you want instead to run the command directly you could replace the last line of the function with sh -c $BUFFER but this seems a bit riskier to me as I'd want to see the command I'm running (in the case of shell snippets with variables that you would fill after selection).

Let me know if this works out for you and I'll add this to the README as well (maybe after figuring out how to do this in the fish shell). I think this makes more sense than trying to run a command from within the-way as this would depend a lot on the terminal environment being used(for instance you could be using the-way from inside vim or a special shell).

Cheers!

nerd190 commented 3 years ago

This makes perfect sense. Now I am aware of cmd being useful for scripting/binding... I agree totally! Closed this as (regardless whether the above functions work or not (I'll try them now!)), cmd serves a great purpose, and that shouldnt be changed. Thank you for going a step beyond and writing some functions! they are very useful, I believe that they should be in the readme somewhere for others to use, the-way makes for a really nice way to add meta/execute those rare/forgetful commands! Thank you.

Ninjani commented 3 years ago

Great, I'll add them to the README, thanks!