2

Given that the pattern .\+ is frequently used, is there an abbreviated way of specifying it in Vim Regex?

  • 4
    If you preface your regex with \v, you don't have to escape that +. e.g., /\v.+. You can put this in your .vimrc file to always use it: nnoremap / /\v – Daniel Kaplan Nov 21 '17 at 18:29
1

When you type / you're in command mode. You can remap this by putting this in your .vimrc file:

cnoremap .+ .\+

This means, if you type .+ in command mode, replace it with .\+. You can replace the .+ on the left with anything you want.

  • Thank you. What is the significance of your first sentence? ("When you type / you're in command mode.") – Sabuncu Nov 21 '17 at 20:18
  • @Sabuncu Well there are lots of mappings. You can change what happens when you're in insert mode, normal mode, command mode, operator-pending mode, etc. I expected that when you typed / you're in normal mode because you type the / in normal mode. But I was surprised to learn that when you type / you're in command mode. The same mode that you're in when you type :. Therefore, you change this with a command mode mapping, not a normal mode mapping. Type :help map.txt to learn more. – Daniel Kaplan Nov 21 '17 at 20:23
  • Could not get it to work. I am using Vim 7.4 gvim on Windows 10. I ran your cnoremap command in command mode (i.e. I did :cnoremap...). Then tried using the mapping, and it did not work. – Sabuncu Nov 21 '17 at 20:29
  • @Sabuncu odd. I'm using vim on mac and it works for me. Sorry, not sure why it wouldn't work for you. – Daniel Kaplan Nov 21 '17 at 20:31
  • 3
    Be warned: 1) this may add a delay when typing . when in command line mode 2) This will expand in other places e.g. :grep '.+'. This may not be as you want. Might be able to limit this via cnoremap <expr> .+ getcmdtype() =~ '[/?]' ? '.\+' : '.+'. 3) This could get award if you want to use very magic mode, e.g. \v – Peter Rincker Nov 21 '17 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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