0

I've been learning about the JavaScript library Sequelize and have noticed some boilerplate code for the sequelize-CLI. For example to generate a model you often use something like this:

npx sequelize-cli model:generate --name SingularModelNameHere --attributes attribute1Name:datatype, attribute2Name:dataType

My question is how do I go about adding these sorts of "boilerplates" to my Ubuntu so that I can easily use the tab-autocomplete feature and replace the boilerplate with the appropriate code? I've seen this tutorial, however they use the host command by itself and google.com, cyberciti.biz, and nixcraft.com are separate autocomplete arguments.

Is there a way to make it so that I could type npx sequelize-cli in my terminal and use tab to autocomplete the model:generate --name SingularModelNameHere --attributes attribute1Name:datatype, attribute2Name:dataType boilerplate?

I'd like to add other boilerplate commands too. For example,

npx sequelize-cli seed:generate --name <descriptiveName>

Then if I typed npx sequelize-cli s I could tab autocomplete.

  • Hi! Tab completion is really meant for completing things one option/argument at a time, which doesn't really match your use case. Can I suggest an alternative? Every command you type in bash in Linux can get saved to a "history" file. If you set that up, and make the history file size large (so *everything* gets saved), you can then use Ctrl-r to quickly search through it. For example `Ctrl+r model:generate` should find the last `npx ... model:generate ...` command and then you can hit Enter to run it. https://superuser.com/questions/7414/how-can-i-search-the-bash-history-and-rerun-a-command. – omajid Oct 01 '21 at 22:53

1 Answers1

1

There are a few ways to go about this, but first, it's important to understand that "completion" is a feature of your Linux shell not of WSL. If you haven't changed it, bash is most likely your shell, as it is the default in most distributions. Some common alternatives include zsh and fish.

While I generally don't "advertise" for my favorite shell in answers, I think it's appropriate in this case since one of the big features in the fish shell is its great out-of-the-box completion, suggestion, and abbreviation system.

As omajid mentioned in the comments, most shells can accomplish what you want to some degree through history search. But regardless of what shell you choose, I'd recommend adding fzf and setting up its shell integration. It can turbo-charge that Ctrl+R history search with an extremely fast pattern search.

You can type npx seq, hit Ctrl+R, and then filter based on flags that you'd used before. npx seed with Ctrl+R would show all your commands with npx and seed, regardless of where they appear in the commandline.

Even something as simple as seqseed Ctrl+R would find those lines.

That could handle your "multiple boilerplate" scenario. Again, this can be set up to work in Bash, Zsh, or fish.

But fish also has an out-of-the-box Autosuggestions feature that can help with this. As you type each command, fish will "suggest" a previously typed commandline that you can accept with or Ctrl+F. Or you can just accept one argument at a time from that previous commandline with Alt+.

Finally, fish has built-in Abbreviation support that could be used for this workflow:

abbr --add seqgen npx sequelize-cli model:generate --name SingularModelNameHere --attributes attribute1Name:datatype, attribute2Name:dataType
abbr --add seqseed npx sequelize-cli seed:generate --name 

Then, typing seqseed followed by the Spacebar will replace the seqseed with npx sequelize-cli seed:generate --name, leaving the cursor at the end where you can add the <descriptiveName>.

A few other commandline editing tips for this type of work. Some of these are found in other shells:

Key                         Function
Ctrl+W Deletes previous token. Fish is smart enough to use things like / and : as token separators by default, so if you use Ctrl+W after attribute2Name:dataType, it will only remove dataType.
Ctrl+ and
Ctrl+
Skip backward and forward over tokens
Alt+E or
Alt+V
Edit the current commandline in your preferred $EDITOR.
NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70