Skip to main content

Run SQL query in vim

Posted in

Lets take a look at how commands can be executed in vim with passing some data from the file you are editing at this moment.

As an example, we are editing the SQL query and you want to run it from vim (e.g. we are dealing with psql).

1) Select the query using vim's visual mode
2) type

:! psql <your connection parameters>

Note: just right after you type ":" (colon) you will see

:'<,'>

Don't worry, keep typing... hit Enter... get the query result.

I should mention that there is one peculiarity that can be considered as a disadvantage of this approach. The query itself will be replaced by its result. Just undo this with vim's 'u' command.

More information please

Hi, could you please give some more information about this? For example, does the :! psql command work for all databases (i.e. sqlserver, mysql etc) or is it for a specific database?

Thanks

:!psql works for postgres.

:!psql works for postgres. But I bet you can use :!mysql for mysql and :!sqlplus for oracle databases...

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
 `o    O                 o     o.OOoOoo  oOoOOoOOo 
o O O O o
O O o o o
oOo O ooOO O
o o .oOo ooOO OoOo. O o
O O `Ooo. o o o o O
o o O O o O O O
O O `OoO' OooO O o ooOooOoO o'

Enter the code depicted in ASCII art style.