Skip to content

purcell/sqlformat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Melpa Status Melpa Stable Status Build Status Support me

sqlformat.el

This Emacs library provides commands and a minor mode for easily reformatting SQL using any one of several popular SQL formatters (see Usage below for a list).

Installation

If you choose not to use one of the convenient packages in MELPA, you'll need to add the directory containing sqlformat.el to your load-path, and then (require 'sqlformat).

Usage

To set your preferred formatter, customise the sqlformat-command variable or set it as a file/directory local variable. The variable should be assigned one of the following symbol values:

For example, to use pgformatter (i.e., the pg_format command) with two-character indent and no statement grouping,

(setq sqlformat-command 'pgformatter)
;; Optional additional args
(setq sqlformat-args '("-s2" "-g"))

Then call sqlformat, sqlformat-buffer or sqlformat-region as convenient.

Enable sqlformat-on-save-mode in SQL buffers like this:

(add-hook 'sql-mode-hook 'sqlformat-on-save-mode)

or locally to your project with a form in your .dir-locals.el like this:

((sql-mode
   (mode . sqlformat-on-save)))

You might like to bind sqlformat or sqlformat-buffer to a key, e.g. with:

(define-key sql-mode-map (kbd "C-c C-f") 'sqlformat)

Adding more formatters

In general, I'd like to avoid supporting an open-ended list of backend formatters: this was probably a poor design choice on my side. My reformatter package allows users or authors to very easily create format-on-save minor modes for arbitrary tools, so please consider using that to write (and perhaps publish) support for your favourite SQL formatter.


💝 Support this project and my other Open Source work

💼 LinkedIn profile

✍ sanityinc.com

About

Reformat SQL code inside Emacs using sqlformat or pgformatter

Resources

Stars

126 stars

Watchers

1 watching

Forks

Sponsor this project

Packages

 
 
 

Contributors