I have been searching a solution which puts the fold marks and codes to an external hidden file.
This way you could have permanent folds without extra fold signs.
How can you control folds by an external file in Vim?
That's a very general question. What is "an external file" in vim, really ? Vim is, after all, a collection of "external" files. So if I define my folding preferences in my vimfiles, is that "an external file" solution ?
You could link, for example, define regex mechanisms of folding and source them from an external file.
But I think, what you ment is, "can I define an external file, so I can have by project custom folding, so that everyone who uses vim after I give them my files, will have the same folding" ? Yes, I guess you could do that by extrapolating from the method above.
But remember, vim has several methods of folding:
manual - where you manually define folds (this is nice, but it leaves your code with lots of curly brackets, but it is "portable")
indenting - where indending defines folds
expression (which I mentioned)
syntax - defined by syntax highlighting
marker - you already know that
... in the end, it will all come down to those few settings in your vimrc.
My advice: DO NOT CHANGE vim's indenting, syntax, etc. files. Normally, you will not even need to change those (unless you're using a language vim has no support for, which I doubt). So, define your settings and preferences in your vimrc and vimfiles directory, and just give your coleagues preferences which they need (usually just those from vimrc) to have the same folding behaviour.
Vim can be made to atuomatically remember folds in a file by putting these two lines in ~/.vimrc
au BufWinLeave ?* mkview
au BufWinEnter ?* silent loadview
Use manual method of making folds. This is not the same as marker as implied above. Use :mkview to save folds and :loadview to reload them.
Related
The editor Vim comes with syntax highlighting for many different programming languages.
Questions:
In Emacs, language-specific settings are called "modes". In Vim, however, the term "mode" refers to command or insert mode. So what is the Vim term for programming language specific settings?
Is the programming language of a document determined from its file name extension, or from its contents?
How can I find out which programming language specific mode Vim is in?
How can I overwrite that once and for all for a certain class of documents?
Posting an answer, as requested.
(1) In Emacs, language-specific settings are called "modes". In vim, however, the term "mode" refers to command vs insert mode. So what is the vim term for programming language specific settings?
The equivalent Vim term is filetype. Vim uses filetypes to apply language-specific options, indentation, syntax highlighting, key mappings, etc. This is described in detail in the help, see :h filetype.
To enable automatic filetype detection and handling, you'd normally add something like this to your vimrc:
filetype plugin indent on
syntax on
To override the settings Vim gives you this way, you need to add the overrides to a file ~/.vim/after/ftplugin/<filetype>.vim (or equivalent). See :h ftplugin for more details.
(2) Is the programming language of a document determined from its file name extension, or from its contents?
Both methods are used. Vim does most filetype detection in a file filetype.vim in it's runtime directory. To find out the exact location of this file:
:echo $VIMRUNTIME.'/filetype.vim'
Plugins can add more filetypes, and / or override detection and handling of standard ones.
(3) How can I find out which programming language specific mode vim is in?
From Vim:
set ft?
(4) How can I overwrite that once and for all for a certain class of documents?
To change the filetype of the current file:
:setf <new_filetype>
or
:setl ft=<new_filetype>
To make the change permanent: do that from a modeline (cf. :h modeline). For example:
# vim: filetype=python
You can also use autocmds to achieve the same effect:
autocmd BufRead,BufNewFile *.py setlocal filetype=python
Do read :h filetype, where all this is described in detail.
How are they called & how to detect them
Vim uses the file filetype.vim which is used to determine the "type" of a file. So if you are editing a python file "example.py" and you used the command :set ft? it should display filetype=python. Using the file type VIM determines whether any plugins indenting rules, or syntax highlighting related to python are loaded.
How to overwrite them
You can write your own indenting rules, or syntax highlighting and other rules for the programming language by putting these settings in your vimrc. using the vim language called VIML you can see this section in Learn Vimscript the Hard Way where the author detects the Potion Filetypes. Once you detect the filetype which is done using the file extension for example python files are *.py or erlang files are *.erl the you can add your own language specific settings.
I recently added filetype plugin indent on to my .vimrc in order to enable special indenting and syntax highlighting for Clojure code (*.clj files). However, it's also causing indenting in my LaTeX files (*.tex). This is annoying when I'm editing, and even more annoying, because the tab characters that get inserted confuse a custom program I use to process my LaTeX files. I know I can make indenting use spaces, but I really just want "intelligent" LaTeX indenting to go away. Actually, I want all intelligent indenting to go away, except where I specifically ask for it.
How can I get correct auto-formatting for Clojure code in Vim, but turn off all special handling of LaTeX files (except for syntax highlighting)?
Sorry if this has already been answered; I haven't succeeded in finding the answer yet.
(Irrelevant editorial comment: Sometimes Vim "upgrades" make me want to go back to Unix 'vi'. OK, not really.)
Each proper filetype plugin script has an inclusion guard at its beginning. If you don't want any of the filetype options for Latex files (i.e. filetype of tex), create a file ~/.vim/ftplugin/tex.vim with these contents:
:let b:did_ftplugin = 1
This causes the default ftplugin from $VIMRUNTIME to abort its execution. The same applies to indent: ~/.vim/indent/tex.vim and b:did_indent is the guard variable.
Alternative
On the other hand, if you just want to undo certain options (e.g. :setlocal expandtab to avoid inserting tabs), you'd put those overriding commands into the so-called after directory: ~/.vim/after/ftplugin/tex.vim.
I want to write a vim plugin that does certain text transformations to the text while in the editor, but I don't want these transformations visible inside the file.
As an example, consider the word Gnarly in a text file I want to edit. Upon load I would want my vim script change that to G, but when I save the buffer I want it to expanded back to Gnarly.
My scenario is a little bit more complex because it will involve an external script, but I want to see exactly how that would be invoked.
Also I'd want to be able to apply this change only to some files based on their extension.
See :h autocmd. The events you need are BufRead and BufWrite.
Maybe you will be interested by :h conceal.
First of all, define your own filetype, e.g. gnarly. Read :help new-filetype for the details, but basically it's this autocmd:
:autocmd BufRead,BufNewFile *.gnarly set filetype=gnarly
Then, the conceal feature introduced in Vim 7.3 is the way to go. Write a syntax script ~/.vim/syntax/gnarly.vim. For your example it would contain:
:syntax keyword gnarlyConceal Gnarly conceal cchar=G
But you can also use :syntax match for more complex patterns.
Finally, concealment is off by default. To turn it on, put the following command into ~/.vim/ftplugin/gnarly.vim (you could put it into the syntax file, too, but this separation is recommended and done by all full plugins that ship with Vim):
:setlocal conceallevel=1
You can customize the 'concealcursor' behavior, too. If you still need help, have a look at the help pages, or existing plugins that use concealment.
I have some character maps for typing braces that I'd like to behave differently based on the file's extension - I imagine that would be pretty useful ability to have in general.
Any ideas on how to make this happen from my .vimrc or a plugin?
Thank you!
There are basically two ways. Use the filetype plugin, or use filetype or extension autocommands.
The autocommands (placed in your .vimrc/_vimrc) take the form of either
autocmd Filetype cpp set textwidth=100
or
autocmd BufRead *.cpp,*.h,*.c set textwidth=100
(Obviously set textwidth=100 can be replaced with any other command)
The better solution, particularly if you have alot of custom commands for a filetype, is to use the files ~/.vim/after/ftplugin/<filetype>.vim for each filetype. Commands in those files will be executed after loading a file of the given type.
In complement to jkerian's answer, here are a few links to other questions related to ftplugins (as script-local variables, mappings, command, settings, abbreviations, etc shall be used when writing ft specific configuration):
Multiple autocommands in vim
gVim and multiple programming languages
I have tried the usual approaches, and have read :help tex.vim
(see : http://vimdoc.sourceforge.net/htmldoc/syntax.html )
I've taken a brief look at syntax/tex.vim, but can't see how to disable it without rebuilding vim without folding. I'm sick of hitting 'zE'.
Lines I've tried in my .vimrc:
set foldlevel=manual
set foldlevelstart=99
let g:tex_fold_enabled=0
Just noticed that there are variables to control folding in vim-latex-suite, at least as of v1.6 of the plugin. The functionality is documented here:
http://vim-latex.sourceforge.net/documentation/latex-suite.html#latex-folding
In short you should be able to change three global variables to get rid of all folding:
:let Tex_FoldedSections=""
:let Tex_FoldedEnvironments=""
:let Tex_FoldedMisc=""
That should get rid of all folding. If you want to disable some folding but not all then you can control things by setting the appropriate values for each variable, as described in the documentation link above. Hope that helps.
What about
autocmd Filetype tex setlocal nofoldenable
The folding functionality all seems to located in folding.vim file of latex-suite distribution. This file is referenced in line 825 of my main.vim file in the latex-suite folder of the ftplugin folder. That line reads:
exe 'source '.fnameescape(s:path.'/folding.vim')
Comment out that line and, as far as I can tell, it strips out all the folding in latex-suite plugin. I don't think it affects anything else, but I haven't checked.