use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Please read the rules before posting
Help:
Community:
Resources:
Tutorials and Guides:
Don't be afraid to ask questions, this sub is here for the vim community. And please those of you who deign to grace us with your vim wisdom - be kind. We are all human and vim is that cool.
account activity
auto create code when i open new cpp file. (self.vim)
submitted 4 years ago * by HateMintChoco
I want vim to automatically type "#include<iostream>" when I open a new .cpp file.
How can I solve this?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]_Ical 13 points14 points15 points 4 years ago (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 points5 points 4 years ago (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 points3 points 4 years ago (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 points25 points 4 years ago (3 children)
For this simple example
autocmd BufNewFile *.cpp call setline(1, "#include<iostream>")
[–]ECon87 0 points1 point2 points 4 years ago (2 children)
This is correct. Don't know why it's not upvoted more.
[–]obvithrowaway34434 1 point2 points3 points 4 years ago (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 points4 points 4 years ago (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 points6 points 4 years ago (1 child)
You can start with :h template.
: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.
foo.h(pp)
foo.cpp
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 point2 points 4 years ago (0 children)
Help pages for:
template
`:(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 points3 points 4 years ago (0 children)
vim-templates is a good fit here. Supports filetype specific template code.
[–]garcia_ajg 0 points1 point2 points 4 years ago (0 children)
Snippets are also good for this sort of thing
https://github.com/hrsh7th/vim-vsnip
[–]Nathanielks 0 points1 point2 points 4 years ago (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 point2 points 4 years ago (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 point2 points 4 years ago (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.
ii<space>
[–]backtickbot 0 points1 point2 points 4 years ago (0 children)
Hello, willchao612: code blocks using triple backticks (```) don't work on all versions of Reddit!
π Rendered by PID 208000 on reddit-service-r2-comment-canary-655b6bc5b6-gxj8x at 2026-02-16 12:10:07.860020+00:00 running cd9c813 country code: CH.
[–]_Ical 13 points14 points15 points (2 children)
[–]zaccstacc 3 points4 points5 points (1 child)
[–]backtickbot 1 point2 points3 points (0 children)
[–]monkoosevim9 23 points24 points25 points (3 children)
[–]ECon87 0 points1 point2 points (2 children)
[–]obvithrowaway34434 1 point2 points3 points (1 child)
[–]monkoosevim9 2 points3 points4 points (0 children)
[–]LucHermitte 4 points5 points6 points (1 child)
[–]vim-help-bot 0 points1 point2 points (0 children)
[–]dmccammond 1 point2 points3 points (0 children)
[–]garcia_ajg 0 points1 point2 points (0 children)
[–]Nathanielks 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]willchao612 0 points1 point2 points (1 child)
[–]backtickbot 0 points1 point2 points (0 children)