Change recognized filetype of a plugin - vim

I want to use 'plasticboy/vim-markdown' and 'nelstrom/vim-markdown-folding.' They require filetype=mkd and filetype-markdown, respectively. Is there a typical way to tell a plugin to recognize a certain filetype?
I've tried changing references of mkd to markdown in the former and markdown to mkd in the latter but that hasn't had any effect. As of now, I can only use one of the plugins because they require different filetypes.

The generic names for the filetype plugins are (:help ftplugin-name):
ftplugin/<filetype>.vim
ftplugin/<filetype>_<name>.vim
ftplugin/<filetype>/<name>.vim
Therefore, you need to rename the file names themselves (possibly in addition to the contents in the file, though there shouldn't be that many). Since that makes upgrading more difficult, you can also write linker scripts, e.g. ~/.vim/ftplugin/mkd_fold.vim which just contains the following command:
:runtime! ftplugin/markdown_fold.vim

Related

Vim not recognizing some commands from vim-latex

I have vim-latex installed via Vundle and I'm trying to disable some annoying mapping that it sets up by default. From the docs I know that, for example, I can use the following command to unmap FEM:
call IUNMAP('FEM','tex')
But when I type that I get the error E117: Unknown function: IUNMAP.
I have installed vim-latex with Vundle by including Plugin 'LaTeX-Suite-aka-Vim-LaTeX' in my vimrc and I have just used the PluginUpdate command to update everything, which runs with no error, so I should have the latest version of the package.
Am I missing something here?
Actually, the problem you're having is related to where you're getting your vim-latex from.
With:
Plug 'LaTeX-Suite-aka-Vim-LaTeX'
You're getting it from here, which you'll notice hasn't been updated since 2010. Looking at the plugin/imaps.vim file in that repository, you'll see there's a definition for function IMAP(), but not for IUNMAP(), which was probably introduced after the last date that repository was synced...
Use this source instead:
Plug 'vim-latex/vim-latex'
Which will get it from here which is an official maintained location for this plug-in.
If you look at plugin/imaps.vim in that source tree, you'll notice function! IUNMAP is defined there.
Updating to the correct plug-in location should fix this problem for you (together with probably quite a few fixes from the last 10 years!)
The functions IMAP() and IUNMAP() are loaded by the vim-latex plug-in only after your vimrc is processed. So you need to execute them from a context where they're available.
Furthermore, in order to have your unmapping succeed, you need to actually execute it after the mapping was created, and mappings are typically created when the filetype is set.
The documentation mentions that these overriding rules should be done in specific files:
An important thing to note is that if you wish to over-ride macros created by Latex-Suite rather than merely create new macros, you should place the IMAP() (or IUNMAP()) calls in a script which gets sourced after the files in Latex-Suite.
A good place typically is as a file-type plugin file in the ~/.vim/after/ftplugin/ directory. [...]
For example, to delete a mapping, you can use
call IUNMAP('FEM', 'tex')
in ~/.vim/after/ftplugin/tex_macros.vim.
The documentation mentions that you should use a file in ftplugin after the filetype you use. Check :set ft? to confirm yours is indeed tex, in which case you can use tex.vim, tex_something.vim (like the suggested tex_macros.vim) or tex/something.vim (a subdirectory.)

How can I apply custom syntax highlighting in VIM to all file types?

The most preferable way to do this would be in some like my .vimrc file or another location in my vimfiles that's easily persisted and not attached to an extra plugin.
The help files for VIM (along with almost all solutions found on the Internet abroad) relate directly to adding syntax highlighting for a specific file type.
However, how would one add highlights that apply to all files?
An example would be highlighting extra keywords as part of the Todo highlighting group - such as "NOTE", "INTERNAL", etc.
I've attempted to use vimfiles\after\syntax\..., but again, it seems to be predicated on the right file type getting used for the .vim file created in that directory.
So, something like vimfiles\after\syntax\cpp.vim with the following works to achieve this in C++:
syntax keyword cTodo contained NOTE INTERNAL IMPORTANT
for C++ files specifically, and this works how I would expect it to.
But how can this be generalized to all file types when a file is loaded into a buffer?
You can hook into the syntax script loading via the Syntax event. Be sure to define this after :syntax on in your .vimrc:
:autocmd Syntax * syntax keyword allTodo NOTE
If you also want to handle buffers that don't have any syntax (/ filetype) set, add the BufNewFile,BufReadPost events.
Alternative
Depending on the syntax, you may need to specify contained[in], or :syn cluster add=... to augment the existing syntax. That unfortunately cannot be generalized. An alternative approach would be using :match or :call matchadd(...), which goes on top of (and is totally independent of) syntax highlighting. Since this is window-local, the autocmd would be:
:autocmd VimEnter,WinEnter * match Todo /\<NOTE\>/

Editing Vim indent files

I have a Vim indent file.
It is not indenting some words like module.
I want to add indent for some more words.
Where should I add these words???
Vim indent plugins (the scripts in $VIMRUNTIME/indent/*.vim) either contain custom settings for Vim's built-in indent-related options (like 'cindent', 'cinoptions'), or (in the majority) a custom Vimscript program that is installed into the 'indentexpr' option.
What such program does (and how it can be configured / extended) is flexible and up to the program's author. Therefore, no general recipe can be given.
Check the list of built-in filetypes at :help indent-expression. If your filetype is listed there, maybe there's a configuration for your particular problem.
Else, study the source code of the particular indent script. Maybe you can extend it to match your needs. (If this is generally useful, then send a patch to the plugin's author (listed at the top of the script).)

How do I organize a vim plugin with unique syntax elements?

I've written a (basic) vim plugin for myself, and I'm having a difficult time organizing it. Right now, it lives in two files: one in syntax/ and one in ftplugin/ (both of which are in bundle/<plugin name>. I would like the plugin to be active for various file types, including those with existing syntax files.
So far, doing something like :set ft=<existing filetype>.<new filetype> in each file works, but I can't figure out how to (consistently) do this from, say, my .vimrc.
So, two questions:
Do I have the right organizational approach? Is there an easier way to (say) combine the syntax and plugin files?
If so, what's the "right" way to append my custom filetype?
Though you didn't include a lot of details, you're right in that syntax/ contains syntax definitions (i.e. mostly :syntax commands) and ftplugin/ is for filetype-specific settings (i.e. mostly :setlocal, :map <buffer>, etc.).
I would like the plugin to be active for various file types, including those with existing syntax files.
The correct approach depends a lot on the relationship between the original syntax and yours. It's hard to extend arbitrary syntaxes, because you have to consider overlaps and use the appropriate containedin=... specifiers in your syntax extension.
The compound filetype :setl filetype=c.doxygen works well when auto-detecting or manually setting it. If you don't want the extension to be so visible (and some plugins don't handle compound filetypes well), you can alternatively :runtime syntax/mine.vim in all ~/.vim/after/syntax/original.vim files (and do the same for ftplugin/).
Another alternative is hooking into :autocmd Syntax ... and :autocmd FileType ...; this avoids writing many short stub files that just :runtime your syntax / ftplugin additions.

Trigger command on buffer load and buffer save

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.

Resources