112 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 ...
75 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 ...
  • 1,704
35 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) ...
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 ...
  • 33.3k
29 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 ...
  • 6,470
29 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 ...
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 (...
  • 2,658
22 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
  • 1,112
18 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 ...
  • 10.4k
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 ...
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 --- ...
  • 24k
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 ...
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.
  • 13.9k
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 ...
  • 1,835
14 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. ...
  • 28.9k
14 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 ...
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/...
  • 306
13 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, ...
  • 18.5k
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 ...
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,*...
  • 471
12 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 ...
  • 229
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 ...
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 ...
  • 10.4k
10 votes

Blocking changes to read only file

Add this to your .vimrc: autocmd BufRead * let &l:modifiable = !&readonly
  • 286
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 " ...
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 ...
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 ...
  • 2,359
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 ...
  • 16.9k
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 ...
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 ...
  • 191

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