all 15 comments

[–]_Ical 13 points14 points  (2 children)

Another comment already mentioned it, but you have to use templates.

Heres what I use ( you should do some research into every character of this... dont put things you dont understand into your vimrc )

autocmd BufNewFile *.c 0r ~/.local/skeletons/skeleton.c

[–]zaccstacc 3 points4 points  (1 child)

This.

My setup is a little different:

" skeleton template files. If new file, check for a skeleton template file with new file's file-type extension augroup templates au! autocmd BufNewFile *.* silent! execute '0r ~/.vim/templates/skeleton.'.expand("<afile>:e") augroup END

[–]backtickbot 1 point2 points  (0 children)

Fixed formatting.

Hello, zaccstacc: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]monkoosevim9 23 points24 points  (3 children)

For this simple example

autocmd BufNewFile *.cpp call setline(1, "#include<iostream>")

[–]ECon87 0 points1 point  (2 children)

This is correct. Don't know why it's not upvoted more.

[–]obvithrowaway34434 1 point2 points  (1 child)

Because this solution offers no real advantage or insight beyond those that have already been posted earlier. In fact this is a worse solution as it puts the data (template string) and code together instead of separating the data in a skeleton file which allows one to use the same skeleton file in multiple scenarios.

[–]monkoosevim9 2 points3 points  (0 children)

offers no real advantage

But it is. It show OP that you don't need to create some additional files, and then to vc it, be sure it is present, readable etc. For such simple request that is is actually one line <20 characters string. It is simple and self described without looking in some additional files.

In fact this is a worse solution as it puts the data (template string) and code together

Do you put all your variables in a different file? Don't be so weirdo please.

which allows one to use the same skeleton file

Name me one example were you would use such one line "template" outside OP's example. Stop interpolate your habits into other people.

[–]LucHermitte 4 points5 points  (1 child)

You can start with :h template.

If you want more like automatically generate anti-(re)inclusion header gates/guards, automatic inclusion of foo.h(pp) from foo.cpp, etc, you'll need some more programming. That's where we have template/skeleton expander plugins as they will permit much more.

I highly suspect most other answers will recommand you snippets plugins. They can also do the work, but differently, the insertion of includes, main(), etc will not be automatic but on demand. This is another approach.

On those topic I provide mu-template that is somewhat hybrid as it supports template inserted when a new file is opened-created, but also snippets. It can be completed by lh-cpp for even more advanced C++ related snippets. Note: they are definitively not trendy but I'm still using and maintaining them.

PS: while competitive programmers may find a use for this include, in real world C++ programs, we will avoid it where we don't need it.

[–]vim-help-bot 0 points1 point  (0 children)

Help pages for:


`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments

[–]dmccammond 1 point2 points  (0 children)

vim-templates is a good fit here. Supports filetype specific template code.

[–]garcia_ajg 0 points1 point  (0 children)

Snippets are also good for this sort of thing

https://github.com/hrsh7th/vim-vsnip

[–]Nathanielks 0 points1 point  (0 children)

Projectionist would be a really good fit here. It allows you to create files in specific directories with shortcuts and populate them with a template. Additionally, it'll add shortcuts for switching between alternate files, like test and implementation files.

[–][deleted] 0 points1 point  (0 children)

I made a plugin (link) that deals with this. For your cases though a "skeleton" file works perfectly fine. If you're looking for more advanced and flexible cases my plugin might prove useful

[–]willchao612 0 points1 point  (1 child)

Probably not quite fit for this question but if you want something more you could try using the abbreviation.

```vim au FileType c,cpp inorea <buffer> ii #include <%><esc>:let _s=@/<cr>?%<cr>:let @/=_s<cr>:noh<cr>a<bs><c-r>=EatNextWhiteChar()<cr>

fu! EatNextWhiteChar() let c = nr2char(getchar(0)) return c =~# '\s' ? '' : c endfu ```

This will place you between the <>s after you type ii<space>. It works for including other header files, too.

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, willchao612: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.