A dedicated and fun sharing series of Shell tips 🌝

preface

As a daily habit for command-line developers, the need to switch paths from CD to CD on the command line every day is common. Such a common requirement, if the daily use of CD only, there will be many pain points.

First, sometimes switching directories too deep can be a headache. As follows:

To switch from directory RSLV to directory react. You just have to play a long path. We can also write relative paths:Just as troublesome.

Second, switching work paths and then doing other things requires two separate commands, which is also a bit of a hassle. (Of course, this could just be an itch spot.)Uh huh. Is there a shorter way?

Finally, there are thousands of concurrent projects. Where can I remember so many work paths? And if there is a spelling mistake, it is sad to find that you have to waste a lot of time looking for it.

So, can’t help thinking, is there a better, more humane way? 🤔

Crazy comma – commacd

One day saw a solution commacd: github.com/shyiko/comm…

Switch paths by using crazy commas. 🤣

It provides three commands for roaming the terminal, which is very convenient. Respectively is:

  • “, “a single comma, used for forward fuzzy matching path roaming.
  • “,, “two commas, used to blur backward matching path roaming.
  • If there are three commas of “,,, “, fuzzy matching roaming can be performed before and after.

A comma looks forward

In a nutshell. The following is an example of a single comma:A comma, a partial path, and Commacd will blur the search for a matching path and switch over. Very convenient, so to speak.

Of course, here comes the magic. With very little information on des/w/ P/R, CommACd also landed very accurately on the working path. The following is an example:Of course, if it encounters multiple matches that are less certain. It doesn’t decide on its own, but gives the user options to choose from. The following is an example:

Look behind two commas

There is a past and there is a past. Two commas plus the destination path section toggle backwards. In this case, the nearest parent directory that best matches DES will be matched.When no target path is specified, it switches back to the project root path. Git is a working path with.git.

Look around three commas

Of course, if you want to make it even simpler, you can use three commas.

More possibilities for commas

For the command line, commands can be combined. Here you can refer to the file lookup I described earlier and preview the FZF command. Use:

command `, path`
Copy the code

Or use a more modern approach:

command $(, path)
Copy the code

Of course command can be open, code, whatever, essentially$(, path)A subshell will open after execution, pathAfter the command is executed, expand and replace it with the original command. For another example, combine this with the echo command:

The disk volume is as follows:

You can combine more commands in the same way, as you discover.

Any gate – RSLV

Crazy comma commands, but sometimes that doesn’t solve my occasional pain point of amnesia: not being able to recall even the slightest hint of the truth about the target’s path.

So, I wondered, is there a better way? There is no utility command that can set an alias for a common working path and then quickly switch through that alias.

It’s a bit like inventing domain names when you can’t remember IP addresses; In order to reduce repeated operations, App invented the “favorites” function; To solve the problem of finding common desktop applications, Windows invented “Send shortcut to desktop”.

The command line should do the same. Implementation will/Users/yanguangjie/Desktop/workspace/projects/react registered as @ the react, then CD @ the react so switch directly in the past.

Just do it!

By collating the requirements, I implemented an RSLV: github.com/sulirc/rslv tool command. Realized the CLI arbitrary door function.

implementation

Of course, the implementation of any gate is very simple, here is a brief description of the implementation principle. A database file is used to store the mapping between registered aliases and paths in this format:

@rslv => /Users/yanguangjie/Desktop/rslv
@react => /Users/yanguangjie/Desktop/workspace/projects/react
Copy the code

Implement an RSLV command to register and expand alias. The command manual is as follows:

Register the alias

Write to the database file when registering an alias. Query from the database file when expanding alias and expand the input to the standard output stream:After registration, you can view the list of registered aliases:

The alias jump

Now comes the most important step. Combine that with what we introduced earlierThe $()Command. The simplest invocation is as follows:Although it seems to have the effect I was looking for. But it feels wordy and not easy at all.

Export command combination

Shell needs, ultimately back to the shell to solve. Add a line to.zshrc or.bashrc:

export rcd() { cd $(rslv -e "The $1"); }
Copy the code

Restart the terminal.So far my dream of any door has come true. Quick switch between working paths with one click.

In addition to CD, many of my favorite commands, such as code, less, cat, open, etc., can be handled in this way.

export rcd() { cd $(rslv -e "The $1"); }
export rcode() { code $(rslv -e "The $1"); }
export rless() { less $(rslv -e "The $1"); }
export rcat() { cat $(rslv -e "The $1"); }
export ropen() { open $(rslv -e "The $1"); }
Copy the code

More and more

One final note. In addition to these two methods, there are many others. For example, if you open a terminal in vscode, the default is the working path of the current project.

However, left hand crazy comma, right hand arbitrary door, we are the FLASH in the CLI ⚡️.