3

I always prefer shebangs over extensions.

A lot of times I make a new file without an extension in vim, which gets opened as plain text, since no extension is provided, which is completely normal.

The first thing I write is a shebang, but the syntax doesn't get highlighted (even though the language is supported), closing and reopening the file is annoying.

Since I'm new to vim, I'm asking you vim lovers if there is a command which I can use, or maybe some modifications to my .vimrc file I can make to fix this issue.

8
  • Just use :e (after writing the file) to cause the buffer to reload and re-detect the filetype. This is almost a duplicate of: vi.stackexchange.com/questions/444/… but, that one asks about the file being modified outside of Vim.
    – Tommy A
    Apr 8, 2017 at 21:50
  • @TommyA Thanks, but why does it complain I haven't saved before doing using :e? Apr 8, 2017 at 21:53
  • @TommyA Never mind I have read your comment again and now it makes sense. Feel free to make it an answer, I will upvote and accept. Apr 8, 2017 at 21:54
  • No worries. I'm interested in helping and learning, not the points :-)
    – Tommy A
    Apr 8, 2017 at 21:56
  • 1
    Actually, :filetype detect, while longer, is my preferred option
    – D. Ben Knoble
    Apr 10, 2017 at 2:16

1 Answer 1

8

If the file has been saved the easiest way is simply to run:

:e

However, if the file has not been saved, this will result in an error. In this case a different command can be run:

:filetype detect

If this seems like too much to type, a mapping can easily be made and placed in your vimrc:

nnoremap <F5> :filetype detect<CR>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.