I am looking for a way to expand some predefined strings in the terminal when tab (or another key combination) is pressed. This is different from tab completion. My terminal already expands ~ to my home directory (I cannot remember how I figured it out, but I am pretty sure this is just an special case of what I am looking for).
Examples:
Assume you type cdd
and press tab, it is expanded to cd /your/home/folder/Downloads/
and the cursor is placed at the end of the line. So, you can type the name of a folder inside the Downloads
folder and run the command. A better example would be to expand sshu
to ssh |@a-pre-defined-host.foo
and place the cursor behind the @
sign to type in the username.
The is similar to code snippet expansion capability in many IDEs. Please don't say I can achieved the same thing through bash functions with parameters written in .bashrc
file.
What is the point?
So what's the point of using such a thing compared to a function call? Well, unlike many of you command-liners, I don't have a good memory. Even for the commands that I use frequently, I cannot remember the details such as the parameters. I usually search the web for things like "how to POST a json file using curl?". Well, I can create a function that accepts a file name, a host name, a port name and probably a few other parameters and do the POST. But, remembering the parameters and the order of them is not simpler than remembering the whole command then. With the snippet expansion, I only need to remember the name of the snippet, something like snppost
, or maybe I dont even need to remember the name of the snippet. I can type snp
and press tab to get a list of snippets, find the one that I am looking for. Press tab again to expand the snippet. And I have the command that I was looking for without searching the web and copy/pasting. I can do whatever customization that I like to the command and run it.
|@
supposed to be?|
is a control operator. Wouldn't it make more sense to type e.g.sshh
, then type the username and then press tab for adding@a-pre-defined-host.foo
? What is the advantage of first printing the host and moving the cursor backwards then? Furthermore the cursor would have to be in front of the@
, not behind.sshu
is a function, you can just typesshu $user
, without having to type Tab first, and you get the same result. The completion features in IDEs are designed for an environment where the completion itself needs to be stored in the source file, rather than a function that can be evaluated when the source file is read.cdd
: if you're using a custom command name, that custom command would presumably have the directory name built in. Same thing with the ssh example, best dealt with through aliases in.ssh/config
. You're trying to fit a square peg in a round hole, or as we say here, it's an XY problem.|
means the cursor location. Sorry if it was confusing.