0

I added a shortcut in VIM so that I can press Esc and ,pt to get the automatic sql formatting from sqlformat command.

Taken from Can VIM autoindent SQL?.

"autocmd FileType sql call SqlFormatter()
nnoremap call SqlFormatter()
augroup end
function SqlFormatter()
    set noai
    " set mappings...
    map ,pt  :%!sqlformat --reindent --indent_width 4 --keywords upper --identifiers lower -<CR>
endfunction

I commented out autocmd FileType sql call SqlFormatter() and replaced it with nnoremap call SqlFormatter() to have it available in any file since often, I have SQL code that is embedded in another language and I thought I might perhaps use it only on a selection of the file.

What I need: In visual mode of vim (Esc+v), I want to mark something, and then press a shortcut of my choice to reformat only the marked code.

  1. Is there away to make vim treat the selection as its own file perhaps, like its own buffer, so that sqlformat?

  2. Another way would be to change sqlformat: add a parameter that allows applying it only to a selection. Such a parameter is not yet there:

usage: sqlformat  [OPTIONS] FILE, ...

Format FILE according to OPTIONS. Use "-" as FILE to read from stdin.

positional arguments:
  filename

optional arguments:
  -h, --help            show this help message and exit
  -o FILE, --outfile FILE
                        write output to FILE (defaults to stdout)
  --version             show program's version number and exit

Formatting Options:
  -k CHOICE, --keywords CHOICE
                        change case of keywords, CHOICE is one of "upper", "lower", "capitalize"
  -i CHOICE, --identifiers CHOICE
                        change case of identifiers, CHOICE is one of "upper", "lower", "capitalize"
  -l LANG, --language LANG
                        output a snippet in programming language LANG, choices are "python", "php"
  --strip-comments      remove comments
  -r, --reindent        reindent statements
  --indent_width INDENT_WIDTH
                        indentation width (defaults to 2 spaces)
  -a, --reindent_aligned
                        reindent statements to aligned format
  -s, --use_space_around_operators
                        place spaces around mathematical operators
  --wrap_after WRAP_AFTER
                        Column after which lists should be wrapped
  --comma_first COMMA_FIRST
                        Insert linebreak before comma (default False)
  --encoding ENCODING   Specify the input encoding (default utf-8)
  1. Would it perhaps work with plugins/wrappers like the vim plugin "vim-autoformat" and/or "sqlparse" within VIM? (see the link above)

1 Answer 1

1

There is :h 'formatprg' you can use to format a portion of the text with gq.

For your case this might work (added to .vim/after/ftplugin/sql.vim):

setlocal formatprg=sqlformat\ --reindent\ --indent_width\ 4\ --keywords\ upper\ --identifiers\ lower\ -

NOTE: I didn't test it as I use sql-formatter implemented in node:

" npm i -g sql-formatter
if executable('sql-formatter')
    setlocal formatprg=sql-formatter\ -i\ 4\ --lines-between-queries\ 3
    command -buffer Fmt :%!sql-formatter -i 4 --lines-between-queries 3
endif

Once you have it set up you can visually select a query and press gq. Or as usual, format all buffer with gggqG, or a paragraph with gqap etc.

PS, instead of .vim/after/ftplugin/sql.vim you can use autocommand with filetype event and set formatprg there.

enter image description here

6
  • I do not know whether I have tested it correctly. Using autocmd FileType sql call setlocal formatprg=sqlformat\ --reindent\ --indent_width\ 4\ --keywords\ upper\ --identifiers\ lower\ - throws Error detected while processing FileType Autocommands for "sql": E107: Missing parentheses: setlocal formatprg=sqlformat\ --reindent\ --indent_width\ 4\ --keywords\ upper\ --identifiers\ lower\ - Press ENTER or type command to continue. Jan 28, 2022 at 23:03
  • And pasting the same command setlocal ... in my ~/.vim/ftplugin/sql.vim does not throw an error, but I do not know how to start it. :h 'formatprg' opens an "options.txt" window at the bottom of the screen. gq: E492: Not an editor command: gq Jan 28, 2022 at 23:12
  • @questionto42 ~/.vim/ftplugin/sql.vim should be ~/.vim/after/ftplugin/sql.vim. As for :h 'formatprg' -- it is a helptopic describing what formatprg option is.
    – Maxim Kim
    Jan 29, 2022 at 6:28
  • @questionto42 gq is a normal mode command, thus, you should press gq in normal mode (or visual), NOT try to run it as :gq
    – Maxim Kim
    Jan 29, 2022 at 6:28
  • @questionto42 as for autocmd -- why did you put call there? It doesn't belong there.
    – Maxim Kim
    Jan 29, 2022 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.