20

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?

23

You can use:

vim -c 'set syntax=sql' -
| improve this answer | |
  • 2
    Note: Works for me even without the colon in the command. – Murphy Mar 10 '16 at 12:27
  • 1
    Shorter variant: vim '+set syn=sql' - – Stéphane Chazelas Mar 10 '16 at 13:01
  • 6
    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... – Martin Tournoij Mar 16 '16 at 7:11
3
vim -R -c 'set ft=sql' -
| improve this answer | |
1

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

augroup filetype
  au! StdinReadPre * set filetype=sql
augroup END
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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