Shell script to grab selected text and alter it? - shell

How can I make a shell script that will know where the caret is and grab selected text, so I can wrap the text in something? For example, this is a script from a Textmate bundle:
<${1:p}>$TM_SELECTED_TEXT</${1/\s.*//}>
It grabs the text and wraps it in open/close HTML tags. And it is variable so the second tag is mirrored as you type the first.
I want to make a script like this but outside of Textmate so I can use it in TextEXpander.
Does this make sense ? :)

$TM_SELECTED_TEXT is not an environment variable provided to scripts running outside of TextMate -- it is a variable that is set by TextMate and provided to scripts that are run as part of its snippet system. It is not even provided to the Shell bundles "Run Script" command.
What you want to do may be doable through other shell facilities (e.g., sed) so the functionality can be replicated by a shell script -- it depends on your usage scenario.

Related

How can I make my terminal text rainbow colored upon every boot up? (with lolcat)

I've been wondering how I could do some of these cool customization options for the terminal on a Mac and I came across lolcat.
However, I can't seem to find an answer as to how to add this into my bashrc (FYI: I'm using zsh now just in case that makes a difference as to which file to add my customizations in) I have tried what many others have suggested, which was just typing zsh | lolcat into the terminal to get rainbow output in the current session, but I was wondering how I can have this every time I start a terminal session.
Also, I'm not sure if this is a bug or if there's something wrong with my terminal settings, but when I use a command with lolcat, I get an output like this:
karenlee#Karens-MBP ~ % Documents
Downloads
Library
Movies
Music
Pictures
Postman
Public
38;5;48m
karenlee#Karens-MBP ~ % 38;5;48m
The colors look right, but as you can see, when I type the ls command on the command line, it disappears and the output also gets messy. It also seems like there's extra lines of 38;5;48m which are appearing. And it also seemed like many of the gems that are installed with lolcat have deprecated; is there another alternative to lolcat that plays nicely with macOS Catalina?
I made a shell extension for the world's fastest website generator that I make called Nift. It has an easter egg where you can turn on lolcat output for most things with lolcat.on (after starting the shell with eg. nift sh). You will even get rainbow output when pressing tab to get possible completion options, I doubt you get that with any other suggested solutions.
The shell extension is for f++ which is the in-built scripting language, which has these functions and these types available. But anything it doesn't recognise is run as a system call using the (probably primary/default) shell on your machine (hence calling it a shell extension in REPL shell mode).
Nift will look for a version of lolcat installed on your machine and otherwise use an in-built version of it which should be the world's fastest (it's near identical to my c++ implementation lolcat-cc which is the world's fastest). I highly recommend installing lolcat-cc (or another version of lolcat) on top of Nift though as otherwise you are frequently running the ~5mb Nift binary for basically all system calls, instead of a <1mb binary for lolcat.
f++ is somewhat of an interesting scripting language as well. Take this script for example which creates and deletes 100k empty text files. On my machine this runs in ~3.5 seconds whereas this Bash script doing essentially the same thing takes more like 3 minutes!! Some of the cool things you might already notice from that f++ script is you can expand variables inside strings, you can define variables with types, you can expand variables in to multiple parameters with function calls, you can have LOTS more than 10k input parameters for function calls (should be able to have millions!).
You can find some more information about the Nift REPLs (including shortcuts for different platforms) here.
If you need to define shell variables (not through f++ but the underlying shell) then you will need to do blocks of code using the sys/system function. You can also do blocks of code for Lua(JIT) and ExprTk similarly as they are both embedded in to Nift. You can use both Lua and ExprTk with accessing/modifying f++ variables as well..
So to get this shell (extension). Install Nift through a package manager or clone, make and install off GitHub. Enter nift sh in to your terminal to start the Nift f++ shell extension, then enter lolcat.on to turn on rainbow output.

Can I press enter twice to execute a command?

I'm using ZSH and I'm wondering if it's possible to map [enter, enter] to execute a command. Specifically, I'd like to move to my home directory when I press enter twice without typing any other text.
I don't want to make someone go through the trouble of writing me a script, but if any of y'all could point me in the right direction (zsh script/applescript/whatever it should be) and tell me if this is possible I'd really appreciate it!
I use iTerm2 on OSX, and zsh is my primary shell. Let me know if you need any more information!
Have a look at preexec in the section SPECIAL FUNCTIONS of the zsh manpage. If you define a function by this name, for instance in your .zshrc, and you have the history mechanism enabled (as is common in interactive shells), this function receives as argument the command line you have entered. If you just typed enter, the command will be the null string. You can catch this and then do whatever you want - for instance doing a chdir.

I have multiple applescripts where I need to replace the same text in each. Can I write a script?

I have multiple applescripts which I need to edit replacing the same text in each (it is an amended prefix to a file name). Is there a way to write an applescript to edit other applescripts. I have to edit over 700 scripts where an new prefix has been introduced.
Thanks in anticipation
Script Editor is itself scriptable, or you could use the command line osadecompile and osacompile tools. Main thing is to make sure whatever you're using to do the find and replace (simple text substitution, regular expression, whatever) only matches the text you intend it to match, so you don't screw up other parts of your code by accident.

Adding Helper Methods to Mongo Shell

Is there any way of adding "helper" methods to the mongo shell that loads each time you use it?
Basically whenever you want to query by _id, you have to do something like this.
db.collectionName.findOne({_id: ObjectId('THIS-IS-AN-OBJECTID')})
Whenever I'm going to be doing a lot of command line commands, I alias the ObjectId function to make it easier to type.
var ob = ObjectId;
db.collectionName.findOne({_id: ob('AN-OBJECTID')})
db.collectionName.findOne({_id: ob('ANOTHER-ONE')})
db.collectionName.findOne({_id: ob('ANOTHER')})
It would be pretty chill if there was a way of either running a specified piece of JS / add a chunk of code that runs each time mongo is pulled up from the shell. I checked out MongoDB's CLI documentation, but didn't see anything like that available, so I figured I would ask here.
I know there is a possibility of using this nefariously, so this might be a situation where it might be unsupported by the mongo shell by default. This might be a situation where we can create a helper bash script of some sort to launch the shell, then inject keyboard input to create the helper ob function? Not sure how this could be tackled personally, but would love some insight on how to do something like this, either natively or through a helper script of some sort.
If you want code to execute every time you launch the shell, then whatever you place in .mongorc.js will be run on launch:
.mongorc.js File
When starting, mongo checks the user’s HOME directory for a JavaScript file named .mongorc.js. If found, mongo interprets the content of .mongorc.js before displaying the prompt for the first time. If you use the shell to evaluate a JavaScript file or expression, either by using the --eval option on the command line or by specifying a .js file to mongo, mongo will read the .mongorc.js file after the JavaScript has finished processing. You can prevent .mongorc.js from being loaded by using the --norc option.
So simply define your variable association there.
You could also supply a file of your choice along with the --shell option to let the command know you want the shell opened on completion of any instructions contained:
mongo --shell file_with_javascript.js
But as mentioned, the .mongorc.js file would still be called (if present) unless the --norc option was also specified.

How to implement command autocomplete in myshell?

I am making my own shell in C. I know one can use the readline library to make use of autocompletion of paths using TAB, but it can only be used for autocompletion of paths but not commands.
For example, if I write clea and press the TAB key, it does not make it clear, nor does it give suggestions for any similar commands on pressing TAB twice. How to do it?
Your shell also needs to implement a PATH variable, which is a list of directories to search for non-builtin commands. For instance, if PATH has the value /usr/bin:/bin, then your completion function would complete cle to clear if either /usr/bin/clear or /bin/clear exists.

Resources