https://github.com/muhmud/qsh Skip to content Sign up * Why GitHub? Features - + Mobile - + Actions - + Codespaces - + Packages - + Security - + Code review - + Issues - + Integrations - + GitHub Sponsors - + Customer stories- * Team * Enterprise * Explore + Explore GitHub - Learn and contribute + Topics - + Collections - + Trending - + Learning Lab - + Open source guides - Connect with others + The ReadME Project - + Events - + Community forum - + GitHub Education - + GitHub Stars program - * Marketplace * Pricing Plans - + Compare plans - + Contact Sales - + Education - [ ] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this user All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up {{ message }} muhmud / qsh Public * Notifications * Star 88 * Fork 1 shell-based database query tool 88 stars 1 fork Star Notifications * Code * Issues 0 * Pull requests 0 * Actions * Projects 0 * Wiki * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Wiki * Security * Insights main Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags 2 branches 3 tags Code Latest commit @muhmud muhmud Update README.md ... 475889e Sep 16, 2021 Update README.md 475889e Git stats * 111 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time bin README updated Sep 16, 2021 clients update to psql tables script May 18, 2021 editors one more fix for micro & vim Sep 15, 2021 images slightly better image for docs Sep 16, 2021 scripts more qsh-reg stuff; works with nvim Sep 1, 2021 .gitignore updated gitignore for connections Apr 26, 2021 README.md Update README.md Sep 16, 2021 View code [ ] qsh Pre-requisites Setup vim/nvim vim-plug packer Micro Usage Executing Queries Scripts Named Scripts Snippets Named Snippets Registering Connections Options Using SSH Exit README.md qsh Query SHell - improved database querying from your terminal QSH Pre-requisites You'll need to install & use tmux, which is needed to manage the split panes. It should be available from your package manager. For better viewing of SQL results, the pspg pager is recommended, however, you could also use less -SinFX. When displaying results, qsh will try to make a sensible choice, however, you can instead explicitly choose a pager. To format SQL statements, you will need python 3 and sqlparse. Setup Clone this repository to your home: $ git clone git@github.com:muhmud/qsh.git ~/.qsh And then add the ~/.qsh/bin directory to your PATH. You now just need to setup the editor you want to use for writing SQL statements, which will be triggered from your SQL client tool. If you want to, you can setup keyboard shortcuts for this in ~/.inputrc. The following example does this for Alt-q: $if mysql "\eq": '\\e;\C-m' $endif $if psql "\eq": '\\e\C-m' $endif You can currently use either vim/nvim or micro. vim/nvim vim-plug Plug 'muhmud/qsh', { 'dir': '~/.qsh', 'branch': 'main', 'rtp': 'editors/vim' } packer { "~/.qsh", rtp = "editors/vim" } These are the default key mappings, which can be disabled by setting g:qsh_enable_key_mappings to 0: " Alt+e (for execute) vnoremap e :call QshExecuteSelection() vnoremap :call QshExecuteSelection() vnoremap :call QshExecuteSelection() " Alt+y inoremap y :call QshExecuteAll() inoremap :call QshExecuteAll() nnoremap y :call QshExecuteAll() nnoremap :call QshExecuteAll() " Alt+g (for go) inoremap g :call QshExecute() inoremap :call QshExecute() nnoremap g :call QshExecute() nnoremap :call QshExecute() " Alt+G inoremap G :call QshExecute("^---$" 0) inoremap :call QshExecute("^---$", 0) nnoremap G :call QshExecute("^---$", 0) nnoremap :call QshExecute("^---$", 0) " Alt+d (for describe) vnoremap d :call QshExecuteNamedScriptVisually("describe") vnoremap :call QshExecuteNamedScriptVisually("describe") nnoremap d :call QshExecuteNamedScript("describe") nnoremap :call QshExecuteNamedScript("describe") inoremap d :call QshExecuteNamedScript("describe") inoremap :call QshExecuteNamedScript("describe") " Alt+r (for rows) vnoremap r :call QshExecuteNamedScriptVisually("select-some") vnoremap :call QshExecuteNamedScriptVisually("select-some") nnoremap r :call QshExecuteNamedScript("select-some") nnoremap :call QshExecuteNamedScript("select-some") inoremap r :call QshExecuteNamedScript("select-some") inoremap :call QshExecuteNamedScript("select-some") " Alt+t (for tidy) vnoremap t :call QshExecuteNamedSnippetVisually("format") vnoremap :call QshExecuteNamedSnippetVisually("format") " Alt+v vnoremap v :call QshExecuteScriptVisually() vnoremap :call QshExecuteScriptVisually() nnoremap v :call QshExecuteScript() nnoremap :call QshExecuteScript() inoremap v :call QshExecuteScript() inoremap :call QshExecuteScript() " Alt+Space vnoremap :call QshExecuteSnippetVisually() vnoremap :call QshExecuteSnippetVisually() nnoremap :call QshExecuteSnippet() nnoremap :call QshExecuteSnippet() inoremap :call QshExecuteSnippet() inoremap :call QshExecuteSnippet() You can add custom key mappings like this: autocmd Filetype sql call QshCustomSqlKeyMappings() function QshCustomSqlKeyMappings() ... endfunction Micro The micro plugin can be installed by executing the following: $ mkdir -p ~/.config/micro/plug && cp -r ~/.qsh/editors/micro ~/.config/micro/plug/qsh The following key mappings, or similar, can be added to ~/.config/ micro/bindings.json: "Alt-g": "command:QshExecute", "Alt-G": "command:QshExecute '^---$' 0", "Alt-e": "command:QshExecuteSelection", "Alt-y": "command:QshExecuteAll", "Alt-d": "command:QshExecuteNamedScript 'describe'", "Alt-r": "command:QshExecuteNamedScript 'select-some'", "Alt-v": "command:QshExecuteScript", "Alt-Space": "command:QshExecuteSnippet", "Alt-t": "command:QshExecuteNamedSnippet 'format'" Usage From within a tmux session, prefix the invocation of your SQL client with qsh: $ qsh psql This will setup your SQL client environment appropriately for qsh. Now, trigger the editor using the command for your environment. For mysql, this would be \e;, and for psql, \e, or if you setup a keyboard shortcut, as described above, you could use that also. You should see the editor pane created, where you can now type in queries. A default SQL file is created for you, however, you could open up any other file you need to. Executing Queries * Alt-e - Highlight a query to run and execute it * Alt-g - Execute a query without needing to highlight it * Alt-G - Execute multiple statements or function/procedure definitions without needing to highlight * Alt-y - Execute everything in the editor buffer For Alt-g, qsh will look for a statement delimited on either side by a semi-colon. This makes it easier to execute a large SQL statement without needing to highlight it every time. Alternatively, using Alt-G does the same thing but changes the delimiter to be the string ---, which must be the only thing on a line. You can change this to what you like, this is simply the default as defined in the key mapping. The following provides an example: create procedure test(a int) begin update test set a = 1; /* <- If the cursor is here, Alt-G will create procedure test only */ end; --- /* <- This is the customizable delimiter defined in the key binding */ create procedure test2(a int) begin update test set a = 2; /* <- If the cursor is here, Alt-G will create procedure test2 only */ end; Scripts * Alt-v - Execute a script, which can be done with or without highlighting Scripts are shortcuts for SQL statements that return a consistent data set across different database servers. For example, to get a list of tables in the current database, whether mysql or postgresql, execute the following script: tables There are quite a few scripts available. You can see what they are by executing the scripts script. You can also add you own custom scripts to ~/.qsh/clients/psql/scripts or ~/.qsh/clients/mysql/scripts, depending on the database platform you are targeting. For reference, here is an example of the kinds of scripts available: $ ls ~/.qsh/clients/psql/scripts all-columns all-references all-sessions columns procedures scripts tables all-databases all-routines all-tables databases references select triggers all-functions all-schemas all-triggers describe routines select-some views all-procedures all-select all-views functions schemas sessions Named Scripts * Alt-d - Describe a particular table, which may or may not be highlighted * Alt-r - Select some of the data for a particular table Named scripts take information from the editor as a payload, which is used to provide context. The scripts mentioned above are created by default, however, you can also add your own. You can either highlight the name of the table to be used with these scripts, however, it's also OK for the cursor to simply be on the table name. Snippets * Alt-Space - Execute a snippet, which may or may not be highlighted Snippets are similar to scripts, however, the results are injected into the editor instead of being displayed as query results. You can also add your own custom snippets to ~/.qsh/clients/psql/snippets for postgresql, for example. The only snippet currently available is: * columns() - Get a comma-separated list of column names for a particular table Named Snippets * Alt-t - Format a SQL statement, which must be highlighted Similar to named scripts, these snippets take a payload from the editor. Currently, this feature can be used to format a SQL statement. Registering Connections You can register connections for database servers that you access frequently. This can also be used to store the password for the connection using the native mechanism of each SQL client. This is achieved using the qsh-reg tool. For example: $ qsh-reg -p dev-server psql -hmy-dev-server -Uroot -ddevdb This will register a connection called dev-server using the provided psql invocation. The -p option means that a password is to be stored, which the script will prompt you for. Once the connection is registered, you can connect like this: $ qsh dev-server You can also execute other tools using the connection info, such as pg_dump: $ qsh -c pg_dump dev-server --table=public.some_table -s Execute qsh-reg without any arguments to see all the available options. Options The following environment variables can be changed if required: * QSH_EDITOR - The editor you are going to be using, which defaults to $VISUAL * QSH_PAGER - The pager you will be using, which by default will try pspg, less, and cat in that order. Using SSH To work with database servers over SSH, you should install qsh on your remote host and start tmux in your SSH session. You won't, however, be able to get qsh to work when the editor is running locally and the SQL client is on a remote server, i.e. running within an SSH connection. If possible, it might be easier to simply provide host and port connection details to the SQL client on your workstation and run everything locally. Exit To clean up any temporary files & go back to normal, simply exit the editor. If you exit accidentally, just trigger the editor again, and it should go back to how it was. You can also explicitly cleanup temporary files created by qsh by executing: $ qsh-cleanup About shell-based database query tool Resources Readme Releases 3 v0.2 Latest Sep 16, 2021 + 2 releases Packages 0 No packages published Contributors 2 * @muhmud muhmud Muhmud Ahmad * @jindraj jindraj Jakub Jindra Languages * Shell 74.2% * Vim script 14.2% * Lua 11.0% * Python 0.6% * (c) 2021 GitHub, Inc. * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.