
In the end, the function invokes the shell command pgfrm using the internal interactive function shell-command-on-region over the current buffer. If there is a region active (i.e., mark-active) the function operates over the region, otherwise, if no region is applied, it operates on the whole buffer. pgfrm is the path to the executable to invoke.b is the beginning of the region to format.The above piece of code defines an interactive function (i.e., a function that can be invoked with M-x) named pgformatter-on-region. ( defun pgformatter-on-region () "A function to invoke pgFormatter as an external program." ( interactive ) ( let (( b ( if mark-active ( min ( point ) ( mark )) ( point-min ))) ( e ( if mark-active ( max ( point ) ( mark )) ( point-max ))) ( pgfrm "/usr/bin/pg_format" ) ) ( shell-command-on-region b e pgfrm ( current-buffer ) 1 )) ) I developed a simple and ugly snippet of Lisp that can be loaded into Emacs to make the pgFormatter usage quicker. Where pg_format is the name of the executable of pgFormatter (e.g., it is called like that in Rocky Linux). This means that, given your own Emacs instance, you can format the code within the region by simply doing With the universal prefix ( C-u) it can also replace the region or buffer you are running the command against.



Use pgFormatter from Emacs, the portable wayĮmacs allows users to run a shell command over a region or a buffer content.
Sql code formatter how to#
Formatting SQL code with pgFormatter within Emacs pgFormatter is a great Perl 5 tool that parses SQL input and re-format it in a beautiful way.ĭespite the name, it works with any SQL piece of code, since it does support the standars from SQL-92 to SQL-2011, plus all little keywords and details that are specific to PostgreSQL.īeing myself an Emacs addicted, I reasoned about how to “pkug in” pgFormatter into Emacs, and I came up with a short and ugly snippet of code that does the trick.īut, being Emacs what it is, there is no particular need to plug in such code, as I will show you in a moment.
