Re: Filetype error

5 views
Skip to first unread message

Tony Mechelynck

unread,
Oct 29, 2011, 2:26:09 AM10/29/11
to vim...@googlegroups.com, Justin Lafferty
On 29/10/11 03:47, Justin Lafferty wrote:
> For the longest of times my .vimrc has looked as follows:
>
> "Because some options may not be vi compatible
> set nocompatible
>
> "Turns on filetype plugins
> filetype plugin on
> filetype indent on
>
> "Ignore case when searching
> set ignorecase
> set smartcase
>
> "Highlight searches
> set hlsearch
>
> "Toggles the highliting off with ctrl+n
> :map <silent> <C-n> :set invhlsearch<CR>
>
> "Shows matches as they are found
> set incsearch
>
> "Remove trailing whitespace
> autocmd FileType c,cpp,java,php,js,python,ruby autocmd BufWritePre
> <buffer> :call
> setline(1,map(getline(1,"$"),'substitute(v:val,"\\s\\+$","","")'))
>
> "use spaces instead of tab
> set expandtab
> set smarttab
>
> "Changes tab amount
> set shiftwidth=4
> set softtabstop=4
>
> "Maps jj to esc for faster exit of insert modes
> imap jj <Esc>
>
> "Turns on syntax coloring
> syntax on
>
> "Enable line numbers
> set number
> set numberwidth=3
>
> "Normal backspace operation
> set backspace=indent,eol,start
>
> But now for some reason whenever I type the comment character for the
> particular language I am using, the character is repeated on the next
> line. For example if i am editing test.cpp if i type:
>
> //Comment
>
> Then press enter the file now looks as follows:
> //Comment
> //
>
> Through editing out the different options I have boiled the problem down
> to the filetype plugin on. And was wondering if anyone knows a fix for
> this. And if it helps, my .vim/ftplugin directory is empty.

It is not an error, or as the saying goes, "T'aint a bug, it's a
feature". IIUC many standard ftplugins have long set formatoptions+=ro �
of course, when you create a [NoName] file it still has no filetype
(even if you give the file a name without reloading it) and therefore no
ftplugin has set anything special for it. As soon as you name it _and_
reload it, the ftplugins apply.

See
:help 'formatoptions'
:help fo-table

- Flag r means: Automatically insert the current comment leader after
hitting <Enter> in Insert mode.
- Flag o means: Automatically insert the current comment leader after
hitting 'o' or 'O' in Normal mode.

To avoid these in C and C++ files, add the following two-line file as
$HOME/.vim/after/ftplugin/c.vim (for Unix, Linux or Mac OS X) or
$HOME/vimfiles/after/ftplugin/c.vim (in Vim notation, for Windows):

" disable comment leader insertion
setlocal fo-=r fo-=o

(the C++ ftplugin does nothing other than invoke the C ftplugin). If the
directories don't yet exist, create them too.

The above is "per-filetype" (with the exception that the C ftplugin is
also used for C++), so if you want the same for, let's say, javascript
and CSS (which have similar comment structures as C and C++), you'll
have to drop copies of the same file as javascript.vim and css.vim in
the same directory: you get the drift. For a blanket disable, you may
use instead

autocmd FileType * setlocal fo-=r fo-=o

which must be placed _after_ "filetype plugin on" in your .vimrc or
_vimrc in order to have an effect; *but* if at any time you use
":filetype plugin off" followed by ":filetype plugin on" this
autocommand will lose its effect, which is not the case of the scripts
in .../after/ftplugin/.

See also the following recent threads on vim_use:
- Permanent disable comment next line
(about 'formatoptions' flags r and o)
- Please fix: make Windows Vim use same files as unix. No reason not to
and it's confusing in mixed envirionments.
(started as: Can't get vim to use custom color in personal folder(finds
.vimrc/.gvimrc but not .vim/colors/name.vim) )
(about ~/vimfiles/ vs. ~/.vim/)


Best regards,
Tony.
--
Blessed are the meek for they shall inhibit the earth.

Reply all
Reply to author
Forward
0 new messages