Vim Tips Wiki
m (Added to LanguageSpecific Category)
(clean up)
Line 1: Line 1:
[[Category:LanguageSpecific]]
+
[[Category:SQL]]
{{review}}
 
 
{{Tip
 
{{Tip
 
|id=41
 
|id=41
Line 22: Line 21:
   
 
fu! VimSQL()
 
fu! VimSQL()
  +
setf sql
 
nnoremap <C-K> :<C-U>
 
nnoremap <C-K> :<C-U>
 
exe "let linenum=".v:count<CR>:1,$-1d<CR><C-W>j:exe lin
 
exe "let linenum=".v:count<CR>:1,$-1d<CR><C-W>j:exe lin
Line 45: Line 45:
 
'''TODO''': Need more time to decide what author means by pressing '5K'.
 
'''TODO''': Need more time to decide what author means by pressing '5K'.
   
 
The following map
----
 
I added the following:
 
set syntax=sql
 
at the start of the function and after loading the history file, this way the files will be shown with syntax highlighting.
 
 
Also the following map
 
 
map ZZ :close<cr>:b afiedt.buf<cr>:xit<cr>
 
map ZZ :close<cr>:b afiedt.buf<cr>:xit<cr>
 
will let the ZZ function close the second window and save the afiedt.buf file and then exit (if the user has no other dirty buffers).
 
will let the ZZ function close the second window and save the afiedt.buf file and then exit (if the user has no other dirty buffers).
 
----
 
Another solution (but not only for Vim):
 
 
If your operating system is HP-UX, try below.
 
$ ied sqlplus <user>/<pass>--AT--<db>
 
SQL> [Escape-Key]
 
SQL> [K-Key] or [J-Key]
 
 
ied is a utility command that is intended to act as an interface between the user and an interactive program such as bc, sqlplus, providing most of the line editing and history functionality found in the Korn shell.
 
   
 
----
 
----

Revision as of 02:12, 23 October 2007

Previous TipNext Tip

Tip: #41 - Command-history facilities for Oracle/sqlplus user

Created: March 8, 2001 21:26 Complexity: advanced Author: vimer--AT--21cn.com Version: 5.7 Karma: 13/6 Imported from: Tip#41

As Oracle users know, sqlplus has a very bad command-line editing environment with no command history.

Below is my Vim solution for sqlplus, to record the command history when you use edit (sqlplus builtin command) to open the editor specified by the EDITOR environment variable. It saves the SQL statements into a standalone file such as .sqlplus.history.

Every time you open the file afiedt.buf (sqlplus's default command-buffer file), you get two split windows: the buffer above is afiedt.buf, the buffer below is .sqlplus.history, and you can see every SQL statement in the windows.

If you want to use SQL statement in line 5 to replace the current command-buffer, just press 5K, then :xa to go back to sqlplus. and use / to repeat the command saved in command-buffer file called afiedt.buf by default.

It can't process multi-line SQL statements conveniently. To do this, just use your favorite vim trick for that:


fu! VimSQL()
  setf sql
  nnoremap <C-K> :<C-U>
  exe "let linenum=".v:count<CR>:1,$-1d<CR><C-W>j:exe lin
  enum."y"<CR><C-W>kP
  let linenum=line("$")
  1,$-1w! >> ~/.sqlplus.history
  e ~/.sqlplus.history
  execute ":$-".(linenum-1).",$m0"
  %!uniq
  if line("$")>100
    101,$d
  endif
  b#
  set splitbelow
  sp ~/.sqlplus.history
  au! BufEnter afiedt.buf
endf

au BufEnter afiedt.buf call VimSQL()

Comments

TODO: Need more time to decide what author means by pressing '5K'.

The following map

map ZZ :close<cr>:b afiedt.buf<cr>:xit<cr>

will let the ZZ function close the second window and save the afiedt.buf file and then exit (if the user has no other dirty buffers).