Git Product home page Git Product logo

vipsql's Introduction

vipsql

A vim-plugin for interacting with psql

Demo

asciicast demo

Install

$ cd ~/.vim/bundle && git clone https://github.com/martingms/vipsql

As a terminal command

To use vipsql in a manner similar to psql, add something like this to your .bashrc or similar:

vipsql() {
    vim -c 'setlocal buftype=nofile | setlocal ft=sql | VipsqlOpenSession '"$*"
}

Or, if you'd prefer that every session gets its own tmp-file:

vipsql() {
    vim -c 'setlocal ft=sql | VipsqlOpenSession '"$*" $(mktemp -t vipsql.XXXXX)
}

Or perhaps always using the same file for the same provided args, so you can reuse queries from previous sessions:

vipsql() {
    local dir=${XDG_DATA_HOME:-"$HOME/.local/share"}"/vipsql/"
    local file=$(echo "$*" | tr -dc "[:alpha:]-=")".sql"
    mkdir -p "$dir"
    vim -c 'setlocal ft=sql | VipsqlOpenSession '"$*" "$dir$file"
}

For all of these, args are redirected to the underlying psql session, so e.g.

$ vipsql -d test

will start vim with vipsql already connected to the database test.

Notes

Note that vipsql uses the vim channels feature, so your vim must be at least version 8, and compiled with +channel. To test whether you're compatible, run:

$ vim --version | grep -o +channel

If the output is +channel you should be good to go.

Please also note that sending an interrupt (SIGINT) to psql (for example to cancel a long running query) results in killing the channel in versions of vim older than 8.0.0588 due to a bug.

Neovim should be supported, but I have not had the chance to test it, so please let me know if you are a neovim user and can confirm. One thing that definitely does not work for neovim is to send SIGINT, as there doesn't seem to be any support for sending arbitrary signals through neovim's job api yet.

Configure

Bindings

Put the following in your .vimrc (and customize bindings to your liking):

" Starts an async psql job, prompting for the psql arguments.
" Also opens a scratch buffer where output from psql is directed.
noremap <leader>po :VipsqlOpenSession<CR>

" Terminates psql (happens automatically if the output buffer is closed).
noremap <silent> <leader>pk :VipsqlCloseSession<CR>

" In normal-mode, prompts for input to psql directly.
nnoremap <leader>ps :VipsqlShell<CR>

" In visual-mode, sends the selected text to psql.
vnoremap <leader>ps :VipsqlSendSelection<CR>

" Sends the selected _range_ to psql.
noremap <leader>pr :VipsqlSendRange<CR>

" Sends the current line to psql.
noremap <leader>pl :VipsqlSendCurrentLine<CR>

" Sends the entire current buffer to psql.
noremap <leader>pb :VipsqlSendBuffer<CR>

" Sends `SIGINT` (C-c) to the psql process.
noremap <leader>pc :VipsqlSendInterrupt<CR>

Options

Configuration options (and their defaults) are:

" Which command to run to get psql. Should be simply `psql` for most.
let g:vipsql_psql_cmd = "psql"

" The prompt to show when running `:VipsqlShell`
let g:vipsql_shell_prompt = "> "

" What `vim` command to use when opening the output buffer
let g:vipsql_new_buffer_cmd = "rightbelow split"

" Commands executed after opening the output buffer
" Chain multiple commands together with `|` like so:
" "setlocal buftype=nofile | setlocal nowrap"
let g:vipsql_new_buffer_config = 'setlocal buftype=nofile'

" Whether or not to clear the output buffer on each send.
let g:vipsql_auto_clear_enabled = 0

" Whether or not to print a separator in the output buffer when sending a new
" command/query to psql. Has no effect if g:vipsql_auto_clear_enabled = 1.
let g:vipsql_separator_enabled = 0

" What that separator should look like.
let g:vipsql_separator = '────────────────────'

License

Copyright (c) Martin Gammelsæter. Distributed under the same terms as Vim itself. See :help license.

vipsql's People

Contributors

martingms avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

vipsql's Issues

vipsql db password

Hello! Is there a way to send the password though the buffer when prompted?

Using vipsql to connect to a foreign server.

I'm using Gentoo Linux. I have some bluehost webspace that has a postgresql database.

I know everything is configured properly on the bluehost database side because this command works and drops me into the postgresql terminal:

voyager:el@~ $ psql --host=10.20.30.40 -U my_username -d machines_testing -p 5432
psql (9.5.3, server 8.4.20)
Type "help" for help.
machines_testing=> select 'foo';
 ?column?
----------
 foo
(1 row)

However when I use the same command for vipsql, the plugin doesn't create a channel.

vipsql --host=10.20.30.40 -U my_username -d machines_testing -p 5432

I try to send a command and I get an error:

Error detected while processing function <SNR>3_Send:
line   11:
Attempted send on a closed channel

I know that things are okay on the vipsql side because it connects correctly to the local postgresql database with this command:

vipsql -Upostgres -d postgres

I can send sql commands and run them as expected.

It appears that vipsql is interpreting the bash parameters passed in and interpreting some of them as new files to open. I've tried putting things in quotes and using all the variations to communicate to vipsql the config options, and all are met with some kind of error.

Is there a streamlined way for me to specify host, port, username, database and password? passing it via bash params seems to be problematic. Maybe there's a silly workaround with escapes and quotes?

amazing

This is amazing! Thanks so much for writing this!

I was just in Olso recently for the NDC conference.
Too bad I didn't get a chance to meet you.
Keep up the great work!

Output is hidden: could vipsql scroll window?

Thanks for writing this excellent plugin.
The only issue I can see is that queries appear one after the other, but are not visible. Would it be possible to modify the code so that the window scrolls down so that the new query result is at the top?

When more than two buffers are opened, vipsql outputs to wrong buffer.

I don't know exactly what are the conditions for this to happen, but I was working with the two buffers, vertically separated, one containing my SQL statements and the other the default __vipsql__ output buffer. I then opened a new one with more SQL statements, again in a vertical split, and issued a vipsql command from it. The output of the command was appended to my previous buffer with my own SQL commands (which happened to be linked to a file on disk).

I don't understand the vim internals, so forgive me.

Anyway, just a minor bug. vipsql is an amazing plugin, it has changed my life.

Autosave, somehow?

I normally open vipsql over an .sql file I already have, then work on it, save, and so on.

In the past days I've lost a bunch of queries I was writing on the [Scratch] file because I was dumb and closed it without realizing it wasn't a file I had.

I don't know how to solve this, maybe save [Scratch] sessions on /tmp/ somehow?

Print some separator between the executions in the results buffer

Something like a ================================== (or perhaps something better).

DROP TABLE
CREATE TABLE
INSERT 0 1
 id |         created_at         |                                             data                                             
----+----------------------------+----------------------------------------------------------------------------------------------
  1 | 2017-09-28 21:36:29.828107 | {"ok": {"pli": true}, "amt": 12, "cred": "xyz", "debs": ["pli"], "txns": [], "asset": "BRL"}
(1 row)

==================================
ERROR:  relation "users" already exists
DROP TABLE
CREATE TABLE
INSERT 0 1
 id |         created_at         |                                             data                                             
----+----------------------------+----------------------------------------------------------------------------------------------
  1 | 2017-09-28 21:36:40.489839 | {"ok": {"pli": true}, "amt": 12, "cred": "xyz", "debs": ["pli"], "txns": [], "asset": "BRL"}
(1 row)

Sometimes is difficult to read the results buffer when a lot of errors and "CREATE STUFF" statements are being logged.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.