Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there a simple way to switch from one command type mode to another without loosing the current command line?

In my ~/.vimrc file I remap the <space> and <c-space> character, coming from, to:

  1. N,V,O => :
  2. I => :
  3. : => / => ?

The code that accomplishes the above behavior is:

function! s:ModMapSpaceChar()
  noremap <unique> <space> :
  map <unique> <nul> <c-space>
  map! <unique> <nul> <c-space>
  noremap <unique> <c-space> /
  inoremap <unique> <c-space> <c-o>:
  cnoremap <unique> <c-space> <c-\>eg:ToggleCmdTypes()<cr><c-c>@"
  let g:CmdTypes={':' : '/', '/' : '?', '?': ':'}
  function! g:ToggleCmdTypes()
    let a=getcmdtype()
    let b=get(g:CmdTypes, a, a)
    let c=getcmdline()
    let @"=b . c
    return ''
  endfunction
endfunction

call s:ModMapSpaceChar()

What bothers me in my approach is the impure function g:ToggleCmdTypes. It should ideally return the command that it currently assigns to the " register. The resulting command shall then be executed.

share|improve this question
add comment

1 Answer

Instead of the detour through the register, you could probably also use feedkeys() for this, to avoid clobbering the register. This has only the disadvantage that it won't work when recorded and replayed as a macro.

In general, I would recommend to re-think your general approach; I don't see how it's useful to switch command-line modes like that. Who starts a search only to realize half-way through that he wants that executed as a command instead?!

share|improve this answer
    
I very often want to use the incremental hilighting to get feedback while building a regular expression for a :s command (or, conversely, go back to / to get the benefit of incremental hilighting to figure out where I screwed up my regex) –  Random832 Mar 22 '13 at 17:54
1  
@Random832: :s re-uses the last search, anyway, if you omit the pattern. For the other direction, you can insert the last command line via <C-r>:. –  Ingo Karkat Mar 22 '13 at 19:09
    
Right but then I don't have it in the command line if I want to modify it for subsequent :s's - I'd be locked into doing / again then :s//... again, and can't see my capture groups in front of me. –  Random832 Mar 22 '13 at 20:50
    
@Random832: You can use <C-r>/ to insert the pattern, too. But if you like the switch function better, go ahead and use it! –  Ingo Karkat Mar 22 '13 at 21:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.