-1

So, I really like the fact that text editors such as Vim allows you to auto-write text (most of the times shebangs) when creating a new file with a specific file extension.

However, I'd like to know if there's any way to do it from the terminal using the touch command.

For example, I want to auto-generate shebangs for my Python script I just created from the terminal, like below:

$ touch test.py                   # create file and auto-generate
$ cat test.py                     # see the contents to confirm
#!/usr/bin/env python
# -*- coding: utf-8 -*-

I know I can just create a bunch of auto-generating text variables in my environment such as PY_SHEBANG="#\!/usr/bin/env python\n# -*- coding: utf-8 -*-" and just do:

$ echo $PY_SHEBANG > file.py

But the thought of going through so much trouble just messes with my head.

That being said: is there any way to configure the terminal/shell so that it can recognize the filetype of the file I'm creating, and append text to it automatically based on filetype, with a single touch command?

NOTE: I am using zsh and oh-my-zsh.

Eddo Hintoso
  • 1,442
  • 14
  • 22
  • 1
    The `touch` command changes the modification time of a file, coincidentally creating an empty file if the name doesn't already exist. It doesn't edit the content of files. No; `touch` is not the right command to do the job you're seeking. I have a `newdoc` script that copies a template into the named file (from a directory with per-suffix templates in it). You could create yourself something similar, or find one that someone else has already created. – Jonathan Leffler Feb 20 '16 at 15:45
  • Hey @JonathanLeffler, just got an answer that worked perfectly, but I was curious as to why you believe `touch` is not the right command to do the job. What does it have to do with changing modification times and such? What are the implications of using aliases to do the job as recommended from the below user? – Eddo Hintoso Feb 21 '16 at 08:57
  • As to why I think [`touch`](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/touch.html) changes the modification times — the POSIX summary of the command says '`touch` - change file access and modification times'. Using an alias as shown below is more or less equivalent to using a shell script to do the job. I don't like cluttering my shell with aliases and functions that I use a few times week at most; I keep my environment, functions and aliases minimal so that the shell has as little to do when started as possible. I'm in a minority in this, though. – Jonathan Leffler Feb 21 '16 at 09:04
  • Okay, I see where you're coming from. I'll keep that advice in mind for future reference. Just curious: how would you have done it? Do you just create the file with `touch` as per usual, then run the `newdoc` script on the file - making it two separate commands? Where do you put the `newdoc` script? – Eddo Hintoso Feb 21 '16 at 09:12
  • 1
    I just run `newdoc`: `newdoc file.c file.h file.sql file.cpp file.y file.l …` (and a whole raft of extensions for esoteric stuff that you'd not recognize). I keep it in my `$HOME/bin` directory, along with the other commands I've written over the last 30 years or so. There are somewhat over 500 commands and scripts in there; there are 850 files in the RCS directory — so there are 350 scripts that I don't currently use on this machine (for a variety of reasons). Where else would you keep your own private commands? I don't put them in `/usr/local/bin` because I don't want to intrude on others. – Jonathan Leffler Feb 21 '16 at 09:20
  • Ah, gotcha. I too put various things in my `$HOME/bin` directory. I'm probably going to let this one slide with an alias but I'll try to avoid cluttering my config files in the future and write my own shell scripts to put in `$HOME/bin`. Thanks for the veteran advice and taking the time to explain to an amateur. – Eddo Hintoso Feb 21 '16 at 09:25

1 Answers1

3

Sure you can. Simply use aliases and substitute touch for a function we write ourselves. However I would propose using the alias etouch (enhanced touch) so if you decide to use the touch command normally, you can.

Since you are using zsh, it makes things even easier (but I'd imagine it'd be similar for bash) In your .zshrc (or .bashrc, I wouldn't exactly know), write a function that checks the filetype and inputs whatever, like the one below:

# function for auto-generating shebang
function enhanced_touch () {
    if [ -f $1 ]; then
       echo "$1 already exists"
       return 0
    fi
    if [[ $(echo -n "$1" | tail -c 3) == ".py" ]]; then
        echo "#!/usr/bin/env python\n# -*- coding: utf-8 -*-" > $1
    else
        touch $1
    fi
}

alias etouch="enhanced_touch"

Obviously, I have only customized the function for recognizing python files just as you requested. But I'll leave the other filetypes for you to write.

Also, don't forget to source .zshrc. Good luck!

jagraham
  • 56
  • 2