7

I've been working with a lot of C++ files that have no extensions and it's too annoying to have to type :set ft=cpp every time I open them, so I'm mostly just working without syntax highlighting. Is there a way to tell vim the file type in the command line? Something like:

$ vim --ft=cpp file_name
9
  • add set ft=cpp to your ~/.vimrc but in this way, all your file opened will be treated as cpp file. Commented Jul 18, 2016 at 15:31
  • 1
    eh... maybe there is another way to do that. alias vimcpp="vim --ft=cpp " Commented Jul 18, 2016 at 15:37
  • 1
    @LeeHoYo - that would work, but then you would need a different alias for each filetype. Expanding on your suggestion, an alternative could be alias vimft="vim --ft=". Then you could just execute vim as vimft cpp. Commented Jul 18, 2016 at 15:40
  • 1
    @SibiCoder - OP specifically mentions that in the post body. Commented Jul 18, 2016 at 15:47
  • 2
    Add this to the end of your file: // vim: ft=cpp. Read :h modeline for details. Commented Jul 18, 2016 at 16:55

1 Answer 1

15

You can use the -c option when launching vim to execute commands after the first file has been read.

For your situation, you can simply use the standard set filetype command -

vim -c 'set filetype=javascript'

You could also use --cmd to execute the command after the first file is loaded.

Lifted from the vim man pages:

   -c {command}
               {command} will be executed after the first file has been read.  {command} is interpreted as an Ex command.  If the {command} contains spaces it must be enclosed in double quotes (this depends on  the  shell  that  is  used).
               Example: Vim "+set si" main.c
               Note: You can use up to 10 "+" or "-c" commands.

   --cmd {command}
               Like using "-c", but the command is executed just before processing any vimrc file.  You can use up to 10 of these commands, independently from "-c" commands.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.