In vim, is there a way to set "very magic" permanently and globally?

Viewed 11333

I use "very magic" for regexp searches (i.e. /\v or %s/\v) but I wish I could set some option so I don't have to include \v anymore, anywhere. Is there a way to do this?

4 Answers

To reply to the answer above as I can't comment yet, from How to make substitute() use another magic mode?, the vim docs and my own testing, smagic (and sm) only enters magic mode and not very magic mode.

                        *:snomagic* *:sno*
:[range]sno[magic] ...  Same as `:substitute`, but always use 'nomagic'.
            {not in Vi} 

                        *:smagic* *:sm*
:[range]sm[agic] ...    Same as `:substitute`, but always use 'magic'.
            {not in Vi}

For example, one should ('s turn into )'s in a file with :%sm/(/)/g and not :%sm/\(/\)/g, which shows the following for me

E54: Unmatched \(
E54: Unmatched \(
E476: Invalid command

Instead, to enter very magic mode, one should use \v in the search expression of substitute (i.e. :%s/\v\(/\)/g) (Please correct me if I've messed up, I am quite new to Vim)

https://learnvimscriptthehardway.stevelosh.com/chapters/31.html#magic

Add a normal mode mapping that will automatically insert the \v for you whenever you begin a search

my current config in init.vim:
(I want to go to the fist line and then search, and return back using 's, so msgg)

" vim has set:  au BufNewFile,BufRead *.ahk  setf autohotkey
if &filetype == 'vim'
    nnoremap / msgg/\v^[^"]*
elseif &filetype == 'autohotkey'
    echo 'ahk'
    nnoremap / msgg/\v^[^;]*
    " todo 
    " https://github.com/hnamikaw/vim-autohotkey
elseif expand('%:t') == 'wf_key.ahk'
    nnoremap / msgg/\v^[^;]*

elseif &filetype  == 'zsh'
    nnoremap / msgg/\v^[^#]*
else
    " vscode neovim can not detect filetype?
    nnoremap / msgg/\v^[^#";(//)(/*)]*
endif

nnoremap ? msgg/\v
" nnoremap / /\v
cnoremap s/ s/\v

todo: plugin for very magic https://github.com/coot/EnchantedVim

Related