{ ... }:
{
  programs.nixvim = {
    autoCmd = [
      ## Close nvim on last buffer closed, not leaving neovim-tree open
      {
        event = [ "BufEnter" ];
        pattern = [ "NvimTree_*" ];
        callback = {
          __raw = ''
            function()
              local layout = vim.api.nvim_call_function("winlayout", {})
              if layout[1] == "leaf" and vim.api.nvim_buf_get_option(vim.api.nvim_win_get_buf(layout[2]), "filetype") == "NvimTree" and layout[3] == nil then vim.cmd("confirm quit") end
            end
          '';
        };
      }
      ## Go to same line in file next time it is open
      {
        event = [ "BufReadPost" ];
        pattern = [ "*" ];
        callback = {
          __raw = ''
            function()
              if vim.fn.line("'\"") > 1 and vim.fn.line("'\"") <= vim.fn.line("$") then
                vim.api.nvim_exec("normal! g'\"",false)
              end
            end
          '';
        };
      }
      ## Highlight tabs and trailing whitespace
      {
        event = [ "BufEnter" ];
        pattern = [ "*" ];
        callback = {
          __raw = ''
            function()
              vim.cmd([[
                if exists('w:extratabs')
                  call matchdelete(w:extratabs)
                  unlet w:extratabs
                endif
                if exists('w:trailingwhitespace')
                  call matchdelete(w:trailingwhitespace)
                  unlet w:trailingwhitespace
                endif
                highlight ExtraTabs ctermbg=red guibg=red
                highlight TrailingWhitespace ctermbg=red guibg=red
                if &ft != 'help'
                  let w:extratabs=matchadd('ExtraTabs', '\t\+')
                  let w:trailingwhitespace=matchadd('TrailingWhitespace', '\s\+$')
                endif
              ]])
            end
          '';
        };
      }
      ## Trim tailing whitespace on save
      {
        event = [ "BufWritePre" ];
        pattern = [ "*" ];
        callback = {
          __raw = ''
            function()
              vim.cmd([[
                if &ft =~ 'javascript\|html\|jade\|json\|css\|less\|php\|python\|sh\|c\|cpp\|markdown\|yaml\|vim\|nix'
                  :%s/\s\+$//e
                elseif expand('%:t') =~ '\.gltf$' || expand('%:t') =~ '\.glsl$'
                  :%s/\s\+$//e
                endif
              ]])
            end
          '';
        };
      }
    ];
  };
}