Wikia

Vim Tips Wiki

Changes: Enhanced command window

Edit

Back to page

(Comments: s/nmap/nnoremap/ not needed?)
(Remove html character entities)
Line 14: Line 14:
 
If you are comfortable
 
If you are comfortable
 
* with Vim's modes and
 
* with Vim's modes and
* with using the <ESC> key (meaning you rarely hit <ESC> unnecessarily)
+
* with using the <Esc> key (meaning you rarely hit <Esc> unnecessarily)
 
and you
 
and you
 
* would prefer entering : commands in a modal command window rather than on the command line
 
* would prefer entering : commands in a modal command window rather than on the command line
Line 28: Line 28:
 
Begin with some mappings to easily enter and leave the command window with a single key-stroke:
 
Begin with some mappings to easily enter and leave the command window with a single key-stroke:
 
<pre>
 
<pre>
nmap <ESC> q:<C-W>_
+
nmap <Esc> q:<C-W>_
 
nmap q/ q/<C-W>_
 
nmap q/ q/<C-W>_
 
nmap q? q?<C-W>_
 
nmap q? q?<C-W>_
 
augroup ECW_au
 
augroup ECW_au
 
au!
 
au!
au CmdwinEnter * nmap <ESC> :q<CR>
+
au CmdwinEnter * nmap <Esc> :q<CR>
au CmdwinLeave * nmap <ESC> q:<C-W>_
+
au CmdwinLeave * nmap <Esc> q:<C-W>_
 
augroup END
 
augroup END
 
</pre>
 
</pre>
Line 46: Line 46:
 
augroup ECW_au
 
augroup ECW_au
 
" musn't do au! again
 
" musn't do au! again
au CmdwinEnter : imap <UP> <C-O>y0<C-O>:let@/='^'.@0<CR><C-O>?<ESC><ESC>
+
au CmdwinEnter : imap <UP> <C-O>y0<C-O>:let@/='^'.@0<CR><C-O>?<Esc><Esc>
 
au CmdwinLeave : iunmap <UP>
 
au CmdwinLeave : iunmap <UP>
au CmdwinEnter : imap <DOWN> <C-O>y0<C-O>:let@/='^'.@0<CR><C-O>/<ESC><ESC>
+
au CmdwinEnter : imap <DOWN> <C-O>y0<C-O>:let@/='^'.@0<CR><C-O>/<Esc><Esc>
 
au CmdwinLeave : iunmap <DOWN>
 
au CmdwinLeave : iunmap <DOWN>
 
au CmdwinLeave : :let @/=""
 
au CmdwinLeave : :let @/=""
Line 60: Line 60:
 
augroup ECW_au
 
augroup ECW_au
 
" musn't do au! again
 
" musn't do au! again
au CmdwinEnter : imap <C-D> <C-O>y0<C-O>:ECWCtrlD<CR><ESC>
+
au CmdwinEnter : imap <C-D> <C-O>y0<C-O>:ECWCtrlD<CR><Esc>
 
au CmdwinLeave : iunmap <C-D>
 
au CmdwinLeave : iunmap <C-D>
 
augroup END
 
augroup END
Line 82: Line 82:
 
if (match(@", '^ *[a-z]\?map\s\s*\(\S\S*\)\?\s*$') >=0 )
 
if (match(@", '^ *[a-z]\?map\s\s*\(\S\S*\)\?\s*$') >=0 )
 
let s:foo = @"
 
let s:foo = @"
let save_more=&amp;more
+
let save_more=&more
 
set nomore
 
set nomore
 
execute ':redir @" |'.s:foo.'|redir END'
 
execute ':redir @" |'.s:foo.'|redir END'
let &amp;more = save_more
+
let &more = save_more
 
put=@"
 
put=@"
 
"Keep this next command even though Vim complains -- it is
 
"Keep this next command even though Vim complains -- it is

Revision as of 09:11, September 29, 2008

Tip 681 Printable Monobook Previous Next

created March 18, 2004 · complexity intermediate · author Suresh Govindachar · version 6.0


If you are comfortable

  • with Vim's modes and
  • with using the <Esc> key (meaning you rarely hit <Esc> unnecessarily)

and you

  • 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

Basic use of the command window is easy, but this tip allows you to improve the command window with simple mappings to enter and leave it, as well as providing missing functionality from the command line such as using <C-D> to view matches for what you have typed so far.

Begin with some mappings to easily enter and leave the command window with a single key-stroke:

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

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 @" |'.s:foo.'|redir END'
    let &more = save_more
    put=@"
    "Keep this next command even though Vim complains -- it is
    "a work-around for some "unknown bad thing"
    silent normal
    return
  endif
  "sf and find can have space separated arguments
  if (match(@", '^ *\(\(sf\)\|\(find\)\)\ *') >=0 )
    let s:foo = substitute(@", '^ *\(\(sf\)\|\(find\)\)\ *', '', '')
  else "pick the trailing non-space separated stuff
    let s:foo = substitute(@", '\(.\{-}\)\(\S\S*\s*\)$', '\2', '')
  endif
  let s:foo = substitute(s:foo, '\s*$', '*', '') "OK if ending has two wild-cards
  let @"=glob(s:foo)
  if(@" == "") | let @"='no match' | endif
  put=@"
endfunction

if !exists(":ECWCtrlD")
  command -nargs=0 ECWCtrlD call s:ECWCtrlD()
endif

If one wants to get even more fancy, one can start with the map

nmap <S-ESC> :ECWtobedefined

wherein the function ECWtobedefined opens up a new buffer in which one can do anything one likes. Even if this new buffer merely mimics the command window, it will the feature of co-existing with other buffers -- which is a feature that the command window does not have!

Comments

 TO DO 

  • Define the undefined :ECWtobedefined, or remove it
  • Test whether this tip even works
    How does it work? I thought the first example needs s/nmap/nnoremap/, but it works.
  • Reword as desired for better "flow" and layout

Still needs review. Only did minor rewordings. --Fritzophrenic 16:51, 28 July 2008 (UTC)


Around Wikia's network

Random Wiki