Vimcraft Docs / vim.filetype - File Type Detection

vim.filetype - File Type Detection

Automatic file type detection supporting approximately 1,200 programming languages and file formats. Powered by go-enry and Neovim's filetype detection rules.

How It Works

Vimcraft automatically detects file types when you open a file based on:

  1. File extension - .ts, .py, .rs, etc.
  2. Filename - Makefile, Dockerfile, .gitignore
  3. Shebang - #!/usr/bin/env python
  4. Content patterns - First few lines of the file

Getting File Type

Current Buffer

// FileType is detected automatically on buffer load
vim.api.createAutocmd('FileType', {
  callback: (args) => {
    console.log('File type:', args.match);
  }
});

Using Buffer Options

const filetype = vim.api.bufGetOption(0, 'filetype');
console.log('Current filetype:', filetype);

Setting File Type

Manually Override

// Set filetype for current buffer
vim.api.bufSetOption(0, 'filetype', 'typescript');

Via Autocommand Pattern

// Custom filetype for specific extensions
vim.api.createAutocmd(['BufRead', 'BufNewFile'], {
  pattern: '*.mdx',
  callback: () => {
    vim.api.bufSetOption(0, 'filetype', 'markdown');
  }
});

FileType Autocommands

React to file type detection:

// TypeScript-specific settings
vim.api.createAutocmd('FileType', {
  pattern: 'typescript',
  callback: () => {
    vim.opt.tabStop = 2;
    vim.opt.shiftWidth = 2;
    vim.opt.expandTab = true;
  }
});

// Python-specific settings
vim.api.createAutocmd('FileType', {
  pattern: 'python',
  callback: () => {
    vim.opt.tabStop = 4;
    vim.opt.shiftWidth = 4;
    vim.opt.expandTab = true;
  }
});

// Multiple filetypes
vim.api.createAutocmd('FileType', {
  pattern: ['javascript', 'typescript', 'json'],
  callback: () => {
    vim.opt.tabStop = 2;
  }
});

Supported Languages

Vimcraft supports detection for ~1,200 file types including:

  • JavaScript, TypeScript, JSX, TSX
  • Python, Ruby, Go, Rust
  • C, C++, C#, Java, Kotlin
  • PHP, Swift, Objective-C
  • HTML, CSS, SCSS, Less

Configuration & Data

  • JSON, YAML, TOML, XML
  • Markdown, reStructuredText
  • Dockerfile, docker-compose
  • Makefile, CMakeLists.txt

Shell & Scripts

  • Bash, Zsh, Fish
  • PowerShell, Batch

Markup & Documentation

  • LaTeX, BibTeX
  • AsciiDoc, Org-mode

Filetype-Specific Keymaps

Create keymaps that only work in certain filetypes:

vim.api.createAutocmd('FileType', {
  pattern: 'typescript',
  callback: () => {
    vim.keymap.set('n', '<leader>r', () => {
      vim.cmd('!npx ts-node %');
    }, { buffer: true, desc: 'Run TypeScript file' });
  }
});

vim.api.createAutocmd('FileType', {
  pattern: 'python',
  callback: () => {
    vim.keymap.set('n', '<leader>r', () => {
      vim.cmd('!python3 %');
    }, { buffer: true, desc: 'Run Python file' });
  }
});

Integration with Syntax Highlighting

File type detection automatically activates:

  1. Treesitter parsing - Syntax highlighting via Treesitter
  2. Theme colors - Language-specific color schemes
  3. Indentation rules - Smart indentation per language
// Customize highlighting per filetype
vim.api.createAutocmd('FileType', {
  pattern: 'markdown',
  callback: () => {
    vim.highlight('markdownH1', { fg: '#61afef', bold: true });
    vim.highlight('markdownLink', { fg: '#98c379', underline: true });
  }
});

See Also