30

When I want to easily read my PostgreSQL schema, I dump it to stderr and redirect it to vim:

pg_dump -h localhost -U postgres dog_food --schema-only | vim -

This gives:

enter image description here

vim does not have a syntax highlight schema, because it has no filename extension when reading from stdin, so I use the following:

:set syntax=sql

Which gives:

enter image description here

Being the lazy developer I am, I would like to force vim to use the SQL syntax by passing a command line argument, saving me the choir of re-typing set syntax=<whatever> every time I open it with stdin data..

Is there a way to set vim syntax by passing a command line argument?

3 Answers 3

31

You can use:

vim -c 'set syntax=sql' -
3
  • 2
    Note: Works for me even without the colon in the command.
    – Murphy
    Commented Mar 10, 2016 at 12:27
  • 1
    Shorter variant: vim '+set syn=sql' - Commented Mar 10, 2016 at 13:01
  • 8
    Typically you're better off using set filetype=sql (or ft=sql for short); that will also load the indentation files and such and not just the syntax highlighting... Commented Mar 16, 2016 at 7:11
6
vim -R -c 'set ft=sql' -
2

You can even automate that by putting the command into your ~/.vimrc:

augroup filetype
  au! StdinReadPre * set filetype=sql
augroup END
2
  • What might an example of filetype be, e.g. if I had an extension of .extension that I wanted to set as html would I use .extension or extension I have tried both but neither worked, do you have any suggestions? Commented Mar 23, 2021 at 13:16
  • @MaxCarroll Depends on what you want to achieve; this smells like a new question unrelated to the issue addressed here. But before you create one, try to search for it; chances are the answer is already somewhere in the web, and probably in the SE network, too.
    – Murphy
    Commented Mar 23, 2021 at 14:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .