Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Vim Howto: automatic file skeleton/template creation
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
no_hope
Guru
Guru


Joined: 23 Jun 2003
Posts: 479
Location: Madison, WI, USA! USA! USA!

PostPosted: Sat Jul 21, 2007 9:34 pm    Post subject: Vim Howto: automatic file skeleton/template creation Reply with quote

If you are not sure what I am talking about, run the following and bask in the glory of Vim! (in the unlikely event of nothing happening, emerge app-vim/gentoo-syntax and retry)
Code:
vim foo.ebuild


Have you ever been in a situation where you had to type the same thing whenever you created a new file of some kind? For example, whenever I start writing a new Python file, they always look like this:

Code:
#!/usr/bin/env python

def main():
      pass

if __name__ == "__main__":
      main()


It used to be that file creation with "gvim foo.py" invariably would be followed by typing the above template. No more. Now Vim does it for me. This is my story.

edit: what comes below is not the proper way to create skeletons. Next post shows how to do it properly. I didn't remove example below in case somebody finds it useful for other purposes.

The simplest way (I know of) to get vim to fill out a new file template/skeleton is for you is to make your ~/.vim/filetype.vim look something like this:
Code:

" The following prevents the file from being executed multiple times
" I am not sure why that happens
if exists("did_load_filetypes")
   finish
endif

" below is a function definition.
" '!' means to that this function will overwrite functions with the same
" name if they exist
fun! PySkel()
    " 0 put means put on the very first line. otherwise vim will start
    " with second.
    " You have to use single quotes;
    " Most of escape characters didn't work for me
    0 put ='#!/usr/bin/env python'
    put =''
    put =''
    put ='def main():'
    put ='   pass'
    put =''
    put ='if __name__ == \"__main__\":'
    put ='   main()'
endfun

"defining a new 'auto command' group
augroup newPy
        "when buffer for a new file that matches *.py is created,
        " run  PySkel()
   au BufNewFile *.py call PySkel()
augroup END


The example above is suboptimal in many ways, but it's the best I can do and it's 100% functional.

Feel free to contribute suggestions and templates :)


Last edited by no_hope on Wed Jul 25, 2007 9:30 pm; edited 2 times in total
Back to top
View user's profile Send private message
Unne
l33t
l33t


Joined: 21 Jul 2003
Posts: 616

PostPosted: Mon Jul 23, 2007 1:57 pm    Post subject: Reply with quote

Another way to do this is to create a file that you want to use as a skeleton, e.g. make a ~/.vim/skeleton/skeleton.py that contains the exact text you want to use. Then a single line in your ~/.vimrc something like:

Code:
au BufNewFile *.py 0r ~/.vim/skeleton/skeleton.py


See also :h skeleton. This has the benefit of being somewhat more readable and maintainable in that your skeleton is a real .py file instead of a bunch of inlined strings.

One thing I don't like is that 0r places the cursor on the first line, which generally isn't where you want to start typing. You can always move the cursor to where you want it (e.g. last line) via

Code:
au BufNewFile *.py 0r ~/.vim/skeleton/skeleton.py|norm G

_________________
Obligatory hompage link.
Back to top
View user's profile Send private message
truc
Advocate
Advocate


Joined: 25 Jul 2005
Posts: 3199

PostPosted: Wed Jul 25, 2007 6:38 am    Post subject: Reply with quote

Thank you!

I've been wondering how to do this for a long time, but never took to time to search!

This will save me some typing!
_________________
The End of the Internet!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum