forked from standardebooks/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive-sr
executable file
·33 lines (27 loc) · 1.03 KB
/
interactive-sr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/sh
usage(){
fmt <<EOF
DESCRIPTION
Use Vim to perform an interactive search and replace on a list of files.
USAGE
interactive-sr REGEX FILE [FILE...]
REGEX is in the form of "/FIND/REPLACE/".
If using () backreferences, parentheses must be escaped.
REGEX is vim-flavored.
Do not pass any flags to REGEX.
Use y/n/a to confirm (y) or reject (n) a replacement, or to replace (a)ll.
EOF
exit
}
require(){ command -v $1 > /dev/null 2>&1 || { suggestion=""; if [ ! -z "$2" ]; then suggestion=" $2"; fi; die "$1 is not installed.${suggestion}"; } }
if [ $# -eq 1 ]; then if [ "$1" = "--help" -o "$1" = "-h" ]; then usage; fi fi
#End boilerplate
require "vim" "Try: apt-get install vim"
#Store the regex
regex="$1"
#Remove the regex from the list of arguments
shift
#'set title' shows the filename in the terminal title
#'set eventignore-=Syntax' enables syntax highlighting in all files
#'wqa writes and quits all buffers
vim "+silent set title" "+silent bufdo set eventignore-=Syntax | %s${regex}gce | silent update" "+silent qa" $@