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:
- File extension -
.ts,.py,.rs, etc. - Filename -
Makefile,Dockerfile,.gitignore - Shebang -
#!/usr/bin/env python - 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:
Popular Languages
- 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:
- Treesitter parsing - Syntax highlighting via Treesitter
- Theme colors - Language-specific color schemes
- 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
- vim.api - Autocommand and buffer APIs
- vim.highlight - Syntax highlighting
- vim.opt - Editor options
- Configuration Guide - Per-filetype configuration