Sunday, February 23, 2014

Re: Negated autocmd filetype match (P.S.)

On 23/02/14 13:47, Tony Mechelynck wrote:
> On 23/02/14 09:15, Nick Dev wrote:
>> I have a function that strips trailing whitespace on save. In my
>> .vimrc I have the line
>>
>> autocmd BufWritePre * if &ft =~ 'sh\|perl\|python\....' | :call
>> <SID>StripTrailingWhitespaces() | endif
>>
>> What I would like is to negate that into matching everything but
>> "condition".
>>
>> This works OK for known filetypes:
>>
>> autocmd BufWritePre * if &ft !=# 'text' | :call
>> <SID>StripTrailingWhitespaces() | endif
>>
>> but I also want to match when filetype is not set. I have tried
>> various with no luck.
>
> When 'filetype' is not set it is set to the empty string, so !=# 'text'
> should match "everything other than 'text', even the empty string" and also
>
> if &ft == "" || &ft =~# 'sh$\|^perl\|^python'
>
> where =~# means "match pattern and don't ignore case", remember, so this
> would match anything ending in "sh", anything beginning in "perl" or
> "python", or the empty string (which is special-cased before the || ).
> You could alternatively represent the empty string inside the pattern by
> ^$ (i.e. start immediately followed by end) as follows:
>
> if &ft =~# '^$\|sh$\|^perl\|^python'

P.S. See also
:help expr-=~
where you will find not only what =~ does but also various similar but
slightly different operators. Maybe !~# is actually the one you want.

>
> Of course, it is important to use single quotes here, not double quotes,
> to avoid making the backslashes "special" when evaluating the quoted
> string — they are part of the pattern on the RHS of the =~# operator.
>
>>
>> I was also advised to use:
>>
>> autocmd FileType * if <amatch> !=# 'text' | ...
>>
>>
>> but can't see how that is correct, nor can I get it to work.
>>
> see above
>
>
> Best regards,
> Tony.
--
Kiss me twice. I'm schizophrenic.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: