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?

Adam Matan
  • 2,543
  • 6
  • 29
  • 31

3 Answers3

31

You can use:

vim -c 'set syntax=sql' -
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
Lambert
  • 12,495
  • 2
  • 26
  • 35
6
vim -R -c 'set ft=sql' -
anishpatel
  • 163
  • 1
  • 5
2

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

augroup filetype
  au! StdinReadPre * set filetype=sql
augroup END
Murphy
  • 2,609
  • 1
  • 13
  • 21
  • 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? – Max Carroll Mar 23 '21 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 Mar 23 '21 at 14:36