2

I just wrote a function in my ~/.bashrc that will let me create a folder for a new website with one command. The function looks like this:

function newsite() {
  mkcd "$*"  # mkdir and cd into it
  mkdir "js"
  mkdir "imgs"
  touch "index.html"
  touch "main.css"
  vim "index.html"
}

Now what I would like to do is, instead of just touching index.html and main.css I'd like to create basic template files for index.html and main.css problem is I have absolutely no idea how to do that. I don't know much about writing to files using bash commands. Typically I'd just open the files in vim and go to town but I'd like to have something already started when I get into vim...

4
  • 1
    It'd probably be easier to save your templates in some directory site_template/, and just do a recursive copy from the template folder to the new site folder, followed by whatever cd and vim commands you want.
    – jw013
    Feb 10, 2012 at 4:15
  • @jw013 AH Yes super simple! Thank you very much :D Make that an answer and I'll accept it. Feb 10, 2012 at 4:22
  • Looks like @HaiVu posted an answer already, so need for me to make another :)
    – jw013
    Feb 10, 2012 at 4:38
  • You're better off using a build tool like scons to do this. Feb 10, 2012 at 5:25

2 Answers 2

6

I like jw013's idea:

mkdir -p ~/site_template/{js,imgs}
# Creates all the files in this directory: index.html, main.css, ...

Now, when it's time to create a new site:

cp -r ~/site_template ~/my_site

That would be much easier. Plus, you can edit your site template files any way you like.

3

jw013's idea and HaiVu's answer are both correct. However for the sake of completeness for anyone who comes upon this question wanting the answer, here it is;

function newsite() {
  mkcd "$*"  # mkdir and cd into it
  mkdir "js"
  mkdir "imgs"
  cat > index.html <<'EOI'
<html>
<head>
</head>
<body>
</body>
</html>
EOI
  cat > main.css <<'EOI'
body {
 font-family: Arial;
}
EOI
  vim "index.html"
}

The <<'EOI' thing is called a heredoc, most scripting languages have them.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .