Can Vim Autoindent SQL

Can VIM autoindent SQL?

By installing the python module sqlparse, which makes the sqlformat command available in your terminal.

pip install sqlparse

from vim you can use

:%!sqlformat --reindent --keywords upper --identifiers lower -

in order to attach a shortcut ,pt I added following configuration to my .vimrc config file:

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

You can customize sqlformat a bit. See

sqlformat --help

Vim can not recognize comment //end when autoindent full verilog code

This looks like a bug with the standard verilog indent function, part of the standard vim distribution at runtime/indent/verilog.vim.

A search for verilog indent files on www.vim.org shows four options, including one labeled "bugfixes to vim indent for verilog : bugfixes: previous version was evaluating expressions inside comments". You could try that one, but it is the lowest-rated of the options. You could also try GitHub.

VIM run command when opening .sql files

Let's break this down into components. First the mapping:

map ,pt :%!sqlformat --reindent --keywords upper --identifiers upper -<cr>

The general rules with mappings is to supply a mode and use noremap if you are able. So this becomes:

nnoremap ,pt :%!sqlformat --reindent --keywords upper --identifiers upper -<cr>

Next we need to understand buffer-local mappings. Your mapping is global which means once you open a buffer with a 'filetype' of sql then this mapping will work in any buffer. This is not likely not what you want. By using the <buffer> option we can set this mapping for just this buffer.

You are using an FileType autocmd event to trigger this mapping for sql filetypes. Here is that cleaned up:

augroup SqlStuff
autocmd!
autocmd FileType sql call SqlFormatter()
augroup end

function SqlFormatter()
set noautoindent
nnoremap <buffer> ,pt :%!sqlformat --reindent --keywords upper --identifiers upper -<cr>
endfunction

Additionally, you may want to avoid the autocmd & function all together and just add both the setting and the mapping into ~/.vim/after/ftplugin/sql.vim

set noautoindent
nnoremap <buffer> ,pt :%!sqlformat --reindent --keywords upper --identifiers upper -<cr>

Note: I have not tested this mapping so if there is an issue with sqlformat then that will also need to be fixed

For more help see:

:h :map-commands
:h :map-local
:h :autocmd
:h :augroup
:h FileType
:h after-directory

More help from Learning Vimscript the Hard Way:

  • Basic Mapping
  • Modal Mapping
  • Strict Mapping
  • Buffer-Local Options and Mappings
  • Autocommands
  • Autocommand Groups

Changing Vim indentation behavior by file type

You can add .vim files to be executed whenever vim switches to a particular filetype.

For example, I have a file ~/.vim/after/ftplugin/html.vim with this contents:

setlocal shiftwidth=2
setlocal tabstop=2

Which causes vim to use tabs with a width of 2 characters for indenting (the noexpandtab option is set globally elsewhere in my configuration).

This is described here: http://vimdoc.sourceforge.net/htmldoc/usr_05.html#05.4, scroll down to the section on filetype plugins.

Is it possible to auto-indent a query in mysql-worckbench

Refer this link you will have Beautify Query option

select the query you want to format in editor and then do

Edit -> format -> Beautify Query

Edit an extremely long sentence in Vim

These settings will help you immensely:

"Make long lines wrap
set wrap

"Make wraps *not* occur in the middle of a word
set linebreak

"Make the last line look OK
set display+=lastline

"Make 'j' and 'k' go down a visual line, not an entire line
nnoremap j gj
nnoremap k gk

"Make the arrow keys also go down a visual line
nnoremap <Up> gk
nnoremap <Down> gj

"Allow the option to move up entire lines
nnoremap gj j
nnoremap gk k

Since lines starting with " are comments, you can just directly copy and paste this into your .vimrc.

Another useful feature is the bar command |. If you type 200 |, this will jump to the 200th character on the current line.

If you want, you could also include these settings:

nnoremap $ g$
nnoremap 0 g0
nnoremap g$ $
nnoremap g0 0

With these settings, you can use g0 and g$ to jump to the first/last character on this visual line, rather than than the first/last character on the extremely long line. These settings don't hurt, but IMO the first group of settings I posted are more useful.

Indenting entire file in Vim without leaving current cursor location

See :h ''

This will get you back to the first char on the line you start on:

gg=G''

and this will get you back to the starting line and the starting column:

gg=G``

I assume the second version, with the backtick, is the one you want. In practice I usually just use the double apostrophe version, since the backtick is hard to access on my keyboard.

Difference in spacing/tab alignment between Sql-server and vim

When you want A and B to look the same you have to define what the desired appearance is. It can be that you want A to look like B or B to look like A or both A and B to look like C.

That desired appearance is what is lacking in your question.

  • Do you want sql-server to look like GVim?

    Look around sql-server's preferences and see if you can set it to use monospaced fonts. Seriously, who wants variable-width fonts for displaying code?

  • Do you want GVim to look like sql-server?

    You can't. Gvim only works with monospaced fonts which is a good thing because who wants variable-width fonts for displaying code?

  • Do you want both to look like another unspecified tool?

    As long as it's based on monospaced font… maybe. What would that tool be?

Whatever, you could start by using the same values for the same options in sql-server and GVim.

Like… 4 everywhere.

Or 2.

How can I custom indent a file with vim?

From the help:


={motion} Filter {motion} lines through the external program
given with the 'equalprg' option. When the 'equalprg'
option is empty (this is the default), use the
internal formatting function |C-indenting|. But when
'indentexpr' is not empty, it will be used instead
|indent-expression|.

It looks like setting the equalprg option is what you want.



Related Topics



Leave a reply



Submit