vim syntax highlight not working (MacOs Ventura) - vim

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!

Related

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.

Vim doesn't highlight syntax after using "tabe" open multiple files

I just started to learn Vim, and I found it is very annoying that vim dose not highlight syntax after using tabe command to open multiple files. Any solutions for that?
Let me explain more details of this situation.
I open file A using vim
I type ":tabe file B" to open the second file.
Two files now are opened in a same vim window. However, only file A has syntax highlight, file B does not have syntax highlight. Same situation also happens in ":sp file B".
If that really happens, then you found a bug in vim! But before we jump to any conclusions, my advice is that you first try to debug your vimrc: vi.stackexchange.com/questions/2003/how-do-i-debug-my-vimrc-file
BTW, it is very annoying that every newbie vim user tries to make it behave like every other graphical editor. So another suggested read: stackoverflow.com/questions/102384/using-vims-tabs-like-buffers

setting custom arguments in vimrc

I've tried looking everywhere for information on this but have come up short. I want to be able to use vimrc to set my own arguments for opening vim with. The idea being that if I run "vim -#code foo.bar" then the vimrc file will set syntax highlighting and line numbers but if run "vim foo.bar" then the file will open without line numbers or syntax highlighting. This seems like an obvious thing to want to do and I'm sure I've missed a trick somewhere but I'm struggling to get vim to play nicely. It seems silly to have to set a bash alias for this when the vimrc file is designed for this kind of thing.
My vimrc currently looks like this:
if $ARGV[0] == "#code"
set nu
filetype plugin on
syntax on
endif
I have a workaround for similar situation
I have special vimrc (.coding_vimrc)
wich loads usual vimrc inside
source .vimrc
and contains all special settings for coding
Run vim with
vim -u .coding_vimrc foo.bar
PS:
assumes full path in both cases

Vim directory with macvim/terminalvim

I am using mac vim in terminal via /Applications/MacVim.app/Contents/vim
By now I didn't really care about what was in the MacVim directory in /Applications/MacVim.app/Contents/Resources/vim/runtime
and still somehow I had proper syntax highlighting etc.. I guess my vim loaded the files from macvim?
Somehow I messed arround with snipmate and all the syntax highlighting stopped to work. Filetypes are correct, but no highlighting. So I copied the syntax folder from the macvim app to my ~/.vim/syntax and it worked like a charm. Is there a way how I can use the ones from the app again?
Thanks for any help
Copying the syntax to your own ~/.vim/ directory effectively forks the factory-defaults. The downside is that you now have to update your copy whenever Macvim is updated.
The key to figuring out the problems is the 'runtimepath' setting. By modifying that (in your ~/.vimrc), you should be able to include the proper runtime files (and that is not just syntax, but also filetype plugins, should you have :filetype plugin on). BTW, it's unlikely this is caused by snipMate.
:set runtimepath?
Also, the :scriptnames command tells you exactly which scripts have been sourced so far.

Syntax Highlighting in VIM after :bd

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.

Resources