Several ways to input similar commands repeatedly

Time:2021-1-28

On the command line, you often need to input some shell code repeatedly, such as switching to a directory with CD, running NPM run local, or git commit. It is very troublesome to type these commands one by one completely every time. At this time, we need to find a way to solve the problem of repeated input and improve efficiency.

Of course, the most primitive is to find a text file and store the commands that you usually type in. Whenever you need to run these commands, you open the file, copy the selected content, and then paste and run it on the terminal. However, this is too primitive.

Use ctrl-r to turn out the history command

Using ctrl-r is a less primitive approach. After pressing ctrl-r in the terminal, the shell will wait for further input and find a matching one from the previously entered commands according to the input. Find the command line you need and hit enter directly. The effect is as follows
Several ways to input similar commands repeatedly

PS: the above is usedfzfSo the selected command will not be executed immediately after entering the carriage return. The native ctrl-r command does not support matching input characters in different positions, so it is recommended to try FZF.

Using alias

Compared with ctrl-r, alias evolves a little bit, because it no longer needs to cram so many characters into the command line – it allows end users to replace longer content with shorter content. For example, I wrote an alias for the command to log in to my local mysql

alias myroot='mysql -u root -p*******'

Moreover, alias is more like macro expansion, so you can add other content later, as shown in the following figure
Several ways to input similar commands repeatedly

Test and user entered after myroot_ Info is fed to the shell to execute along with the result of myroot expansion. After using alias, you only need to enter a shorter myroot each time.

Using functions

If alias is a macro in C language, then the functions supported by shell are functions in C language. Alias is always not suitable for scenarios where there are a lot of content to be input – the definition is particularly difficult to write, and it has no input parameters to speak of, and it is not suitable for dealing with the situation where there is a need for wonderful differences in duplicate content. Shell functions are very suitable for this situation. For example, I need to use thesdeditConvert it into a. PNG file, and then upload it to confluence and paste it into the design document. I hope the. PNG file has the same basename as the. SD file, so the following shell function can reduce the labor of repeated input

#The. PNG file with the same name is generated according to the. SD file
function sdpng() {
    basename=
    /usr/local/bin/sdedit -t png -o ${basename}.png ${basename}.sd
}

I just need to input the file name once, the effect is as follows
Several ways to input similar commands repeatedly

Using Alfred’s snippets function

AlfredWith a feature called snippets
Several ways to input similar commands repeatedly

It’s very similar to the above-mentioned alias, but it’s not handled by the shell itself. It’s an explicit input of a long string of characters. For example, I defined three phrases: GPD, GCT, and GPT, which will expand into

git push -u origin develop
git checkout test
git push -u origin test

The effect is as follows
Several ways to input similar commands repeatedly

Alfred’s snippets, like alias, can’t accept parameters, but it supports some placeholders, which can be expanded into dynamic content of some specific patterns. A more useful one is the {cursor} placeholder, which allows the cursor to be positioned here. For example, I can define such a string of expansion results

SELECT * FROM `user_info` WHERE `userId` = {cursor}\G

In this way, I can correctly locate the where statement after typing the corresponding phrase, and then directly enter the parameters to query. The effect is as follows
Several ways to input similar commands repeatedly

In addition to Alfred, there are other software to improve input efficiency through snippet, such asaTextDashBut I haven’t actually used it, so I won’t say more.

Postscript

No code is the fastest way to input code

Read the original

Recommended Today

020_CSS3

catalog How to learn CSS What is CSS History of development quick get start Advantages of CSS Three ways to import CSS Expansion: two ways of writing external style selector Basic selector Hierarchy selector Structure pseudo class selector attribute selectors Beautify web page elements Why beautify web pages Span label: for the text that needs […]