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.

Tommaso Thea
  • 153
  • 1
  • 7
  • Just use :e (after writing the file) to cause the buffer to reload and re-detect the filetype. This is almost a duplicate of: http://vi.stackexchange.com/questions/444/how-do-i-reload-the-current-file but, that one asks about the file being modified outside of Vim. – Tommy A Apr 08 '17 at 21:50
  • @TommyA Thanks, but why does it complain I haven't saved before doing using :e? – Tommaso Thea Apr 08 '17 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. – Tommaso Thea Apr 08 '17 at 21:54
  • No worries. I'm interested in helping and learning, not the points :-) – Tommy A Apr 08 '17 at 21:56
  • @TommyA Thanks again for the help, have a nice day. – Tommaso Thea Apr 08 '17 at 21:58
  • 1
    Actually, :filetype detect, while longer, is my preferred option – D. Ben Knoble Apr 10 '17 at 02:16
  • @TommyA If you're interested in helping then you should leave an answer to make it easier for future readers. :) – Tumbler41 Apr 11 '17 at 20:18
  • @Tumbler41 I don't think turning that comment into an answer would add more value. Feel free to post an answer if you disagree. – Tommy A Apr 11 '17 at 22:36

1 Answers1

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>
Tumbler41
  • 7,746
  • 1
  • 21
  • 48