Skip to content

Add modeline to set filetype to help automatically #12

Description

@gbroques

Usally vim help docs have a modeline similar to the following at the bottom of the document for automatically setting options:

vim:tw=78:ts=8:noet:ft=help:norl:

See :help help-writing and :help modeline for details.

The important bit is ft=help to set the filetype to help.

This ensures proper syntax highlighting and other helpful features that plugins or autocommands may provide to help docs by detecting the filetype of the buffer.


An explanation of the above modeline is as follows:

vim: This indicates that the following text contains Vim modeline options.
tw=78: This sets the textwidth option to 78 characters. This means that Vim will automatically wrap lines when they exceed 78 characters. This is common in help files to ensure readability.
ts=8: This sets the tabstop option to 8. This means that each tab character will be displayed as 8 spaces wide.
noet: This is short for noexpandtab. It prevents Vim from expanding tab characters into spaces when they are inserted.
ft=help: This sets the filetype option to help. This tells Vim to apply syntax highlighting and other settings specific to Vim help files.
norl: This is short for norelativenumber. It disables the relativenumber option, which would display line numbers relative to the current cursor position. In help files, absolute line numbers are typically preferred or no line numbers at all.


As a workaround, I have the following in my lazy vim config:

{
    'milisims/nvim-luaref',
    commit = '9cd3ed50d5752ffd56d88dd9e395ddd3dc2c7127',
    config = function()
      -- Need to add modeline to set ft=help for automatically maximizing window,
      -- and providing syntax highlighting.
      -- See https://github.com/emiasims/nvim-luaref/issues/12
      vim.api.nvim_create_autocmd('BufWinEnter', {
        desc = 'Set the filetype to help for Lua reference',
        group = vim.api.nvim_create_augroup('nvim-luaref', { clear = true }),
        pattern = 'lua_reference.txt',
        callback = function()
          vim.o.filetype = 'help'
        end
      })
    end
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions