MaiaVictor
MaiaVictor

Reputation: 53047

How do I configure VIM to use different syntaxes in different parts of a file?

I have a file that works like this:

:: JavaScript
function whatIsThis(){
    return "an example";
}

:: Haskell
main = putStrLn "is this is an example?"

:: C
#include <stdio.h>
int main(){
    printf("yes, it is");
};

In other words, a line starting with :: determines the syntax of the next lines until another :: is found. Is there any way to configure vim to highlight the syntax of those blocks correctly?

Upvotes: 1

Views: 123

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172738

If you want to have full editing capabilities for each separate part (i.e. filetype-specific settings, customizations, mappings, commands), not just specific syntax highlighting, you won't get around having a separate buffer for each fragment (as these things are all tied to the buffer's filetype).

With the NrrwRgn - A Narrow Region Plugin similar to Emacs, you can cut up the original file into separate buffers (shown e.g. in split windows), and the plugin syncs any changes back to the original automatically.

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172738

Have a look at my SyntaxRange plugin; it offers both a :[range]SyntaxInclude command, and functions to define regions (based on start and end patterns) to highlight with a particular syntax. For your example:

call SyntaxRange#IncludeEx('start="^:: Haskell" end="^::"me=e-3', 'haskell')
call SyntaxRange#IncludeEx('start="^:: C" end="^::"me=e-3', 'c')
call SyntaxRange#IncludeEx('start="^:: JavaScript" end="^::"me=e-3', 'javascript')

Upvotes: 3

Related Questions