Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #681 - Enhanced command window

Created: March 18, 2004 21:45 Complexity: intermediate Author: Suresh Govindachar Version: 6.0 Karma: 16/6 Imported from: Tip#681

"

"Enhanced Command Window

" Suresh Govindachar March 18, 2004

"

"If you are comfortable

"

" 1) with Vim's modes and

" 2) with using the <ESC> key (meaning

" you rarely hit <ESC> unnecessarily)

"

"and you

"

" 3) would prefer entering : commands in a modal

" command window rather than on the command line

"

"Then this tip is for you.


"_____________________________________________________

"Some key features of the command window (see ;help cmdwin)

"that would lead one to be interested in it are:

"

" - One can edit the buffer any way one wants

" - Hitting <CR> results in the line one was one being executed

" - (editing in the command window is much nicer than editing

" on the command line)


"_____________________________________________________

"Begin with some mappings:


nmap <ESC> q:<C-W>_ 
nmap q/ q/<C-W>_ 
nmap q? q?<C-W>_ 


augroup ECW_au 
au! 
au CmdwinEnter * nmap <ESC> :q<CR> 
au CmdwinLeave * nmap <ESC> q:<C-W>_ 


augroup END 


"Simple observation: with these mappings, one can go

"from normal-mode to cmdwin and back via escape (<ESC>)!


"_____________________________________________________

"Some nice things about the command line that are not present

"in the usual command window are:

"

" - The <UP> and <DOWN> arrow find all the commands that match

" the text entered to the left of the cursor

" - Hitting <C-D> shows the ways in which the typed text can be

" completed (see :help cmdline-completion)

"

"It is possible to have similar features in the command-window too.


"For the <UP> <DOWN> feature, add the following au-command event

"triggered maps:

augroup ECW_au 
" musn't do au! again 


au CmdwinEnter : imap <UP> <C-O>y0<C-O>:let@/='^'.@0<CR><C-O>?<ESC><ESC> 
au CmdwinLeave : iunmap <UP> 
au CmdwinEnter : imap <DOWN> <C-O>y0<C-O>:let@/='^'.@0<CR><C-O>/<ESC><ESC> 
au CmdwinLeave : iunmap <DOWN> 


au CmdwinLeave : :let @/="" 
augroup END 


"Now, while in the command window, going to insert mode and hitting

"the <UP> arrow followed by the n key results in one visiting all

"the commands that match the text to the left of the cursor when

"the <UP> key was hit -- then hitting <CR> while on any line causes

"it to be executed. Likewise, for the <DOWN> arrow.


"_____________________________________________________

"Next for the <C-D> feature. This is slightly more complex.

"Begin by adding the following autocommand event triggered maps:


augroup ECW_au 
" musn't do au! again 
au CmdwinEnter : imap <C-D> <C-O>y0<C-O>:ECWCtrlD<CR><ESC> 
au CmdwinLeave : iunmap <C-D> 
augroup END 


"Then provide this function: function! s:ECWCtrlD()

"With this function, hitting <C-D> while in insert mode in the

"command window results in more information about the text to

"the left of the cursor appearing on the lines below the cursor.

"This information can left on the command window or removed

"by typing u (undo).

"

"The nature of the information provided by <C-D> depends on what

"is to the left of the cursor:

"

" If the stuff to the left of the cursor looks, essentially

" like "map " or "map foo" then the information provided by

" <C-D> is the same as the information that appears when the

" the same stuff is typed on the command line and return is hit.

"

" If the stuff to the left of the cursor begins, essentially

" like "sf " or like "find " then what is displayed on <C-D> is

" the glob of the remaining stuff (after appending the remaining

" stuff with a *)

"

" If the stuff to the left of the cursor looks like neither of

" the above two cases then what is displayed is the glob of the

" very last non-space separated "word" (after appending that "word"

" with a *)

"

function! s:ECWCtrlD()


if (match(@", '^ *[a-z]\?map\s\s*\(\S\S*\)\?\s*$') >=0 ) 


let s:foo = @" 
let save_more=&more 
set nomore 
execute ':redir @"

Comments

Any line wraps you see above are pseudo-wraps based on your browser's width. Selecting the preceding text, copying it to a Vim buffer, and setting the filetype to vim (:set ft=vim) will make it easier to read.

Suresh Govindachar , March 18, 2004 21:49


Advertisement