sponsor Vim development Vim logo Vim Book Ad

sql.vim : ftplugin for SQL (Common for Oracle, Microsoft SQL Server, Sybase, ...)

 script karma  Rating 27/11, Downloaded by 4428  Comments, bugs, improvements  Vim wiki

created by
David Fishburn
script type
This file should only contain values that are common to all SQL languages Oracle, Microsoft SQL Server, Sybase ASA/ASE, MySQL, and so on.

Attention Vim7 users
ftplugin/sql.vim version 10.0 is included in the Vim 7.3 patches.
ftplugin/sql.vim previous versions have been sent to Bram when updates are made and they are always part of the Vim release.  Runtime updates can be pulled from the Vim website or through Vim patches.

ftplugin/sql.vim version 1.0 is included in the Vim 7.0 release.
Version 0.04 is included with Vim 6.3.

This is the main file for supporting different SQL dialects (Oracle, DB2, Sybase, MySQL, Postgres, SQLite and so on) in Vim.  There are many features within which allow you to default your buffer to specific database dialects as well as change each buffer (at any time) to a different SQL dialect.

See :h sql.txt for more details.

Patterns have been defined for the matchit plugin, to use the % command to match statements.  Supported patterns are:
        elseif | elsif
        else [if]
        end if
        [while condition] loop
        end loop
        end loop
        end case
        when not matched
        when matched
        WHEN column_not_found THEN

Define how to find the macro definition of a variable using the various
[d, [D, [_CTRL_D and so on features ( see :h [d )

Match these values ignoring case:
i.e.  DECLARE varname INTEGER

Added additional buffer mappings which work in both normal and visual mode:
        ]] - next BEGIN
        [[ - previous BEGIN
        ][ - next END
        [] - previous END

The following mappings also work in normal and visual mode.
A global variable has been defined to control the behaviour of these maps.  You can override this variable in your vimrc file if it is not complete, or as new objects are added to other databases.
    let g:ftplugin_sql_objects = 'function,procedure,event,' .
                \ '\(existing\\|global\s\+temporary\s\+\)\?table,trigger' .
                \ ',schema,service,publication,database,datatype,domain' .
                \ ',index,subscription,synchronization,view,variable'

        ]} - Next CREATE statement (of the above types)
        [{ - Previous CREATE statement (of the above types)

These mappings also work in both normal and visual mode.
        ]" - Next comment
        [" - End of previous comment

It also sets up Vim's OMNI SQL completion which calls into autoload/SQLComplete.vim  (this file is also automatically included in the Vim distribution).  SQLComplete (http://www.vim.org/scripts/script.php?script_id=1572) is also updated on the website and can be downloaded separately.  A raft of insert mode mappings have been created to allow you to write your SQL more efficiently.

There is a user defined keyboard shortcut (i.e. <CTRL-C>) followed by a single letter to initiate the imaps.

If you are using a syntax enabled Vim:
    a - Complete based on current syntax
    k - Complete based on sqlKeywords
    f - Complete based on sqlFunctions
    o - Complete based on sqlOptions
    T - Complete based on sqlTypes
    s - Complete based on sqlStatements

If the dbext plugin (http://www.vim.org/scripts/script.php?script_id=356) is installed and can connect to your database:
    t - Complete based on tables
    p - Complete based on procedures
    v - Complete based on views
    c - Complete based on columns
    l - Return a comma separated list of columns for a table
    L - Return a comma separated list of columns for a table when a list of tables is shown
    R - Complete based on resetCaches
    Right Arrow - Drill into table to show columns
    Left Arrow  - Back to table list

install details
Copy into your .vim/ftplugin or vimfiles/ftplugin directory.

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
sql.vim 10.0 2012-12-07 7.0 David Fishburn Version 10.0 (Dec 2012)
NF: Changed all maps to use noremap instead of must map

NF: Changed all visual maps to use xnoremap instead of vnoremap as they should only be used in visual mode and not select mode.

BF: Most of the maps were using doubled up backslashes before they were changed to using the search() function, which meant they no longer worked.
Version 9.0
NF: Completes 'b:undo_ftplugin'
BF: Correctly set cpoptions when creating script
Version 8.0
NF: Improved the matchit plugin regex (Talek)
Version 7.0
NF: Calls the sqlcomplete#ResetCacheSyntax() function when calling SQLSetType.
Version 6.0
NF: Adds the command SQLGetType
Version 5.0
NF: Adds the ability to choose the keys to control SQL completion, just add the following to your .vimrc:
   let g:ftplugin_sql_omni_key       = '<C-C>'
   let g:ftplugin_sql_omni_key_right = '<Right>'
   let g:ftplugin_sql_omni_key_left  = '<Left>'
BF: format-options - Auto-wrap comments using textwidth was turned off by mistake.
sql.vim 0.08 2005-02-21 6.0 David Fishburn Missed an = sign, <sigh>
sql.vim 0.07 2005-02-20 6.0 David Fishburn Very minor update.

Changed this line:
setlocal cpo&vim

set cpo
sql.vim 0.06 2005-02-19 6.0 David Fishburn Added some additional visual maps, and now allow the user to customize part of the search strings with this global variable: g:ftplugin_sql_objects
sql.vim 0.05 2004-11-25 6.0 David Fishburn Added additional matchit.vim features.
Created additional buffer maps to assist in navigation:
]] - next BEGIN
[[ - previous BEGIN
][ - next END
[] - previous END

]} - Next CREATE statement
[{ - Previous CREATE statement

]" - Next comment
[" - End of previous comment
sql.vim 0.04 2004-03-03 6.0 David Fishburn Updated the IF...ELSE...END IF block to correct an error.
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.