Kayode Oluwasegun
Kayode Oluwasegun

Kayode Oluwasegun

How To Create Files Faster in Visual Studio Code With a Command

How To Create Files Faster in Visual Studio Code With a Command

Kayode Oluwasegun's photo
Kayode Oluwasegun
·Jan 26, 2022·

2 min read

Creating files using a mouse on Visual Code seems like a lot of work so I mostly use UNIX commands; mkdir to make directories and touch to make files.

But the touch command only works when the file directory(s) (destination) exist(s).

So we will create a bash-alias to create a file and create the directory(s) from the path if they do not exist.

For instance:

[command] src/components/App.ts

# should create
- src/
  |
    -- components/
         |
         -- App.ts
  • create a shell script and paste this code into it:
#!/bin/bash

function t() {
    regex="([^/]+\/)"
    f=$1
    temp="./"

while [[ $f =~ $regex ]]; do
    name="${BASH_REMATCH[1]}"
    temp=$temp$name
    mkdir $temp -p
    f=${f#*"${BASH_REMATCH[1]}"}
done

touch $temp$f    
}
  • enable running commands from the file by adding it to bash aliases using this command below:
source ./path/to/your_bash_alias_file

# mine, for instance
source /temp/scripts/my_bash_alias.sh
  • Now, we can make use of t to created files and automatically create the paths to the file even if they do not exist:
t src/components/App.js

Conclusion

The alias we created above is temporal only for testing the script out. To create an alias permanently add the alias to your .bashrc file.

And I also published an npm package to make use of the t command. by installing it globally with

npm install-g touch-alias.

Please, kindly star the repository if you find this helpful and leave a comment.

 
Share this