Skip to content

Question: How to exclude one single file #637

Description

@dzintars

Is there easy way to exclude just one random file from formatting?
I don't want to turn off the formatter. I don't want to get distracted with configuring Nvim config.
I would love to have some kind of modeline option to tell "Don't touch this file" by [this, this and this] formatter.
Is this somehow possible?

Activity

  1. PowerUser64 commented on Feb 12, 2025

    @PowerUser64

    Doing noau w works as a one-off way to write without conform jumping in, but I think there isn't a way to exclude a file. A way to do it with a modeline would be nice.

  2. stevearc commented on Feb 13, 2025

    @stevearc
    Owner

    Yes, :noau w for one-offs and if you want to have a more durable way to disable formatting for a specific file look at this recipe: https://github.com/stevearc/conform.nvim/blob/master/doc/recipes.md#autoformat-with-extra-features
    It allows you to set a buffer-local variable (vim.b.disable_autoformat) to disable formatting for a file.

  3. PowerUser64 commented on Feb 16, 2025

    @PowerUser64

    I just realized, a way to do this in a modeline would be to write an autocommand that runs for filetypes nofmt_* (eg. nofmt_cpp), enables vim.b.disable_autoformat (linked in @stevearc's comment), and sets the filetype to the version of the filetype without the nofmt_ at the start. You would then put the nofmt_cpp as the filetype in the modeline, and then the autocommand would run and do its thing! I don't need to do this myself, since I've been using noau w lately, but maybe this will be helpful to someone else!

  4. danielyrovas commented on Sep 11, 2026

    @danielyrovas

    I was also after a solution and came up with this:

    -- in plugin/autocmds.lua
    vim.api.nvim_create_autocmd("BufReadPost", {
        group = vim.api.nvim_create_augroup("read_buf_autoformat_config", { clear = true }),
        callback = function(args)
            local lines = vim.api.nvim_buf_get_lines(args.buf, 0, 9, false)
    
            for _, line in ipairs(lines) do
                if line:match "nvim:%s*noautoformat" then
                    vim.b[args.buf].disable_autoformat = true
                    break
                end
            end
        end,
    })

    This enables putting nvim: noautoformat near the top of a file to turn it off for the buffer.

    It only works if you use something like the format_on_save setup from the previous comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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