ack.vim icon indicating copy to clipboard operation
ack.vim copied to clipboard

Automatically escape special chars

Open duraki opened this issue 7 years ago • 5 comments

Not sure if this is a bug so please close if I'm wrong.

Are we supposed to escape all special characters? Example below.

Works

:Ack! "\\-\\>confirm" src/ => will search for "->confirm" which is ok.

Does not work

:Ack! "->confirm" src/ => drops error:

1 || Unknown option: >                                                                                                                                                                                                                                                                                                    
1 || Value "src/" invalid for option m (number expected)                                                                                                                                                                                                                                                                  
2 || ack: Invalid option on command line                                                                                                                                                                                                                                                                                  

Info

$ ack --version
ack --version
ack 2.12
Running under Perl 5.16.3 at /usr/bin/perl

$ nvim --version
NVIM v0.2.2
Build type: Debug

Thanks!

duraki avatar Feb 01 '18 11:02 duraki

Hi @duraki

:Ack! "\-\>confirm" src/ works as well. even -Q --literal Don't parse PATTERN as a regular expression doesn't help in this case. I think the escaping is required by ack, grep or ag. Ack.vim is just a wrapper here.

mboughaba avatar Feb 20 '18 21:02 mboughaba

I propose something like:

:Ack!! "->confirm" src/

# => !! equals literal

To be eligible for literal search and not regex matching one could use this flag. If user wants to combine "do not open" with this feature, he would add another exclamation like: Ack!!! (don't open + literal search).

Regarding the last point, as far as I know, you are supposed to escape only quotes (at least in grep):

$ grep "this have \" quote in the middle"
...

Cheers

duraki avatar Feb 20 '18 21:02 duraki

It looks like ack thinks of > as a switch because there's a dash before it. To solve the problem, you must add -- before the query pattern. To make Ack work straight from visual mode selection:

function! AckClipboard()
    execute printf('Ack! -Q -- "%s"', substitute(@", '\([%"\\]\)', '\\\1', 'g'))
endfunction
vnoremap <A-f> y:call AckClipboard()<CR>

siikamiika avatar Mar 03 '19 22:03 siikamiika

Yes, you're correct. -Q will turn off all metacharacters, and -- tells ack that it is at the end of the command line options.

petdance avatar Mar 03 '19 23:03 petdance

@siikamiika yup, that makes sense. Will close until further discussed. Thanks eryn. (btw would love above proposal to be impl in the future)

duraki avatar Mar 04 '19 00:03 duraki