Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Greplace plugin

  • Loading branch information...
commit 3230b91f421348cd2c882237e61433024877b6f1 1 parent 30a13c6
@jyurek authored
View
161 vim/bundle/vim-greplace/doc/greplace.txt
@@ -0,0 +1,161 @@
+*greplace.txt* Plugin for replacing pattern across multiple files
+
+Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com)
+For Vim version 7.0 and above
+Last change: 2007 March 2
+
+Overview~
+
+The Global Replace plugin allows you to search and replace a pattern across
+multiple files. The lines containing a specified pattern in multiple files are
+displayed in a buffer. You can edit the lines in this buffer and make the
+desired modifications to them. The plugin can then incorporate these changes
+back into the corresponding files interactively.
+
+==============================================================================
+
+Installation~
+
+1. Download the greplace.vim file and unzip the files into the $HOME/.vim
+ or the $HOME/vimfiles or the $VIM/vimfiles directory. After this step, you
+ should have the following two files (the directory structure should be
+ preserved):
+
+ plugin/greplace.vim - main global replace plugin file
+ doc/greplace.txt - documentation (help) file
+
+2. Change to the $HOME/.vim/doc or $HOME/vimfiles/doc or $VIM/vimfiles/doc
+ directory, start Vim and run the ":helptags ." command to process the
+ help file. Without this step, you cannot jump to the help topics.
+3. Restart Vim.
+
+To uninstall the global replace plugin, remove the plugin/greplace.vim and
+doc/greplace.txt files from the $HOME/.vim or $HOME/vimfiles directory.
+
+==============================================================================
+
+The following commands are provided by this plugin:
+
+:Gsearch Search for a given pattern in the specified group of
+ files and display the matches in the replace buffer.
+:Gbuffersearch Search for a given pattern in all the buffers
+ in the Vim buffer list.
+:Gargsearch Search for a given pattern in all the files in the
+ Vim argument list.
+:Gqfopen Use the results from the quickfix list.
+:Greplace Incorporate the modifications from the replace buffer
+ into the corresponding files.
+
+One example sequence of commands for using this plugin is:
+
+ :Gsearch mypattern *.java *.c
+ <The above command will search for mypattern in *.java and *.c
+ files in the current directory and display the matching lines
+ in a buffer.>
+ <You can now use the Vim editing commands to modify the buffer>
+
+ :Greplace
+ <The above command will load each of the buffer which needs to be changed
+ and ask you to confirm whether to make the change or not>
+
+ :wall
+ <To save all the modified buffers>
+
+In the above sequence, instead of ":Gsearch", you can use ":Gbuffersearch" or
+":Gargsearch" commands to search for a pattern in the files in the Vim buffer
+list or the argument list.
+
+The ":Gsearch" command uses the Vim ":grep" command to search for the pattern
+in the specified files. The ":grep" command uses the program specified by the
+"grepprg" option to search for the pattern. By default, the "grepprg" option
+is set to either grep (on Unix) or findstr (on MS-Windows). By modifying the
+"grepprg" option, you can also use other programs for searching. To use the
+Vim internal grep, set the "grepprg" option to "internal".
+
+The syntax of the ":Gsearch" command is:
+>
+ :Gsearch [<grep-option(s)>] [[<pattern>] [<filename(s)>]]
+<
+The arguments to the ":Gsearch" command are optional.
+
+The first set of arguments, if present, specify the options to the grep
+program. These options must start with "-" (for Unix) and "/" (for
+MS-Windows). For example, to ignore case, you can use "-i" for the Unix
+grep program.
+
+The next argument specifies the pattern. You cannot use space characters in
+the pattern. To specify space characters in the pattern, don't specify the
+pattern in the command-line. See below for more information.
+
+The last set of arguments specify the filenames. You can use wildcards in the
+filenames. You can also complete directory and file names by pressing <Tab>.
+
+If the pattern or the filenames is not supplied as argument to the ":Gsearch"
+command, then you will be prompted to enter the pattern and the filenames. The
+default value for the search pattern is the keyword under the cursor. In the
+prompt for entering the pattern, you can enter a pattern with space
+characters. In the prompt for entering the filenames, you can press <Tab> to
+complete the directory and file names.
+
+To search for a pattern in the files in the Vim buffer list, use the
+":Gbuffersearch" command. The syntax of this command is:
+>
+ :Gbuffersearch [<grep-option(s)>]
+<
+This command is similar to the ":Gsearch" command. This command searches for
+the specified pattern in all the files in the buffer list. You cannot specify
+filenames to this command.
+
+To search for a pattern in the files in the Vim argument list, use the
+":Gargsearch" command. The syntax of this command is:
+>
+ :Gargsearch [<grep-option(s)>]
+<
+This command is similar to the ":Gsearch" command. This command will search
+for the specified pattern in all the files in the argument list. You cannot
+specify filenames to this command.
+
+The difference between the ":Gbuffersearch" command and the Vim
+":bufdo %s/pattern/replace/c" command is that the ":Gbuffersearch" command
+allows you to inspect and change the matching lines in a buffer and then
+incorporate the changes. You can also make different changes to different
+lines. With a single ":bufdo" command, you can make only the one type of
+change in all the buffers. The same difference applies for the ":Gargsearch"
+and the ":argdo" command.
+
+Sometimes, you may have the desired list of filenames and the matching lines
+in them already in the quickfix list. For example, you can run tools like
+cscope, GNU id-utils, GNU global, etc., and get the results into the quickfix
+list. To use this list of files for replacing text, you can use the ":Gqfopen"
+command. This command doesn't take the pattern or filenames arguments. It
+parses the file names and lines in them from the current quickfix list and
+displays it in the replace buffer.
+
+You can edit the contents of the replace buffer using the Vim editing
+commands. You cannot save the contents to a regular file. You should not
+change the filename or line numbers in the replace buffer. You should not add
+additional lines in this buffer. If you don't want to make any changes, you
+can close the replace buffer.
+
+You can use the ":Greplace" command to store the modified lines from the
+replace buffer back to the corresponding files. This command is available only
+in the replace buffer.
+
+The ":Greplace" command will prompt you to confirm each of the changes. At
+this prompt, you can press 'y' to accept the change, 'n' to reject the change,
+'a' to accept all the changes, 'b' to accept all the changes in the current
+buffer and 'q' or <Escape> to exit the replace prompt and stop making the
+changes.
+
+To incorporate the modifications without the prompt, add "!" to the
+":Greplace" command. This will force the ":Greplace" command to make the
+changes without any prompts for confirmation.
+
+The modified files will not be automatically saved. You can save all of them
+using the ":wall" command or you can individually inspect the buffers for the
+changes and then save the buffer using the ":w" command. You can undo the
+changes individually by using the Vim "u" command.
+
+==============================================================================
+
+vim:tw=78:ts=8:noet:ft=help:
View
310 vim/bundle/vim-greplace/plugin/greplace.vim
@@ -0,0 +1,310 @@
+" File: greplace.vim
+" Script to search and replace pattern across multiple files
+" Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com)
+" Version: 1.0 Beta1
+" Last Modified: March 3, 2007
+"
+if exists("loaded_greplace")
+ finish
+endif
+let loaded_greplace = 1
+
+" Requires Vim 7.0 and above
+if v:version < 700
+ finish
+endif
+
+" Line continuation used here
+let s:cpo_save = &cpo
+set cpo&vim
+
+if &isfname =~ '['
+ let s:gRepl_bufname = '[Global\ Replace]'
+else
+ let s:gRepl_bufname = '\[Global\ Replace\]'
+endif
+
+" Character to use to quote patterns
+if !exists("Greplace_Shell_Quote_Char")
+ if has("win32") || has("win16") || has("win95")
+ let Greplace_Shell_Quote_Char = ''
+ else
+ let Greplace_Shell_Quote_Char = "'"
+ endif
+endif
+
+let s:save_qf_list = {}
+
+function! s:warn_msg(msg)
+ echohl WarningMsg
+ echomsg a:msg
+ echohl None
+endfunction
+
+highlight GReplaceText term=reverse cterm=reverse gui=reverse
+
+function! s:gReplace()
+ if empty(s:save_qf_list)
+ return
+ endif
+
+ let change_all = v:cmdbang
+
+ let changeset = {}
+
+ " Parse the replace buffer contents and get a List of changed lines
+ let lines = getbufline('%', 1, '$')
+ for l in lines
+ if l !~ '[^:]\+:\d\+:.*'
+ continue
+ endif
+
+ let match_l = matchlist(l, '\([^:]\+\):\(\d\+\):\(.*\)')
+ let fname = match_l[1]
+ let lnum = match_l[2]
+ let text = match_l[3]
+
+ let key = fname . ':' . lnum
+ if s:save_qf_list[key].text == text
+ " This line is not changed
+ continue
+ endif
+
+ let fname = s:save_qf_list[key].fname
+ if !has_key(changeset, fname)
+ let changeset[fname] = {}
+ endif
+
+ let changeset[fname][lnum] = text
+ endfor
+
+ if empty(changeset)
+ " The replace buffer is not changed by the user
+ call s:warn_msg('Error: No changes in the replace buffer')
+ return
+ endif
+
+ " Make the changes made by the user to the buffers
+ for f in keys(changeset)
+ let f_l = changeset[f]
+ if !filereadable(f)
+ continue
+ endif
+ silent! exe 'hide edit ' . f
+
+ let change_buf_all = 0 " Accept all the changes in this buffer
+
+ for lnum in keys(f_l)
+ exe lnum
+
+ let cur_ltext = getline(lnum)
+ let new_ltext = f_l[lnum]
+
+ let s_idx =0
+ while cur_ltext[s_idx] == new_ltext[s_idx]
+ let s_idx += 1
+ endwhile
+
+ let e_idx1 = strlen(cur_ltext) - 1
+ let e_idx2 = strlen(new_ltext) - 1
+ while e_idx1 >= 0 && cur_ltext[e_idx1] == new_ltext[e_idx2]
+ let e_idx1 -= 1
+ let e_idx2 -= 1
+ endwhile
+
+ let e_idx1 += 2
+
+ if (s_idx + 1) == e_idx1
+ " If there is nothing to highlight, then highlight the
+ " last character
+ let e_idx1 += 1
+ endif
+
+ let hl_pat = '/\%'.lnum.'l\%>'.s_idx.'v.*\%<'.e_idx1.'v/'
+ exe '2match GReplaceText ' . hl_pat
+ redraw!
+
+ try
+ let change_line = 0
+
+ if !change_all && !change_buf_all
+ let new_text_frag = strpart(new_ltext, s_idx,
+ \ e_idx2 - s_idx + 1)
+
+ echo "Replace with '" . new_text_frag . "' (y/n/a/b/q)?"
+ let ans = 'x'
+ while ans !~? '[ynab]'
+ let ans = nr2char(getchar())
+ if ans == 'q' || ans == "\<Esc>" " Quit
+ return
+ endif
+ endwhile
+ if ans == 'a' " Accept all
+ let change_all = 1
+ endif
+ if ans == 'b' " Accept changes in the current buffer
+ let change_buf_all = 1
+ endif
+ if ans == 'y' " Yes
+ let change_line = 1
+ endif
+ endif
+
+ if change_all || change_buf_all || change_line
+ call setline(lnum, f_l[lnum])
+ endif
+ finally
+ 2match none
+ endtry
+ endfor
+ endfor
+endfunction
+
+function! s:gReplace_show_matches()
+ let qf = getqflist()
+ if empty(qf)
+ call s:warn_msg('Error: Quickfix list is empty')
+ return
+ endif
+
+ let new_qf = {}
+
+ " Populate the buffer with the current quickfix list
+ let lines = []
+ for l in qf
+ if l.valid && l.lnum > 0 && l.bufnr > 0
+ let fname = fnamemodify(bufname(l.bufnr), ':.')
+ let buf_text = fname . ':' . l.lnum . ':' . l.text
+ let k = fname . ':' . l.lnum
+ let new_qf[k] = {}
+ let new_qf[k].fname = fnamemodify(bufname(l.bufnr), ':p')
+ let new_qf[k].text = l.text
+ else
+ let buf_text = l.text
+ endif
+
+ call add(lines, buf_text)
+ endfor
+
+ if empty(lines)
+ " No valid matching lines
+ return
+ endif
+
+ let w = bufwinnr(s:gRepl_bufname)
+ if w == -1
+ " Create a new window
+ silent! exe 'new ' . s:gRepl_bufname
+ else
+ exe w . 'wincmd w'
+
+ " Discard the contents of the buffer
+ %d _
+ endif
+
+ call append(0, '#')
+ call append(1, '# Modify the contents of this buffer and then')
+ call append(2, '# use the ":Greplace" command to merge the changes.')
+ call append(3, '#')
+ call append(4, lines)
+
+ call cursor(5, 1)
+ setlocal buftype=nofile
+ setlocal bufhidden=wipe
+ setlocal nomodified
+
+ command! -buffer -nargs=0 -bang Greplace call s:gReplace()
+
+ let s:save_qf_list = new_qf
+endfunction
+
+" gSearch
+" Search for a pattern in a group of files using ':grep'
+function! s:gSearch(type, ...)
+ let grep_opt = ''
+ let pattern = ''
+ let filenames = ''
+
+ " Parse the arguments
+ " grep command-line flags are specified using the "-flag" format
+ " the next argument is assumed to be the pattern
+ " and the next arguments are assumed to be filenames or file patterns
+ let argcnt = 1
+ while argcnt <= a:0
+ if &grepprg =~ 'findstr' && a:{argcnt} =~ '^/'
+ let grep_opt = grep_opt . ' ' . a:{argcnt}
+ elseif a:{argcnt} =~ '^-'
+ let grep_opt = grep_opt . ' ' . a:{argcnt}
+ elseif pattern == ''
+ let pattern = a:{argcnt}
+ else
+ let filenames = filenames . ' ' . a:{argcnt}
+ endif
+ let argcnt += 1
+ endwhile
+
+ " If search pattern is not specified on command-line, ask for it
+ if pattern == ''
+ let pattern = input('Search pattern: ', expand('<cword>'))
+ if pattern == ''
+ return
+ endif
+
+ " Quote the supplied pattern
+ let pattern = g:Greplace_Shell_Quote_Char . pattern .
+ \ g:Greplace_Shell_Quote_Char
+ endif
+
+ if a:type == 'grep'
+ if filenames == ''
+ let filenames = input('Search in files: ', '*', 'file')
+ endif
+ elseif a:type == 'args'
+ " Search in all the filenames in the argument list
+ let arg_cnt = argc()
+
+ if arg_cnt == 0
+ call s:warn_msg('Error: Argument list is empty')
+ return
+ endif
+
+ let filenames = ''
+ for i in range(0, arg_cnt - 1)
+ let filenames .= ' ' . argv(i)
+ endfor
+ else
+ " Get a list of all the buffer names
+ let filenames = ''
+ for i in range(1, bufnr('$'))
+ let bname = bufname(i)
+ if bufexists(i) && buflisted(i) && filereadable(bname) &&
+ \ getbufvar(i, '&buftype') == ''
+ let filenames .= ' ' . bufname(i)
+ endif
+ endfor
+ endif
+
+ if filenames == ''
+ call s:warn_msg('Error: No valid file names')
+ return
+ endif
+
+ " Use ! after grep, so that Vim doesn't automatically jump to the
+ " first match
+ let grep_cmd = 'grep! ' . grep_opt . ' ' . pattern . ' ' . filenames
+
+ " Run the grep and get the matches
+ exe grep_cmd
+
+ call s:gReplace_show_matches()
+endfunction
+
+command! -nargs=0 Gqfopen call s:gReplace_show_matches()
+command! -nargs=* -complete=file Gsearch call s:gSearch('grep', <f-args>)
+command! -nargs=* Gargsearch call s:gSearch('args', <f-args>)
+command! -nargs=* Gbuffersearch call s:gSearch('buffer', <f-args>)
+
+" restore 'cpo'
+let &cpo = s:cpo_save
+unlet s:cpo_save
+
Please sign in to comment.
Something went wrong with that request. Please try again.