spaces rather than tab only for C/C++ files

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

spaces rather than tab only for C/C++ files

Kiwon Um

Hello there.
I have a question.

I'm using spaces rather than tab,
using "set softtabstop=2 shiftwidth=2 tabstop=4 expandtab" in
the .vimrc file.

But these settings work for all type of files.
I just want to use these only for the C/C++ sources.

Any advise?

Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: spaces rather than tab only for C/C++ files

François Ingelrest-2

>  I'm using spaces rather than tab,
>  using "set softtabstop=2 shiftwidth=2 tabstop=4 expandtab" in
>  the .vimrc file.
>
>  But these settings work for all type of files.
>  I just want to use these only for the C/C++ sources.

You can do it with an autocommand:

filetype on
au FileType c,cpp setlocal softtabstop=2 shiftwidth=2 tabstop=4 expandtab

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: spaces rather than tab only for C/C++ files

Ben Schmidt
In reply to this post by Kiwon Um

Kiwon Um wrote:

> Hello there.
> I have a question.
>
> I'm using spaces rather than tab,
> using "set softtabstop=2 shiftwidth=2 tabstop=4 expandtab" in
> the .vimrc file.
>
> But these settings work for all type of files.
> I just want to use these only for the C/C++ sources.
>
> Any advise?

You can create files in ~/.vim/after/syntax called c.vim and cpp.vim and put the
command in there.

Or you can make an autocommand that applies to the files you want and put that in
your .vimrc:

au FileType c,cpp setlocal softtabstop=2 shiftwidth=2 tabstop=4 expandtab

Either way, you should use setlocal not set, so it only affects the file being
edited, not all new files as well.

See

:help after-directory
:help FileType

Hope this helps,

Ben.




Send instant messages to your online friends http://au.messenger.yahoo.com 


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: spaces rather than tab only for C/C++ files

Tony Mechelynck
In reply to this post by François Ingelrest-2

François Ingelrest wrote:

>>  I'm using spaces rather than tab,
>>  using "set softtabstop=2 shiftwidth=2 tabstop=4 expandtab" in
>>  the .vimrc file.
>>
>>  But these settings work for all type of files.
>>  I just want to use these only for the C/C++ sources.
>
> You can do it with an autocommand:
>
> filetype on
> au FileType c,cpp setlocal softtabstop=2 shiftwidth=2 tabstop=4 expandtab

...or with an after-plugin (one line in this case; create the file and/or
directories if they don't exist yet).

----- ~/.vim/after/ftplugin/c.vim (Unix)
----- $HOME/vimfiles/after/ftplugin/c.vim
setlocal sts=2 sw=2 ts=4 et
----- end

Note: The c.vim ftplugin is also sourced for the cpp filetype, see the last
line of $VIMRUNTIME/ftplugin/cpp.vim


Best regards,
Tony.
--
"Well," Brahma said, "even after ten thousand explanations, a fool is
no wiser, but an intelligent man requires only two thousand five
hundred."
                -- The Mahabharata.

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: spaces rather than tab only for C/C++ files

Tony Mechelynck
In reply to this post by Ben Schmidt

Ben Schmidt wrote:

> Kiwon Um wrote:
>> Hello there.
>> I have a question.
>>
>> I'm using spaces rather than tab,
>> using "set softtabstop=2 shiftwidth=2 tabstop=4 expandtab" in
>> the .vimrc file.
>>
>> But these settings work for all type of files.
>> I just want to use these only for the C/C++ sources.
>>
>> Any advise?
>
> You can create files in ~/.vim/after/syntax called c.vim and cpp.vim and put the
> command in there.

or rather, in ~/.vim/after/ftplugin/c.vim (or, on Windows,
~/vimfiles/after/ftplugin/c.vim):

- In Vim, "syntax" scripts apply only to the coloring: the definition of
syntax groups, the default coloring for highlight groups, how far back to
synchronize the syntax, and the like. OTOH, options are set (using ":setlocal"
indeed, as you say) in "ftplugin" scripts, which may also contain ":map
<buffer> {lhs} {rhs}" for filetype-dependent mappings (similarly other mapping
modes, abbreviations, etc.). It is conceivable (though it rarely happens, if
ever) that there could be several scripts of the one kind for one of the
other. But ":filetype on" without the "plugin" parameter, or "filetype plugin
off" will disable the options & mappings but not the coloring. Similarly,
indent/<filetype>.vim scripts are concerned only by indenting, and are
disabled by ":filetype plugin off". (Sorry, I got into "teaching mode" again.)

- In the particular case of c and cpp ftplugins, it isn't necessary to copy in
cpp.vim what you already put in c.vim, because the
$VIMRUNTIME/ftplugin/cpp.vim script consists of hardly anything but the line
"runtime! ftplugin/c.vim".

>
> Or you can make an autocommand that applies to the files you want and put that in
> your .vimrc:
>
> au FileType c,cpp setlocal softtabstop=2 shiftwidth=2 tabstop=4 expandtab
>
> Either way, you should use setlocal not set, so it only affects the file being
> edited, not all new files as well.
>
> See
>
> :help after-directory
> :help FileType
>
> Hope this helps,
>
> Ben.

Best regards,
Tony.
--
This life is a test.  It is only a test.  Had this been an actual life,
you would have received further instructions as to what to do and where
to go.


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: spaces rather than tab only for C/C++ files

Ben Schmidt

>> You can create files in ~/.vim/after/syntax called c.vim and cpp.vim and put the
>> command in there.
>
> or rather, in ~/.vim/after/ftplugin/c.vim (or, on Windows,
> ~/vimfiles/after/ftplugin/c.vim):

Mmm. I originally had that but then changed it. I am kind developing a philosophy,
which applies more to my personal .vim than to the $VIMRUNTIME stuff, which says,
rather than the distinction between syntax and ftplugin being 'colours and syntax'
vs. 'anything else that isn't indenting', more 'anything to do with display' vs.
'annoying maps and stuff that screw up more things than indentation'. Moreover, if
filetype detection is on, filetype syntax detection is on, so something in
/syntax/ will always work, and it is unlikely you want to turn off your personal
customisations, so I thought syntax was a better place to put it. Of course, you
can make arguments either way. Probably should have mentioned both and let the OP
decide what (s)he thinks is best.

P.S. I think $HOME or something like that is better to use for Windows, which
doesn't usually use ~ and though Vim may expand it for some commands, I guess, my
suspicion is that it usually uses the shell to evaluate it, so it won't actually
work on Windows (except probably Cygwin).

> (Sorry, I got into "teaching mode" again.)

LOL. Yes. You're good at that. I was aware of all you said, but not offended that
you mentioned it. I struggle from a similar tendancy to be more verbose than
needed, and am sure it is offensive sometimes, or just plain unhelpful as it makes
for a lot of words for a user to sift through in search of their answer!

> - In the particular case of c and cpp ftplugins, it isn't necessary to copy in
> cpp.vim what you already put in c.vim, because the
> $VIMRUNTIME/ftplugin/cpp.vim script consists of hardly anything but the line
> "runtime! ftplugin/c.vim".

Yeah. I noted that from your earlier post. If I'd thought about it, that would've
been obvious, too...

Smiles,

Ben.




Send instant messages to your online friends http://au.messenger.yahoo.com 


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: spaces rather than tab only for C/C++ files

Tony Mechelynck

Ben Schmidt wrote:

>>> You can create files in ~/.vim/after/syntax called c.vim and cpp.vim and put the
>>> command in there.
>> or rather, in ~/.vim/after/ftplugin/c.vim (or, on Windows,
>> ~/vimfiles/after/ftplugin/c.vim):
>
> Mmm. I originally had that but then changed it. I am kind developing a philosophy,
> which applies more to my personal .vim than to the $VIMRUNTIME stuff, which says,
> rather than the distinction between syntax and ftplugin being 'colours and syntax'
> vs. 'anything else that isn't indenting', more 'anything to do with display' vs.
> 'annoying maps and stuff that screw up more things than indentation'. Moreover, if
> filetype detection is on, filetype syntax detection is on, so something in
> /syntax/ will always work, and it is unlikely you want to turn off your personal
> customisations, so I thought syntax was a better place to put it. Of course, you
> can make arguments either way. Probably should have mentioned both and let the OP
> decide what (s)he thinks is best.
>
> P.S. I think $HOME or something like that is better to use for Windows, which
> doesn't usually use ~ and though Vim may expand it for some commands, I guess, my
> suspicion is that it usually uses the shell to evaluate it, so it won't actually
> work on Windows (except probably Cygwin).

I'm not on Windows anymore, but IIRC, when I used ~/ at the start of a path
(in "native" Windows Vim, not Cygwin Vim, which I hardly ever used), Vim
translated it to the HOME environment variable or whatever it used for it.
Cannot find it back in the help though.

My suspicion is that Vim evaluates filename wildcards other than ** by using
the shell on Unix, but that it evaluates them itself on Windows (where the
shell normally passes to the program any wildcards found on the command-line).

>
>> (Sorry, I got into "teaching mode" again.)
>
> LOL. Yes. You're good at that. I was aware of all you said, but not offended that
> you mentioned it. I struggle from a similar tendancy to be more verbose than
> needed, and am sure it is offensive sometimes, or just plain unhelpful as it makes
> for a lot of words for a user to sift through in search of their answer!
>
>> - In the particular case of c and cpp ftplugins, it isn't necessary to copy in
>> cpp.vim what you already put in c.vim, because the
>> $VIMRUNTIME/ftplugin/cpp.vim script consists of hardly anything but the line
>> "runtime! ftplugin/c.vim".
>
> Yeah. I noted that from your earlier post. If I'd thought about it, that would've
> been obvious, too...
>
> Smiles,

Me too.

>
> Ben.


Best regards,
Tony.
--
Peter's Law of Substitution:
        Look after the molehills, and the mountains will look after
themselves.

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---