Editing Vim indent files - vim

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).)

Related

filetype and vim syntax highlighting for the output of ipython

In ipython, I can use ? to get the doc for the object preceding it, like os.path.join?<enter>. Then I get the output like this:
Signature: os.path.join(a, *p)
Docstring:
Join two or more pathname components, inserting '/' as needed.
If any component is an absolute path, all previous path components
will be discarded. An empty last part will result in a path that
ends with a separator.
File: ~/conda/envs/test/lib/python3.6/posixpath.py
Type: function
I want to load this output into Vim and get the syntax highlighting.
Which filetype should I set for it?
If no existing filetype describes this format, how to set the syntax highlight for it?
If Vim came with a syntax highlighting for it, there would most likely also be a filetype detection for it, as these usually come together. You could search the Internet whether someone has already created a syntax for that and published it, on vim.org and/or in a GitHub repository.
To start developing your own syntax see :help :syn-define and :help usr_44.txt.
For example, to highlight the Signature:, File:, ... prefixes, you'd use:
syntax match pythondocPrefix "^\a\+:"
Instead of defining custom colors, it is recommended to link to existing highlight groups; cp. :help highlight-groups.
highlight def link pythondocPrefix Type
This is just a start; you can define as many distinct elements as you like! It helps to look at existing syntax scripts (in $VIMRUNTIME/syntax/) to see how it's done. For the Python string after Signature:, it would be nice to include the Python syntax there; see :help :syn-include.
You'd put all these commands into a file ~/.vim/syntax/pythondoc.vim. :help 44.12 has additional tips.
Usually, you'd then define a :help new-filetype with detection, but as you apparently want to trigger the doc lookup via a custom mapping or command, you can just directly :setlocal syntax=pythondoc in that scratch buffer.

Turn on Vim plugins for some files, not others?

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.

How do you set up formatting in vim?

Earlier, when I used open .py files in vim on ubuntu, they would be well formatted, with separate colours for separate segments of the program. Now, when I am using VIM on ubuntu, all the text in the .py file appears black. How can I correct this?
Formating in Vim means text formatting; e.g. indenting lists and breaking long lines. You're concerned about syntax highlighting, which is purely about the visual appearance of code.
First, it needs to be turned on.
:syntax on
does that.
Second, you probably want Vim to automatically detect the used language (e.g. Python) and choose the correct syntax plugin for you.
:filetype on
does that, though you usually enable more via :filetype plugin indent on.
To make these settings persistent, put them into your ~/.vimrc configuration.
Check man vim. In a nutshell, find a copy of a vimrc file, one might be under /usr/share/vim/ subtree. It may be named vimrc_example.vim. Copy to your home directory and rename it as .vimrc.

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.

How do you disable a specific plugin in Vim?

I have Vim set up to use the excellent NERDTree plugin. However, there are some environments where I do not want this plugin to be loaded.
In my .vimrc I have a sections that are only run when specific environment variables are true. In one of these sections I would like to disable the loading of NERDTree but all of the information I've come across states how to disable all plugins, not just one.
Could someone demonstrate how to disable the loading of one specific plugin in Vim?
Most plugins have a (re)inclusion guard.
Open the plugin, see the name of the guard, if any (if not, add one by yourself, and contact the author to make him fix his plugin), and finally set its value to 1 in your .vimrc.
That's all.
I can't be more specific as "open, and look for the guard" as not all plugins use the same guards-naming policy. It's often g:loaded_pluginname though.
Regarding ftplugins, it becomes more tricky. The guard is a buffer-local variable. As such, it can't be specified into your .vimrc (as it would apply only to the first buffer you open).
The easiest way would be to move your ftplugin from .vim/ftplugin to .vim/after/ftplugin, and to set the relevant anti-reinclusion guard to 1 in a ftplugin in your non-after hierarchy. As long as the ftplugin does not expect to be placed in after/ (or the contrary, it should be fine).
Note: most ftplugins believe they are unique and (mis-)use the variable b:did_ftplugin as reinclusion guard.
Add this line to your .vimrc
let g:loaded_nerdtree_exec_menuitem = 1

Resources