Skip to content

Add vim/emacs modeline support#41899

Closed
elmarco wants to merge 5 commits intozed-industries:mainfrom
elmarco:modeline
Closed

Add vim/emacs modeline support#41899
elmarco wants to merge 5 commits intozed-industries:mainfrom
elmarco:modeline

Conversation

@elmarco
Copy link
Copy Markdown
Contributor

@elmarco elmarco commented Nov 4, 2025

Many editors such as vim and emacs support "modelines", a comment at the beginning of the file that allows the file type to be explicitly specified along with per-file specific settings

  • The amount of configurations, style and settings mapping cannot be handled in one go, so this opens up a lot of potential improvements.
  • I left out the possiblity to have "zed" specific modelines for now, but this could be potentially interesting.
  • Mapping the mode or filetype to zed language names isn't obvious either. We may want to make it configurable.

This is my first contribution to zed, be kind. I struggled a bit to find the right place to add those settings. I use a similar approach as done with editorconfig (merge_with_editorconfig). There might be better ways.

Closes #4762

Release Notes:

  • Add basic emacs/vim modeline support.

elmarco and others added 3 commits November 4, 2025 17:30
This will allow to configure vim/emacs modeline support.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Co-authored-by: Claude <noreply@anthropic.com>
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Nov 4, 2025
elmarco and others added 2 commits November 4, 2025 18:08
vim and emacs support "modelines", a comment at the beginning of the
file that allows the file type to be explicitly specified along with
per-file specific settings.

Release Notes:

- Add basic emacs/vim modeline support.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Co-authored-by: Claude <noreply@anthropic.com>
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
@ConradIrwin
Copy link
Copy Markdown
Member

Amazing, thank you for this! I think the overall structure is reasonable, and I like that we're doing this as part of detect language.

Looking through the code I have a few suggestions:

  • Let's hard limit the number of bytes to search for modeline to ~1k, we've had persistent problems with performance of files with extremely long lines (we could maybe do the emacs thing and make it ~3k, but seems a bit excessive). If we do that then I don't think we need the setting for number of lines.
  • We should store the ModelineSettings as an Rc in the Buffer and BufferSnapshot to make it cheaper to create a new snapshot (another area of performance sensitivity for us).
  • We need some docs! Maybe a new page describing what variables can be set that's linked to from the existing settings pages?
  • Let's (probably) not ship the "emacs locals" syntax. I've seen modelines before, but never that, and don't want to maintain a third parser for this stuff.
  • I'd also like to tighten up the syntax for vim. Not sure what's actually used in the wild but in my experience it's always " vim?:. I don't think we need to support ex:`, nor doing this on lines with no comments (and particularly not lines with other non-modeling content before the modeline!).

Thanks again, and let me know if you want help with any of these: https://cal.com/conradirwin/pairing

@ConradIrwin
Copy link
Copy Markdown
Member

(Also feel free to edit typos.toml to fix the style check)

@elmarco
Copy link
Copy Markdown
Contributor Author

elmarco commented Nov 6, 2025

Hi @ConradIrwin ,

  • I thought the file was already preloaded, and thus it would not be a big deal to limit by number of lines, which is a bit easier to configure or tweak than a fixed number of Kb. Or do you think that feature shouldn't be configurable?
  • Ok to wrap with Rc, yes
  • Ok for the docs, good idea
  • The emacs "Local Variables:" is a bit esoteric, but is much easier than parsing vim modelines (and thus easy to maintain)! A quick search in github reveal that many projects use them (https://github.com/search?type=code&q=%22Local+Variables%3A%22, in particular GNU projects: glibc, bison, guile, emacs etc). Nevertheless, I can drop support for it.
  • I am afraid that the vim syntax diversity is also very wide. And there are many valid cases where there are extra lines without vim modeline: shebang, and other modelines when using both emacs and vim for example, which is very common.

btw, I wonder if you would rather use it as a third-party crate? we could even make it compile-time optional? wdyt

It would be great to experience pairing, I haven't tried it and I am sure I could learn a ton from this experience. Let see if we can find some time for it.

@ConradIrwin
Copy link
Copy Markdown
Member

The file will be loaded, but line 1 could be 2Gb long (think a JSON export); which you probably don't want to run a regex on on the main thread (particularly if the mode-line regexes are not anchored to the start). Limiting to a few kb prevents that being slower than it is for more normal files which avoids surprisingly slowdowns. If we limit by kb, I don't think we need the "number of lines" check either.

I don't think we need to make the number of bytes configurable, as long as it's easy to explain (e.g. emacs' 3k limit).

I'd rather have the code in the Zed repo than as a crate because it's easier to change and test it.

https://cal.com/conradirwin/pairing has some times when I'm free for pairing.

@ConradIrwin
Copy link
Copy Markdown
Member

Closing through lack of response, but happy to review new versions of this

@ConradIrwin ConradIrwin closed this Dec 2, 2025
@github-project-automation github-project-automation bot moved this from Community PRs to Done in Quality Week – December 2025 Dec 2, 2025
ConradIrwin added a commit that referenced this pull request Jan 23, 2026
Many editors such as vim and emacs support "modelines", a comment at the
beginning of the file that allows the file type to be explicitly
specified along with per-file specific settings

- The amount of configurations, style and settings mapping cannot be
handled in one go, so this opens up a lot of potential improvements.
- I left out the possiblity to have "zed" specific modelines for now,
but this could be potentially interesting.
- Mapping the mode or filetype to zed language names isn't obvious
either. We may want to make it configurable.

This is my first contribution to zed, be kind. I struggled a bit to find
the right place to add those settings. I use a similar approach as done
with editorconfig (merge_with_editorconfig). There might be better ways.

Closes #4762

Release Notes:

- Add basic emacs/vim modeline support.

Supersedes #41899, changes:
- limit reading to the first and last 1kb
- add documentation
- more variables handled
- add Arc around ModelineSettings to avoid extra cloning
- changed the way mode -> language mapping is done, thanks to
`modeline_aliases` language config
- drop vim ex: support
- made "Local Variables:" handling a separate commit, so we can drop it
easily
- various code style improvements

---------

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
elmarco added a commit to elmarco/zed that referenced this pull request Jan 23, 2026
Many editors such as vim and emacs support "modelines", a comment at the
beginning of the file that allows the file type to be explicitly
specified along with per-file specific settings

- The amount of configurations, style and settings mapping cannot be
handled in one go, so this opens up a lot of potential improvements.
- I left out the possiblity to have "zed" specific modelines for now,
but this could be potentially interesting.
- Mapping the mode or filetype to zed language names isn't obvious
either. We may want to make it configurable.

This is my first contribution to zed, be kind. I struggled a bit to find
the right place to add those settings. I use a similar approach as done
with editorconfig (merge_with_editorconfig). There might be better ways.

Closes zed-industries#4762

Release Notes:

- Add basic emacs/vim modeline support.

Supersedes zed-industries#41899, changes:
- limit reading to the first and last 1kb
- add documentation
- more variables handled
- add Arc around ModelineSettings to avoid extra cloning
- changed the way mode -> language mapping is done, thanks to
`modeline_aliases` language config
- drop vim ex: support
- made "Local Variables:" handling a separate commit, so we can drop it
easily
- various code style improvements

---------

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
elmarco added a commit to elmarco/zed that referenced this pull request Jan 24, 2026
Many editors such as vim and emacs support "modelines", a comment at the
beginning of the file that allows the file type to be explicitly
specified along with per-file specific settings

- The amount of configurations, style and settings mapping cannot be
handled in one go, so this opens up a lot of potential improvements.
- I left out the possiblity to have "zed" specific modelines for now,
but this could be potentially interesting.
- Mapping the mode or filetype to zed language names isn't obvious
either. We may want to make it configurable.

This is my first contribution to zed, be kind. I struggled a bit to find
the right place to add those settings. I use a similar approach as done
with editorconfig (merge_with_editorconfig). There might be better ways.

Closes zed-industries#4762

Release Notes:

- Add basic emacs/vim modeline support.

Supersedes zed-industries#41899, changes:
- limit reading to the first and last 1kb
- add documentation
- more variables handled
- add Arc around ModelineSettings to avoid extra cloning
- changed the way mode -> language mapping is done, thanks to
`modeline_aliases` language config
- drop vim ex: support
- made "Local Variables:" handling a separate commit, so we can drop it
easily
- various code style improvements

---------

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
elmarco added a commit to elmarco/zed that referenced this pull request Jan 28, 2026
Many editors such as vim and emacs support "modelines", a comment at the
beginning of the file that allows the file type to be explicitly
specified along with per-file specific settings

- The amount of configurations, style and settings mapping cannot be
handled in one go, so this opens up a lot of potential improvements.
- I left out the possiblity to have "zed" specific modelines for now,
but this could be potentially interesting.
- Mapping the mode or filetype to zed language names isn't obvious
either. We may want to make it configurable.

This is my first contribution to zed, be kind. I struggled a bit to find
the right place to add those settings. I use a similar approach as done
with editorconfig (merge_with_editorconfig). There might be better ways.

Closes zed-industries#4762

Release Notes:

- Add basic emacs/vim modeline support.

Supersedes zed-industries#41899, changes:
- limit reading to the first and last 1kb
- add documentation
- more variables handled
- add Arc around ModelineSettings to avoid extra cloning
- changed the way mode -> language mapping is done, thanks to
`modeline_aliases` language config
- drop vim ex: support
- made "Local Variables:" handling a separate commit, so we can drop it
easily
- various code style improvements

---------

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
ConradIrwin added a commit that referenced this pull request Mar 25, 2026
Many editors such as vim and emacs support "modelines", a comment at the
beginning of the file that allows the file type to be explicitly
specified along with per-file specific settings

- The amount of configurations, style and settings mapping cannot be
handled in one go, so this opens up a lot of potential improvements.
- I left out the possiblity to have "zed" specific modelines for now,
but this could be potentially interesting.
- Mapping the mode or filetype to zed language names isn't obvious
either. We may want to make it configurable.

This is my first contribution to zed, be kind. I struggled a bit to find
the right place to add those settings. I use a similar approach as done
with editorconfig (merge_with_editorconfig). There might be better ways.

Closes #4762

Release Notes:

- Add basic emacs/vim modeline support.

Supersedes #41899, changes:
- limit reading to the first and last 1kb
- add documentation
- more variables handled
- add Arc around ModelineSettings to avoid extra cloning
- changed the way mode -> language mapping is done, thanks to
`modeline_aliases` language config
- drop vim ex: support
- made "Local Variables:" handling a separate commit, so we can drop it
easily
- various code style improvements

---------

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement

Projects

Development

Successfully merging this pull request may close these issues.

Support modeline comments for per-file settings

3 participants