How to do search & replace with ack in vim?

蹲街弑〆低调 提交于 2019-11-28 02:53:55

I've decided to use ack and Perl to solve this problem outside of Vim so I could use the more powerful Perl regular expressions instead of the GNU subset. You could map this to a key stroke in your vimrc.

ack -l 'pattern' | xargs perl -pi -E 's/pattern/replacement/g'

Explanation

ack

ack is an awesome command line tool that is a mix of grep, find, and full Perl regular expressions (not just the GNU subset). Its written in pure Perl, it's fast, it has match highlighting, works on Windows and it's friendlier to programmers than the traditional command line tools. Install it on Ubuntu with sudo apt-get install ack-grep.

xargs

Xargs is an old unix command line tool. It reads items from standard input and executes the command specified followed by the items read for standard input. So basically the list of files generated by ack are being appended to the end of the perl -pi -E 's/pattern/replacemnt/g' command.

perl -pi

Perl is a programming language. The -p option causes Perl to create a loop around your program which iterates over filename arguments. The -i option causes Perl to edit the file in place. You can modify this to create backups. The -E option causes Perl to execute the one line of code specified as the program. In our case the program is just a Perl regex substitution. For more information on Perl command line options perldoc perlrun. For more information on Perl see http://www.perl.org/.

Now, Vim has this new command cdo that will run the given command to each line of the quickfix list.

So you can use

:Ack pattern
:cdo s/pattern/newpattern/g

I don't believe there's a built in way of doing this, but it should be easy to make one.

What you need to do is create a command that calls a custom function. The function should then use the getqflist() function to get all of the entries in the quickfix list and exe to do the dirty work. Be careful what you pass as an argument!

" Define a command to make it easier to use
command! -nargs=+ QFDo call QFDo(<q-args>)

" Function that does the work
function! QFDo(command)
    " Create a dictionary so that we can
    " get the list of buffers rather than the
    " list of lines in buffers (easy way
    " to get unique entries)
    let buffer_numbers = {}
    " For each entry, use the buffer number as 
    " a dictionary key (won't get repeats)
    for fixlist_entry in getqflist()
        let buffer_numbers[fixlist_entry['bufnr']] = 1
    endfor
    " Make it into a list as it seems cleaner
    let buffer_number_list = keys(buffer_numbers)

    " For each buffer
    for num in buffer_number_list
        " Select the buffer
        exe 'buffer' num
        " Run the command that's passed as an argument
        exe a:command
        " Save if necessary
        update
    endfor
endfunction

You could using ack by this way

:args `ack -l User app/`
:argdo %s/, :expire.*)/)/ge | update

Or use ag

:args `ag -l User app/`
:argdo %s/, :expire.*)/)/gec | w

I use MacVim (activated with mvim in a shell). I pipe the results of ack to mvim:

mvim -f $(ack -l $@)

Then in MacVim, I search/replace using bufdo:

:bufdo %s/SEARCH/REPLACE/gce | update

Omit the c option if confirmation is not needed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!