Automatic Syntax/Headers in Vim for C++ Files

Automatic syntax/headers in vim for c++ files

One common method for doing this is described at :help template.

Another option would be to use a snippets plugin (like snipMate or UltiSnips). These don't automatically insert the code when you open a new file, but you can create various snippets that will expand to portions of the template you describe and let you fill in the portions that vary (like the header in an #include <...> statement).

C.Vim plugin, adding Automatic Header to *.h file

Add this line to your .vimrc file:

let g:C_SourceCodeExtensions  = 'h cc cp cxx cpp CPP c++ C i ii'

The important part here to notice is the h at the begining of the string. For more information on why this works see :h csupport-comm-frame.

Note: in order to see the help documentation, make sure to run :helptags ~/.vim/doc after installing the plugin. Otherwise the documentation that comes with the plugin will not be installed.

open VIM with default code

You can create a macro/abbr to do this for you, but you can also make this an autocmd to write to a file of a particular type. There may be a better way to do this, but this is something you would only have to include in your .vimrc

autocmd FileType c call IoStream()
fu! IoStream()
if line("$") == 1
call append(0, "#include <iostream>")
call append(1, "")
call append(2, "//other headers or macros go here")
call append(3, "")
call append(4, "using namespace std;")
call append(5, "")
call append(6, "int main(){")
call append(7, " return 0;")
call append(8, "}")
endif
endfu

.vimrc auto command to add text when opening a new file

You could use a substitution on the first line:

autocmd BufNewFile * let ftype = &ft | execute 'silent! 1s:.*:#!/usr/bin/env ' . ftype

Because shebangs are used in interpreted languages you could replace BufNewFile * with BufNewFile *.sh,*.perl,*.py. This prevents having shebangs in languages such as C.

How to run c.vim plugin for python files

There is a way to make snippets for both C and Python file types using neosnippet plugin.

Install the plugin and configure the key mappings:

imap <C-k> <Plug>(neosnippet_expand_or_jump)
smap <C-k> <Plug>(neosnippet_expand_or_jump)

You might want to use Honza's snippets as well as your own:

let g:neosnippet#snippets_directory = [ '~/.vim/bundle/vim-snippets/snippets',
\ '~/.vim/my-neosnippets' ]

Create ~/.vim/my-neosnippets/python/python.c.snippets file with custom snippets, for instance:

snippet [
[${1}]

Then create a symbolic link to this file:

├── c
│   └── c.python.snippets -> ../python/python.c.snippets
└── python
└── python.c.snippets

Now you can use python.c file type for Python files:

# vim: ft=python.c

And c.python file type for C files:

/* vim: set ft=c.python: */

If you don't like the idea to put modelines in every file, then you can use the rule for all C and Python files:

au FileType,BufRead c set ft=c.python
au FileType,BufRead python set ft=python.c

How to apply syntax file for vim?

All you need to do is make sure the filetype is loaded properly. So if the syntax file is called cpp.vim make sure set ft? returns cpp. You should place it in $HOME/.vim/syntax. It should get loaded automatically. Assuming filetype on and syntax on is set.

The only way to make sure the syntax file isn't loaded properly is to use :scriptnames. Make sure the file isn't listed if it is your problem is something different then what you have described.

Changing Vim indentation behavior by file type

You can add .vim files to be executed whenever vim switches to a particular filetype.

For example, I have a file ~/.vim/after/ftplugin/html.vim with this contents:

setlocal shiftwidth=2
setlocal tabstop=2

Which causes vim to use tabs with a width of 2 characters for indenting (the noexpandtab option is set globally elsewhere in my configuration).

This is described here: http://vimdoc.sourceforge.net/htmldoc/usr_05.html#05.4, scroll down to the section on filetype plugins.



Related Topics



Leave a reply



Submit