Command-history facilities for Oracle/sqlplus user
From Vim Tips Wiki
created 2001 · complexity advanced · author vimer · version 5.7
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()
 Setting your editor in Oracle
If running Windows simply open up the SQLPlus command line and enter
This will then tell you your current editor in order to set your new editor to vim simply enter
define _editor = gvim
Now when you use the built-in edit command it will open gVim instead of Notepad.
- Decide what author means by pressing '5K' and explain in tip.
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).