DEV Community

Cover image for How To Create Files Faster in Visual Studio Code With a Command
Kayode
Kayode

Posted on • Originally published at blog.zt4ff.dev

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

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
Enter fullscreen mode Exit fullscreen mode
  • 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    
}
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)