Syntax Highlighting in VIM after :bd - vim

When I useVIMthe syntax highlighting works, except after I return to a buffer using:bdthe highlighting disappears. I can enable it again using:syn on.This problem occurs only for files without an extension. I remember I had to change some settings to get files without an extension to be highlighted in the first place, but I can't remember the details. (I use files without an extension because thegfcommand just works when I do this.) Can anyone help?
Thanks.

take a look at :h modeline you should be able to set a comment in the file that tells vim what settings to use when you visit that file.

Related

vim syntax highlight not working (MacOs Ventura)

I use vim as my text editor, and I want to make use of highlighted syntax. However, I cannot get it to work when I use the command “syntax on” in my ~/.vimrc file.
The strange thing is that it does the syntax is highlighted in the ~/.vimrc file itself, but not in other files.
When I use the command “syntax on” in my ~/.vimrc file, it does highlight the text in the ~/.vimrc file itself, but not in other files. If I provide other commands in the ~/.vimrc file they work: colorscheme wors, set number also works. So the ~/.vimrc file itself is actually used and works correctly, only the “syntax on” command does not work.
Anybody who can help me with this?
Thanks in advance!
I have found the issue. Beginners mistake I guess...
As mentioned the comments, in the .vimrc file syntax highlighting worked fine. In the files I made myself, I just opened a file without extension (i.e. vim testfile).
In a file without extension, the syntax highlighting does not work because vim does not know how to highlight the syntax. When I now for instance open a file vim testfile.py (with the Python extension), it works fine.
Thanks anyway for your comments!

Vim syntax doesn't highlight in real time

I have enabled vim syntax on (in ~/.vimrc syntax on) and it works but only on files with a code in when I view them. When I create a new file with vim and write there some code - no syntax highlight. After saving this file and reopening with vim - syntax highlight works perfect.
I am using manjaro KDE.
When you open a new file without an extension (vim mynewfile) none of vim’s filetype detection mechanisms can recognize it (they all use either extensions or first-couple-of-lines heuristics, which don’t work here).
When you enter code and reopen the file, the line-checks for filetypes work, causing the syntax to be set correctly, causing highlights to apply.
You can always set syntax=mine (though set filetype=mine is better) to set it manually.
This problem shouldnt happen when you do vim some.c or similar, because the extension will force detection based on extension rules.
Vim must know how to highlight your syntax in order to actually highlight it. One way to do this, is for Vim to check the file name and sometimes inspect the contents of the file, and set the file type. The file type is then used to highlight the syntax.
To enable detection of the file type (and load plugin and indent files), add the following to your vimrc:
filetype on plugin indent
If Vim is unable to detect the file type, and you have not yet saved your file with a known extension, you can set the file type manually, like this:
:set filetype=html
Vim will then highlight the syntax of the file as HTML syntax.
More information is available in the help pages.

set vim to autoindent using the same characters as the previous line

I use tabs for indentation in all my files. However, some bigger projects still use spaces, so I also need to follow those guidelines when I contribute.
This issue could easily be solved if the editor would detect the indent character used in the edited file, and use that one when e.g. autoindenting.
Sadly, vim forces spaces for indent, even when I am editing a file which otherwise only uses tabs.
How can I tell vim to always just use what the file is using (and use tabs if the file isn't using anything)?
I tried adding the following line (as I understand it, this should be forcing tabs - not exactly what I want, but better than inserting spaces everywhere) to ~/.vimrc and reopening the file I was editing, but this failed to take effect.
I am running vim-8.0.0386.
It looks like tpope/sleuth plugin is exactly what you want.
Otherwise, DetectIndent works too.
Clone the DetectIndent repo in your .vim/bundle/ directory (I use pathogen to install plugins), then open any file and type: :DetectIndent
Now shiftwidth, expandtab and tabstop should be adjusted to your file.
Here's another plugin recommendation: My IndentConsistencyCop plugin. It does not only detect the used indent setting, but also warns you if inconsistent settings are used within the same file.

Defining the settings for the vim help file

With opened vim help file the command
:set conceallevel=0
works fine and e.g. the "|" signs for the internal links are showed as expected, but unfortunately, it remains not permanent, i.e., jumping to another link hides the "|" signs again.
As I put this setting into my .vimrc as into my .gvimrc too to make it permanent, there is no effect at all, while other settings from my config files are applied properly.
So are there any local definitions for the vim help file?
Perhaps I didn't look for this deeply enough, but because I think it is from some importance for other people too, I've got the courage to ask this question here ;-)
I really appreciate any suggestions...
(Tested with VIM 7.4 from 2013 August 10 from the ubuntu repository)
Yes, as with most filetypes, Vim has help-specific settings that override your generic settings.
You can use :verbose to see where an option was set:
:verbose set conceallevel?
The default ftplugin for help files is where conceallevel is set:
$VIMRUNTIME/ftplugin/help.vim
To override that setting, create ~/.vim/after/ftplugin/help.vim and put this line:
setlocal conceallevel=0

vim spell checking - comments only in LaTeX files

I use gvim to edit LaTex .tex file. I noticed that it checks spelling on the fly only for the commented text. If I have a mistake in a regular text - no underline. If I comment this text with % , the misspell is underlined immediately. What is wrong? Is there any strange option turned on?
The latex ft plugin conveniently defines this behaviour.
SpellChecker : Spell check text including LaTeX documents
Using latexmk, vim spell checking and vim latex-suite
There is an option that appears to come close:
:syntax spell [toplevel | notoplevel | default]
Update
Also
:he ft-tex-syntax
has very useful tidbits, like
Don't Want Spell Checking In Comments? ~
Some folks like to include things like source code in comments and so would
prefer that spell checking be disabled in comments in LaTeX files. To do
this, put the following in your <.vimrc>: >
let g:tex_comment_nospell= 1
You'll have to figure out whether you can use that/extrapolate from there
I had the same problem (VIM 7.3), but this post at the vim-latex-devel mailing list provided the clue. To get the spell checking working, I had to put
syntax spell toplevel
in my ~/.vimrc, but it has to be declared after
syntax enable
or
syntax on
for it to work.
I don't whether this is a crude hack and the intended solution, but I created a file called .vim/after/syntax/tex.vim
containing the single line:
syn match texText "\<\w\+\>" contains=#Spell
Now vim spell checks the normal text between the commands and the text passed as parameters, because you cannot differentiate them syntacticly:
\frametitle{TextToBeChecked}
\pgfuseimage{VariableNotToBeChecked}
Hence, it checks way too much in my preamble. But I have it located in another file, so I don't really care.
This problem often occurs when working with files which are included by a master document.
If you are opening a TeX file which will be included and does not contain a section, chapter, \begin{document}, ... you can mark it by adding %begin-include at the top of the file. This way vim recognizes the file content as being part of the texDocZone region, which enables spellchecking.
With %end-include you can set the end of the texDocZone.
This behavior seems not to be documented, but is described in the vim syntax file: https://github.com/vim/vim/blob/master/runtime/syntax/tex.vim
tl;dr: Add %begin-include to the top of your tex file.
I encountered the same problem -- the .tex file for the first chapter of my book spell checked normally, but the file for the second chapter would only spell check the comments. This apparently happens because vim isn't looking at enough lines of context and gets confused. Ingo Karkat's solution here fixed it for me. Specifically, I used:
syn sync maxlines=2000
syn sync minlines=500
in ~/.vim/after/syntax/tex.vim
tldr; don't put \section commands in your master .tex document
I made the same observation and I also would insist on "before, it worked and I didn't change anything".
Then I observed that this unwanted behaviour only occurred in .tex documents where I have a master.tex that includes chapters as separate chapterx.tex files. Moreover, everything works fine if all the \section definitions are in the chapterx.tex's and NOT in the master.tex.
Otherwise, as I think, the vim syntax and spell checking routines have a hard time determining in which region they are, cf. this question Vim spellcheck not always working in .tex file. Check region in Vim
I found the same problem, but a different solution to it. In some .tex files the spell-check was working as expected others not (documentclass{scrlttr2}). Only in the comments words were underlined ... So I compared the headers of one working and one not working .tex document. I found a surprisingly texblock, which were hindering the spell check in the document itself:
\usepackage{array}
\newenvironment{Conditio}
{\par\vspace{\abovedisplayskip}\noindent\begin{tabular}{>{$}l<{$} #{${}={}$} l}}
{\end{tabular}\par\vspace{\belowdisplayskip}}
And this code was only an "hangover" of a different juridical text, I edited before. Commenting it out, set the normal spell-check with high-lighted texts in the letter document. (MacVim 8.1 latex-suite macOS 10.13.6 vim-latex v1.10.0)
None of the answers here worked for me, but I could see if I typed syntax spell as a command in vim it would work.
It turns out it was because syntax wasn't being set for the tex filetype automatically, and I found the solution here.
vim only spell checking in TeX comments
Editing a LaTeX document with vim, I noticed it was only spell-checking comments. I fixed this
by add this to my ./vimrc:
autocmd FileType plaintex,tex,latex syntax spell toplevel

Resources