📜 ⬆️ ⬇️

vim + psql

Already half a year as they switched to vim. We work with RoR (ruby on rails) and javascript (Dojo).
Vim squeaked a week and began to serve faithfully.
Bribes simplicity and at the same time the power of this editor, especially in * nix environment.

In this post I will tell you how easy it is to make friends vim and psql (postgresql console client).

Much lyrics:

* nix is a constructor consisting of many cubes.
of these cubes you can create :) - the highest pleasure available to mortals (loot = evil)
')
cubes are libraries and, above all, utilities, highly specialized programs with licked code that perform their functions well.

cube 1 -psql, a console client for postgresql, works something like this:

user@host:/~$ psql
Welcome to psql 8.3.7, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

postgres=# \l --
postgres=# \c db_name --
postgres=# \d table_name --
postgres=# select * from big_table; -- just sql

cube 2 - vim, a unique text editor that perfectly edits the code.
However, by customizing interaction with other utilities, you can turn it into a powerful tool.

So the task is to execute sql queries to postgresql from vim.

We can send psql requests not only interactively, but also through a pipe. And the result is redirected to the file.

user@host:/~$ cat my.sql | psql > result.txt

In vim we can send the contents of the buffer to the console utility.

:w!psql db_name > /var/temp/result.txt

Next we just need to open this file in vim and we get approximately what we wanted.

Let's design it in the form of a plugin - we put the psql.vim file with the contents in the daddy .vim / plugin /:

"simple vim integration with psql

" ,
" psql
"
" -- db:db_name or more arguments for psql
fun! s:get_db()
let s:db=(split(getline(1),":"))[1]
return s:db
endfun

"
" psql
fun! PsqlPW()
let s:db=s:get_db()
exec 'w !psql '.s:db.' > ~/temp/sqlres'
"
" preview
ped ~/temp/sqlres
endfun

"
" {{{ }}}
"
" psql
"
" foldmethod=marker
" (zc za)
" {{{ }}} (see :h folding)
fun! PsqlLine()
let s:db=s:get_db()
normal $
let s:start_pos = search('{{{','bcnW')
let s:end_pos = search('}}}','cnW')
if s:start_pos!=0 && s:end_pos!=0
let s:cmd= ':'.s:start_pos.','.s:end_pos.'w !psql '.s:db.' > ~/temp/sqlres'
"echo cmd
exec s:cmd
ped ~/temp/sqlres
endif
endfun


Now, for ease of use of these functions, add another file to the configuration daddy.
.vim / ftplugin / sql.vim:

map <buffer> <F5> :call PsqlPW()<cr>
map <buffer> <cr> :call PsqlLine()<cr>
setlocal foldmethod=marker


This script will be called every time you open a file with type sql and as a result -
by pressing F5 you execute the entire file, and by Enter the block between {{{}}}
You can also fold these blocks.

image

Tip: Want to autocomplete - the simplest option is to ask for the description of the tables
\ d table_name
and use the standard vim <cx> <cp> :)
yo

Source: https://habr.com/ru/post/64198/


All Articles