How to Not Require JS Extension in Vi
In this article
When editing JavaScript files in vi or vim, you don't need to manually specify the .js extension every time you save a file. By configuring your editor properly, you can create and edit JavaScript files using shorter, cleaner names—like script instead of script.js—and still retain full syntax highlighting, indentation, and other language-specific features. This is achieved by setting up file type detection rules in vi/vim that associate certain file patterns or contents with the JavaScript language, regardless of the file extension. Doing so improves workflow efficiency and reduces typing without sacrificing functionality.
What You'll Need
- A working installation of vi or, preferably, vim (version 7.0 or higher)
- Access to your user home directory to edit or create the
.vimrcconfiguration file - Basic understanding of command-line text editing
- Write permissions for creating or modifying files in your home directory
Step-by-Step
- Open your terminal and launch vi by typing
vi ~/.vimrc. If the file doesn’t exist, vi will create it when you save. - Enter insert mode by pressing
i. - Add the line
autocmd BufNewFile,BufRead * javascript setf javascriptto automatically detect and apply JavaScript syntax rules to any file, regardless of extension. This is a broad rule and should be used cautiously (see tips). - Alternatively, for more control, use file name patterns. For example, to treat all files starting with
js_as JavaScript, add:autocmd BufNewFile,BufRead js_* setf javascript. - Or, detect JavaScript based on file content. Add this line to recognize files with typical JS syntax:
autocmd BufNewFile,BufRead * if getline(1) =~ '^[ \t]*\(var\|let\|const\|function\|import\|export\)' | setf javascript | endif. - To detect JavaScript via shebang lines (e.g.,
#!/usr/bin/env node), use:autocmd BufNewFile,BufRead * if getline(1) =~ '^#!.*\/bin\/env.*node' | setf javascript | endif. - Ensure syntax highlighting is enabled globally by adding
syntax onif not already present. - Enable file type detection and plugins by adding:
filetype onandfiletype plugin on. These lines are essential for any file type automation to work. - Save the file by pressing
Esc, then typing:wqand pressingEnter. - Test the configuration by creating a test file:
vi myscript. Enter some JavaScript code likeconst greet = () => console.log('Hello');. Exit and reopen to verify syntax highlighting and file type are applied correctly. - Use
:set filetype?while in the buffer to confirm the file type is set tojavascript.
Common Mistakes
- Forgetting to enable file type detection: Without
filetype on, auto-commands will not trigger. This is the most common reason configurations fail. - Overly broad file matching: Using
*to match all files can cause non-JavaScript files to be highlighted incorrectly, leading to confusion and formatting issues. - Incorrect regular expressions: Miswritten patterns in
getline()checks may fail to match valid JavaScript content, especially if whitespace or syntax varies. - Not escaping slashes in paths: In shebang detection, forward slashes in
/usr/bin/envmust be escaped as\/in the regex. - Editing the wrong vimrc: Some systems have
.vimrc, others use.vim/vimrc. Always edit the one in your home directory unless you have a specific setup.
Tips
- Use specific file naming patterns (e.g.,
js_*or*.jslocal) instead of applying rules to all files. This avoids unintended side effects. - Combine multiple detection methods. For example, use both shebang and content checks for greater reliability.
- Add
set expandtab tabstop=2 shiftwidth=2in the same auto-command to standardize JavaScript indentation when working in Node.js or browser environments. - Use
autocmd FileType javascriptto run additional settings only when a file is recognized as JavaScript, such as enabling linters or custom key mappings. - Test changes incrementally. Add one rule at a time and verify it works before adding more.
Troubleshooting
If JavaScript syntax is not being applied, first check that filetype on and syntax on are present in your .vimrc. Run :filetype in vi to confirm file type detection is active. If it shows filetype detection: OFF, re-add the line and restart vi. Use :scriptnames to list all loaded scripts and confirm your .vimrc was read. If using content-based detection, ensure the first line contains matching text. For shebang detection, verify the first line starts exactly with #!/usr/bin/env node or similar. If rules still don’t trigger, simplify the auto-command to test with a known file name pattern first. You can also add echom "JavaScript rule triggered" inside the auto-command to debug execution.
Frequently Asked Questions
Can I use this method for other languages?
Yes, the same technique applies to any language by replacing javascript with the appropriate file type, such as python or html.
Will this affect performance?
Minimal impact occurs with simple patterns. Complex content checks on large files may cause slight delays, but generally, the effect is negligible.
Do I need to restart vi after editing .vimrc?
No, you can reload the configuration by running :source ~/.vimrc inside vi.
What if my file has no extension and no shebang?
Use content-based detection or name the file with a recognizable prefix to ensure proper type assignment.
Can I disable this for specific files?
Yes, manually set the file type with :setf plaintext or use autocommands with negative conditions to exclude certain names or paths.
Is this safe for team environments?
Yes, since .vimrc is user-local, it won’t affect others. However, avoid overly aggressive rules that could misclassify files.