Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 5 down vote accepted

You can use:

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

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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