Bash – Script to Create Files Using a Template

bashfunctionvim

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…

Best Answer

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.