Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.
111 votes
Accepted

How to edit binary files with Vim?

The simplest way is to use the binary option. From :help binary: This option should be set before editing a binary file. You can also use the -b Vim argument. When this option is switched on a few ...
user avatar
74 votes
Accepted

What is the difference between `filetype plugin indent on` and `filetype indent on`?

filetype plugin indent on is like a combination of these commands: filetype on filetype plugin on filetype indent on It turns on "detection", "plugin" and "indent" at once. You can check for ...
user avatar
  • 1,664
34 votes
Accepted

Is it possible and useful to split vimrc?

Yes this is possible and useful and even considered best practice. They are called filetype plugins in Vim speech. And Vim even comes with many filetype plugins (as well as indent and syntax files) ...
user avatar
32 votes
Accepted

How can I change the default indentation based on filetype?

The best method is to put those settings in ~/.vim/after/ftplugin/<filetype>.vim. For HTML (assuming you want 4 characters-wide tabs): ~/.vim/after/ftplugin/html.vim setlocal shiftwidth=4 ...
user avatar
  • 32.6k
28 votes
Accepted

How do I tell vim that some file extensions are synonymous?

The preferred approach is to create a ~/.vim/filetype.vim, as mentioned in :help 43.2 and explained in Vim FAQ 26.8: A better alternative to the above approach is to create a filetype.vim file in ...
user avatar
  • 6,370
28 votes

For an autocmd in a ftplugin, should I use pattern matching or <buffer>?

Is this usage of <buffer> correct? I think it is correct, but you just need to wrap it inside an augroup, and clear the latter, to make sure that the autocmd won't be duplicated every time you ...
user avatar
26 votes

How to edit binary files with Vim?

To view the content of a binary file in a hex view, open the file, switch on binary mode, and filter the buffer through the xxd command: :set binary :%!xxd You can make changes in the left area (...
user avatar
  • 2,618
20 votes

How do I tell vim that some file extensions are synonymous?

You could do (in your global .vimrc): autocmd BufEnter *.tpp :setlocal filetype=cpp
user avatar
  • 1,092
17 votes
Accepted

How to tell vim not to try to unzip a file

Functionality like this is handled by autocmds. In order to disable autocmds for a specific command, you can use :noautocmd (abbreviated :noau). In this case :noau e foo.xlsx will simply open the ...
user avatar
  • 10.3k
16 votes
Accepted

Blocking changes to read only file

I found a solution. Setting nomodifiable prevents the file being edited (as used in help windows). I created a simple function to set or unset modifiable depending on if readonly is set and attached ...
user avatar
16 votes
Accepted

Is there any list of filetype extension names?

The reason Vim detects the filetype for *.cpp and not for *.CPP files is that Vim has auto-commands for *.cpp setting the filetype, but nothing for *.CPP: :au BufRead *.cpp --- Auto-Commands --- ...
user avatar
  • 23.7k
15 votes
Accepted

What is the difference between 'set ft=' and 'setfiletype'?

From the :setfiletype help: :setf[iletype] [FALLBACK] {filetype} :setf :setfiletype Set the 'filetype' option to {filetype}, but only if ...
user avatar
14 votes

Open .md files as Markdown

I currently have this line as an ugly hack in my .vimrc: autocmd BufRead *.md set ft=markdown I'm still wondering if there's a better way, though.
user avatar
  • 13.6k
14 votes

How can I change the default indentation based on filetype?

You can do this with autocommands in your .vimrc. For example, I have a function html_like_mode that sets up various things for editing HTML files. In my .vimrc, I have: au BufNewFile,BufRead *.html ...
user avatar
  • 1,815
13 votes
Accepted

How do I run a specific vim command when opening a file with a given extension?

Instead of checking the extension and setting the textwidth accordingly you could simply create a tex.vim (the name must match the filetype) file in .vim/ftplugin/ or for Windows $HOME/vimfiles/...
user avatar
  • 306
13 votes
Accepted

How to create a new vim filetype?

The symlink you've created at ~/.vim/ftplugin/Rhtml.vim tells Vim how to handle files with the RHTML filetype, but you haven't told Vim how to recognise which files should have this filetype. ...
user avatar
  • 27.4k
13 votes

How to create a new vim filetype?

Create ~/.vim/ftdetect/SOME_EXTENSION.vim and add this line to it: autocmd BufNewFile,BufRead *.SOME_EXTENSION set filetype=SOME_FILETYPE So if you wanted vim to use the filetype html for all files ...
user avatar
12 votes

How can I change the default indentation based on filetype?

augroup allows you to group related autocommands into named groups. (See here to learn about Autocommand Groups.) We use augroups to add autocommands to ~/.vimrc in a way that won't add a duplicate ...
user avatar
12 votes

How to tell vim not to try to unzip a file

This seems to the the "zip" plugin, which is shipped with Vim and enabled by default. :help zip has some information about it, among other things: PREVENTING LOADING If for some reason you do ...
user avatar
12 votes
Accepted

Open .md files as Markdown

In the file filetype.vim that is shipped with your distribution you will find a line that looks probably similar to this (on my machine it lives at /usr/share/vim/vim74) : au BufNewFile,BufRead *.md,*...
user avatar
  • 461
11 votes

How to edit binary files with Vim?

TL;DR Answer Open the file with Vim in binary mode: vim -b <file_to_edit> In Vim, get into hex editing mode like so: :%!xxd -p To save: :%!xxd -p -r :w The first line will convert the ...
user avatar
  • 219
11 votes
Accepted

Filetype that LaTeX should be

When there's not enough information to decide otherwise, 'filetype' is set to the most basic -- plaintex. Your existing file must have had enough content for the detection to determine that it was ...
user avatar
  • 10.3k
10 votes
Accepted

Use single ftplugin for more than one filetype

The default ftplugin (/usr/share/vim/vim74/ftplugin/cpp.vim on my system) should already do this: " Only do this when not done yet for this buffer if exists("b:did_ftplugin") finish endif " ...
user avatar
10 votes

Blocking changes to read only file

Add this to your .vimrc: autocmd BufRead * let &l:modifiable = !&readonly
user avatar
  • 286
10 votes
Accepted

Run shell commands on current file based on file extension

You could use :make for this; you can set makeprg (short for make program) to any command. Some examples: au FileType ruby set makeprg=ruby\ % au FileType javascript set makeprg=node\ % au FileType ...
user avatar
10 votes
Accepted

How does Vim know how to syntax-highlight some files with strange extension or without extension at all?

In the file recognition procedure used to detect what syntax highlighting to use, there can be a few kinds of checks. The most known one is checking the the file extension. This is done by a command ...
user avatar
  • 2,309
10 votes

How do I run a specific vim command when opening a file with a given extension?

In ~/.vimrc you can add the following line: au BufReadPost,BufNewFile *.md,*.txt,*.tex setlocal tw=79 For extra commands, separate them by |. This will set your settings right after reading the ...
user avatar
  • 16.5k
10 votes
Accepted

Why is "set noexpandtab" in my ~/.vimrc ignored when I open a file?

It is not ignored, it is overwritten by your filetype plugin. In this case the python plugin. You can verify that like Marth said, with the :verbose command: :verbose set expandtab? If you want to ...
user avatar
9 votes

How to edit binary files with Vim?

Use the "bvi" editor. http://bvi.sourceforge.net/ (It's in every Linux repository.) $ apt-cache show bvi [snip] Description-en: binary file editor The bvi is a display-oriented editor for binary ...
user avatar
  • 191
9 votes
Accepted

How to do file type detection based on file contents (e.g. a certain shebang line)?

If you want to introduce a file type and that file type can only be identified by examining the contents of such files then you'll need to add appropriate logic to a file named scripts.vim. But first, ...
user avatar
  • 18.1k

Only top scored, non community-wiki answers of a minimum length are eligible