You are not logged in.
I finally got vim just the way I like it, and I plan to write papers using LaTeX now that I have seen the light. I created a .tex file called documentTemplate.tex with all the options set up for the title, author, font, margins, etc. just the way I like it. What I would like to do is be able to execute a command that opens this document with vim and then be forced to save it as a different file name. I have made my documentTemplate.tex read-only, and when I open it with vim, it gives me the usual warning and then allows me to edit the document without actually writing over it.
The problem I have experienced is after writing the file with a new name using ":w ~/Documents/<filename>", I am not able to save over that document with ":w". Every subsequent ":w" tries to save what I have over documentTemplate.tex, but not the new file name. How can I make this work correctly?
If there is a better way to use .tex documents as templates with vim please let me know. I am not using vim-latex because it seems to be dying/not that good/unnecessary/whatever. What are the other LaTeXers doing? At least some of you guys must use vim!
Thanks,
Allamgir
Last edited by Allamgir (2009-08-17 16:19:22)
дɭɭɑӎɠїɾ
Offline
would this work?
#!/bin/sh
# note "$*" vs "$1" to handle spaces as input
case "$*" in
*.tex) filename="$*" ;;
*) filename="$*.tex" ;;
esac
template="/path/to/documentTemplate.tex"
cp "$template" "$filename" && vim "$filename"
exit 0
then these /should/ all work:
newdoc.sh SomeDocName
newdoc.sh Some Doc Name
newdoc.sh Some DocName.tex
note: untested
/edit: typo.
Last edited by brisbin33 (2009-08-17 16:12:12)
//github/
Offline
Wow! That worked perfectly! Thank you so much!
brisbin33, you always seem to come to my posts and have a magical script to solve the problem. You must be a shell script guru or something!
дɭɭɑӎɠїɾ
Offline
Or add something like this in your .vimrc (much cleaner imo):
if has('autocmd')
autocmd BufNewFile * silent! 0r $HOME/.vim/templates/%:e.tpl
endif
Where in this case $HOME/.vim/templates/tex.tpl is your latex template. Now everytime you open a file that has a .tex extension (or possibly filetype, I'm not sure what is used here) your template will be loaded. You can obviously modify it to whatever suits you the best.
Note that this is taken straight out of my .vimrc, and it is set up to handle multiple template languages, not just tex files. Adding a $HOME/.vim/templates/py.tpl automatically adds python templates too.
Last edited by rson451 (2009-08-17 16:33:42)
archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson
Offline
Oooooo that looks nice. I'll definitely keep that in mind, but right now it seems like keeping this script in ~/.bin and running the command with the filename simplifies the usage.
дɭɭɑӎɠїɾ
Offline
You must be a shell script guru or something!
ahem, devs? can we get that member tag changed? sed s/Member/shell\ script\ guru/g
haha thanks for the compliment though, glad it's working.
i find rson's solution cleaner by the way, but BufNewFile * means it'll apply the template to every file opened in vim no? maybe BufNewFile *.tex?
//github/
Offline
BufNewFile * means it'll apply the template to every file opened in vim no? maybe BufNewFile *.tex?
Yeah, but leaving it as * means that you can simply create a {extension}.tpl in that directory and never have to set up loading that template. There is no noticable difference with it on all files or just specific filetypes.
archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson
Offline
But with that method, what will happen if I want to edit a .tex file. Let's say I'm working on a report and edit it over the course of a few days. Won't I run into template confusion problems if I try to open my report TeX file and edit it?
дɭɭɑӎɠїɾ
Offline
But with that method, what will happen if I want to edit a .tex file. Let's say I'm working on a report and edit it over the course of a few days. Won't I run into template confusion problems if I try to open my report TeX file and edit it?
BufNewFile means it'll only load the template when you initially create the file, BufNewFile BufReadFile would do what you describe above. (if i understand it correctly).
//github/
Offline
Oh I see. Then either method would work just fine!
дɭɭɑӎɠїɾ
Offline
I'm surprised nobody has mentioned using :saveas in vim instead of :w.
Offline