My after/ftplugins/fortran.vim has the following lines
let fortran_fold=1
let fortran_fold_multilinecomments=1
set foldmethod=syntax foldlevel=1 foldnestmax=2
let fortran_free_source=1
let fortran_do_enddo=1
If I open a fortran file, I don't see folded code, but when I change foldmethod to indent it works and I see code folded based on indentation.
The same settings work if I put them in init.vim.
I also have
syntax on
filetype indent plugin on
in my init.vim.
Nvim enables filetype plugin indent on and syntax on by default (:h nvim-defaults). So, just nvim test.for (without any init.vim) would do the right thing.
But when doing it manually, the order does matter! This is correct:
filetype plugin indent on
syntax on
Vim suffers the same issue.
Related
I put in my ~/.vimrc file the next lines of code:
filetype plugin on
set omnifunc=syntaxcomplete#Complete
but nothing happened, when I created a file with .c extension and I pressed <Ctrl-X><Ctrl-O> the vim editor popped up a "pattern not found" message.
Here one can see my .vimrc:
You have two problems.
First problem
The command filetype plugin indent on tells Vim to:
detect the filetype of every buffer,
source the appropriate filetype plugin for the detected filetype,
source the appropriate indent script for the detected filetype.
Those things happen automatically, as you load a new buffer. In this specific case (and others) the C ftplugin contains this line:
setlocal ofu=ccomplete#Complete
which effectively overrides your:
set omnifunc=syntaxcomplete#Complete
There are several ways to solve this problem but the most sensible is to make your own filetype plugin:
Create ~/.vim/after/ftplugin/c.vim.
Add setlocal omnifunc=syntaxcomplete#Complete to it.
See :help filetype-plugin.
Second problem
As explained under :help ft-syntax-omni, syntaxcomplete#Complete pulls its suggestions from the current syntax definition. Since syntax highlighting is not enabled by default and you don't enable it explicitly, there is no "current syntax" and thus no syntax-based completion.
You are supposed to enable it explicitly, in your vimrc:
syntax on
Conclusion
This is how your vimrc should look like:
filetype plugin indent on
syntax on
And this is your brand new custom filetype plugin for C:
" ~/.vim/after/ftplugin/c.vim
setlocal omnifunc=syntaxcomplete#Complete
The syntax code completion uses current language keywords to complete the text. If the text doesn't match any keyword, it says "pattern not found". For example, in a .c file
func| -> function
abc| -> "pattern not found"
See :h ft-syntax-omni for details.
I'm not sure if this is the right place to ask about this, but I figured it couldn't hurt to ask here. I am using a plugin called auto-close so that I don't have to close my own parentheses. It has a very nice feature that does the following:
This is a great feature, but I don't like how far it indents for me.
I have the following line in my .vimrc:
" for filetype "js", tab = insert 4 spaces, backspace will delete all 4
autocmd Filetype javascript setlocal expandtab softtabstop=4
In editing a javascript file, it automatically did an 8-space indentation instead of a 4-space indenation, as I've specified in my .vimrc. Can anybody help me figure out how I can make it automatically indent 4-space tabs instead of 8-space tabs? I can't find it in the documentation either. Thanks!
If you get shiftwidth=8, softtabstop=0, tabstop=8, that means that your autocmd FileType didn't take effect. You'd have to troubleshoot that.
I would recommend putting any settings, mappings, and filetype-specific autocmds into ~/.vim/ftplugin/{filetype}_whatever.vim (or {filetype}/whatever.vim; cp. :help ftplugin-name) instead of defining lots of :autocmd FileType {filetype}; it's cleaner and scales better; requires that you have :filetype plugin on, though. Settings that override stuff in default filetype plugins should go into ~/.vim/after/ftplugin/{filetype}.vim instead. The change of indent settings would fit the latter, after directory location.
When writing code in vim, I've noticed that it sometimes messes up the indentation of the current line after pressing return. (In the gif, I go into insert mode, with the cursor at the end of the date function. When I press return, the whole "echo date('Y');" part looses its indentation when it should not).
I have a ton of directives in my vimrc to try and stop all kinds of autoindentation, but much to my irritation, the problem persists.
filetype plugin indent off
filetype plugin off
set noautoindent
set nosmartindent
set nocindent
set indentexpr=''
let b:did_indent = 1
set ft?
filetype indent off
I wish there was a way to have vim indent files much like your "more standard" text editors, whereby they don't try to be smart, but just maintain the current indentation of the current line in the new line. Like this:
Is this possible?
set smartindent is only needed on my VIM to get the effect you shown by gedit.
So it seems that this line was causing the first issue displayed in the vim. Its part of the Vundle plugin manager inclusion code:
filetype plugin indent on
Ensuring that my indent rules were after this fixed the problem completely.
I know that gg=G is one of the simplest and easiest commands to indent lines in vim.
But, is there a way to indent lines as I type and press Enter?
Yes. Add the following to your vimrc:
set autoindent
filetype plugin indent on
Autoindent just indents based on the previous line's indentation if it does not know how to indent. Filetype indentation uses some things vim knows about what kind of programming language you are writing in to indent it correctly. Filetype indentation will override autoindentation.
After I installed Vundle, my vim no longer obeys the expandtab settings I had. My tabs were set to 2 spaces, but now in python files it no longer does that. The problem is being called by this line:
filetype plugin on
What does this line do (It is required by vundle)? Also, what can I do to make sure my settings are obeyed?
Thanks!
VIMRC: pastebin.com/tGmfCi78
The problem is that your settings are being overridden by a filetype plugin that's part of Vim. The issue is in ftplugin/python.vim:
" As suggested by PEP8.
setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8
The python plugin attempts to setup your source code to be PEP8 compliant by default, so it's adjusting the tabstop. You'll want some of what these plugins have to offer, but you may need to setup your own autocommands to fixup anything you don't like.
There are two ways to go about doing this. If you have a ~/.vim folder, the easiest way is to add the file ~/.vim/after/ftplugin/python.vim:
" Here, you can set the setting directly, or call a command or function
" to help you. We'll call a command, and then implement that command in
" your top-level vimrc to help keep things in one place.
SetupPython
In your .vimrc, add:
function! SetupPython()
" Here, you can have the final say on what is set. So
" fixup any settings you don't like.
setlocal softtabstop=2
setlocal tabstop=2
setlocal shiftwidth=2
endfunction
command! -bar SetupPython call SetupPython()
The latter bit just allows you to call the function as SetupPython rather than call SetupPython() in the after file.
The other way, is to keep everything in your .vimrc, but you use the VimEnter autocommand to setup a FileType autocommand for python to set your preferences. By waiting until VimEnter is triggered, all the other plugins will have had time to setup their autocommands, so your's will be added to the end of the list. This allows you to run after the python plugin's FileType autocommand and set your own settings. This is a bit of a mess though, and the after/ mechanism above is the preferred way of doing this.
FWIW, many common settings I keep in a SetupSource() function to be called from a number of different FileTypes. Then I'd have SetupPython() call SetupSource(). This helps to keep the specific functions a little cleaner and reduce some duplication. If it helps, take a look at the functions in my vimfiles here: https://github.com/jszakmeister/vimfiles/blob/master/vimrc#L5328
Overridden settings
It could be that the settings are being overridden by language-specific settings. See http://vim.wikia.com/wiki/Keep_your_vimrc_file_clean for more information:
The quick way to get started is to move all the language-specific stuff from your .vimrc file into a file named .vim/ftplugin/language.vim (or $HOME/vimfiles/ftplugin/language.vim on Windows).
Check in those locations for a python specific .vim file.
Filetype on
Vundle appears to require filetype off, and I'm not sure if you should turn it back on. There's a thread on the github issues page for Vundle explaining why filetype on is required. Perhaps this will provide some insight.
I also think having filetype plugin indent on followed by filetype on is redundent. According to the vim help docs, the former turns detection, plugin and indent on, and the latter turn detection on and leaves the plugin and indent unchanged:
Overview: *:filetype-overview*
command detection plugin indent
:filetype on on unchanged unchanged
:filetype off off unchanged unchanged
:filetype plugin on on on unchanged
:filetype plugin off unchanged off unchanged
:filetype indent on on unchanged on
:filetype indent off unchanged unchanged off
:filetype plugin indent on on on on
:filetype plugin indent off unchanged off off