├── .gitignore
├── .watchmanconfig
├── LICENSE.md
├── README.md
├── autoload
└── scalpel.vim
├── doc
├── .gitignore
└── scalpel.txt
└── plugin
└── scalpel.vim
/.gitignore:
--------------------------------------------------------------------------------
1 | /media
2 | /scalpel-*.zip
3 |
--------------------------------------------------------------------------------
/.watchmanconfig:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wincent/scalpel/25147091c1c2a6475b0cef93ed38fee575fcffb5/.watchmanconfig
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | Copyright (c) 2016-present Greg Hurrell
2 |
3 | # MIT License
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining
6 | a copy of this software and associated documentation files (the
7 | "Software"), to deal in the Software without restriction, including
8 | without limitation the rights to use, copy, modify, merge, publish,
9 | distribute, sublicense, and/or sell copies of the Software, and to
10 | permit persons to whom the Software is furnished to do so, subject to
11 | the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be
14 | included in all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | # scalpel
9 |
10 |
11 | ## Intro
12 |
13 | Scalpel provides a streamlined shortcut for replacing all instances of the word currently under the cursor throughout a file.
14 |
15 | In normal mode pressing `e` (mnemonic: "edit") will display a prompt pre-populated with the current word and with the cursor placed so that you can start typing the desired replacement:
16 |
17 | ```
18 | :Scalpel/\v//
19 | ```
20 |
21 | Press `` and Scalpel will prompt to confirm each substitution, starting at the current word (unlike a normal `:%s` command, which starts at the top of the file).
22 |
23 | Scalpel works similarly in visual mode, except that it scopes itself to the current visual selection rather than operating over the entire file.
24 |
25 | Screencasts that show Scalpel in action:
26 |
27 | - https://youtu.be/YwMgnmZNWXA: "Vim screencast #13: Multiple Cursors"
28 | - https://youtu.be/7Bx_mLDBtRc: "Vim screencast #14: *Ncgn"
29 | - https://youtu.be/iNVyCPPYFzc: "Vim screencast #21: Scalpel update"
30 |
31 | Note that `:Scalpel` just calls through to an underlying `scalpel#substitute` function that does the real work, ultimately calling Vim's own `:substitute`. As such, be aware that whatever changes you make to the command-line prior to pressing `` must keep it a valid pattern, or bad things will happen.
32 |
33 | The mapping can be suppressed by setting:
34 |
35 | ```
36 | let g:ScalpelMap=0
37 | ```
38 |
39 | Or overridden:
40 |
41 | ```
42 | " Use s instead of default e:
43 | nmap s (Scalpel)
44 | ```
45 |
46 | In any case, Scalpel won't overwrite any pre-existing mapping that you might have defined for `e`, nor will it create an unnecessary redundant mapping if you've already mapped something to `(Scalpel)`.
47 |
48 | The `:Scalpel` command name can be overridden if desired. For example, you could shorten it to `:S` with:
49 |
50 | ```
51 | let g:ScalpelCommand='S'
52 | ```
53 |
54 | Then your Scalpel prompt would look like:
55 |
56 | ```
57 | :S/\v//
58 | ```
59 |
60 | The command can be entirely suppressed by setting `g:ScalpelCommand` to an empty string:
61 |
62 | ```
63 | let g:ScalpelCommand=''
64 | ```
65 |
66 | Finally, all plug-in functionality can be deactivated by setting:
67 |
68 | ```
69 | let g:ScalpelLoaded=1
70 | ```
71 |
72 | in your `~/.vimrc`.
73 |
74 |
75 | ## Installation
76 |
77 | To install Scalpel, use your plug-in management system of choice.
78 |
79 | If you don't have a "plug-in management system of choice" and your version of Vim has `packages` support (ie. `+packages` appears in the output of `:version`) then you can simply place the plugin at a location under your `'packpath'` (eg. `~/.vim/pack/bundle/start/scalpel` or similar).
80 |
81 | For older versions of Vim, I recommend [Pathogen](https://github.com/tpope/vim-pathogen) due to its simplicity and robustness. Assuming that you have Pathogen installed and configured, and that you want to install Scalpel into `~/.vim/bundle`, you can do so with:
82 |
83 | ```
84 | git clone https://github.com/wincent/scalpel.git ~/.vim/bundle/scalpel
85 | ```
86 |
87 | Alternatively, if you use a Git submodule for each Vim plug-in, you could do the following after `cd`-ing into the top-level of your Git superproject:
88 |
89 | ```
90 | git submodule add https://github.com/wincent/scalpel.git ~/vim/bundle/scalpel
91 | git submodule init
92 | ```
93 |
94 | To generate help tags under Pathogen, you can do so from inside Vim with:
95 |
96 | ```
97 | :call pathogen#helptags()
98 | ```
99 |
100 |
101 | ## FAQ
102 |
103 |
104 | ### Why use Scalpel rather than a built-in alternative?
105 |
106 | Scalpel is a lightweight plug-in that provides subtle but valuable improvements to the experience you'd get by using Vim's built-in functionality.
107 |
108 | Compared to writing a `:substitute` command manually:
109 |
110 | - Scalpel is quickly activated by a mapping.
111 | - Scalpel prepopulates the search pattern with the word currently under the cursor.
112 | - Scalpel avoids a jarring jump to the top of the file, instead starting replacements at the current location.
113 |
114 | Compared to a mapping such as "*Ncgn":
115 |
116 | - Scalpel allows you to preview the location at which each change will occur instead of performing the change blindly.
117 |
118 |
119 | ## Website
120 |
121 | Source code:
122 |
123 | - https://github.com/wincent/scalpel
124 | - https://gitlab.com/wincent/scalpel
125 | - https://bitbucket.org/ghurrell/scalpel
126 |
127 | Official releases are listed at:
128 |
129 | http://www.vim.org/scripts/script.php?script_id=5381
130 |
131 |
132 | ## License
133 |
134 | Copyright (c) 2016-present Greg Hurrell
135 |
136 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
137 |
138 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
139 |
140 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
141 |
142 |
143 | ## Development
144 |
145 |
146 | ### Contributing patches
147 |
148 | Patches can be sent via mail to greg@hurrell.net, or as GitHub pull requests at: https://github.com/wincent/scalpel/pulls
149 |
150 |
151 | ### Cutting a new release
152 |
153 | At the moment the release process is manual:
154 |
155 | - Perform final sanity checks and manual testing.
156 | - Update the [scalpel-history](#user-content-scalpel-history) section of the documentation.
157 | - Regenerate the documentation:
158 |
159 | ```
160 | docvim README.md doc/scalpel.txt
161 | ```
162 |
163 | - Verify clean work tree:
164 |
165 | ```
166 | git status
167 | ```
168 |
169 | - Tag the release:
170 |
171 | ```
172 | git tag -s -m "$VERSION release" $VERSION
173 | ```
174 |
175 | - Publish the code:
176 |
177 | ```
178 | git push origin main --follow-tags
179 | git push github main --follow-tags
180 | ```
181 |
182 | - Produce the release archive:
183 |
184 | ```
185 | git archive -o scalpel-$VERSION.zip HEAD -- .
186 | ```
187 |
188 | - Upload to http://www.vim.org/scripts/script.php?script_id=5381
189 |
190 |
191 | ## Authors
192 |
193 | Scalpel is written and maintained by Greg Hurrell <greg@hurrell.net>.
194 |
195 | Other contributors that have submitted patches include (in alphabetical order):
196 |
197 | - Keng Kiat Lim
198 |
199 | This list produced with:
200 |
201 | ```
202 | :read !git shortlog -s HEAD | grep -v 'Greg Hurrell' | cut -f 2-3 | sed -e 's/^/- /'
203 | ```
204 |
205 |
206 | ## History
207 |
208 |
209 | ### 1.1 (12 June 2020)
210 |
211 | - Automatically escape characters that may have special meaning for `/\v` (patch from Keng Kiat Lim, https://github.com/wincent/scalpel/pull/11).
212 |
213 |
214 | ### 1.0.1 (6 March 2019)
215 |
216 | - Prefer `execute()` when available to avoid potential nested `:redir` issues.
217 |
218 |
219 | ### 1.0 (3 January 2019)
220 |
221 | - Perform multiple replacements per line even when `'gdefault'` is on.
222 |
223 |
224 | ### 0.5 (28 July 2018)
225 |
226 | - Fix problem with `Visual` mode operation on older versions of Vim (GitHub issue #8).
227 |
228 |
229 | ### 0.4 (23 July 2018)
230 |
231 | - Fix problem with replacement patterns containing the number 1 (GitHub issue #7).
232 |
233 |
234 | ### 0.3 (10 May 2018)
235 |
236 | - Fix compatibility with older versions of Vim that don't implement `getcurpos()`.
237 |
238 |
239 | ### 0.2 (13 June 2016)
240 |
241 | - Support visual mode.
242 | - Do not show "N substitutions on N lines" messages.
243 |
244 |
245 | ### 0.1 (29 April 2016)
246 |
247 | - Initial release.
248 |
--------------------------------------------------------------------------------
/autoload/scalpel.vim:
--------------------------------------------------------------------------------
1 | " Copyright 2016-present Greg Hurrell. All rights reserved.
2 | " Licensed under the terms of the MIT license.
3 |
4 | function! scalpel#cword(curpos) abort
5 | " Doesn't work usefully in visual mode (always returns first word),
6 | " so fake it.
7 | let l:line=getline(a:curpos[1])
8 | let l:col=a:curpos[2]
9 | let l:chars=split(l:line, '\zs')
10 | let l:word=[]
11 |
12 | " Look for keyword characters rightwards.
13 | for l:char in l:chars[(l:col):]
14 | if match(l:char, '\k') != -1
15 | call add(l:word, l:char)
16 | else
17 | break
18 | endif
19 | endfor
20 |
21 | " Look for keyword characters leftwards.
22 | for l:char in reverse(l:chars[:(l:col) - 1])
23 | if match(l:char, '\k') != -1
24 | call insert(l:word, l:char, 0)
25 | else
26 | break
27 | endif
28 | endfor
29 |
30 | return join(l:word, '')
31 | endfunction
32 |
33 | function s:g()
34 | return &gdefault ? '' : 'g'
35 | endfunction
36 |
37 | " a:lastline is effectively reserved (see `:h a:lastline`), so use _lastline
38 | " instead.
39 | function s:replacements(currentline, _lastline, patterns, g)
40 | let s:report=&report
41 | try
42 | set report=10000
43 | execute a:currentline . ',' . a:_lastline . 's' . a:patterns . a:g . 'ce#'
44 | catch /^Vim:Interrupt$/
45 | execute 'set report=' . s:report
46 | return
47 | finally
48 | normal! q
49 | let s:transcript=getreg('s')
50 | if exists('s:register')
51 | call setreg('s', s:register)
52 | endif
53 | endtry
54 | endfunction
55 |
56 | function! scalpel#substitute(patterns, line1, line2, count) abort
57 | if a:count == -1
58 | " No range supplied, operate on whole buffer.
59 | let l:currentline=a:line1
60 | let l:firstline=1
61 | let l:lastline=line('$')
62 | else
63 | let l:firstline=a:line1 <= a:line2 ? a:line1 : a:line2
64 | let l:lastline=a:line2 >= a:line2 ? a:line2 : a:line1
65 | let l:currentline=l:firstline
66 | endif
67 |
68 | let l:g=s:g()
69 |
70 | " As per `:h E146`, can use any single-byte non-alphanumeric character as
71 | " delimiter except for backslash, quote, and vertical bar.
72 | if match(a:patterns, '\v^([^"\\|A-Za-z0-9 ]).*\1.*\1$') != 0
73 | echomsg 'Invalid patterns: ' . a:patterns
74 | echomsg 'Expected patterns of the form "/foo/bar/".'
75 | return
76 | endif
77 | if getregtype('s') != ''
78 | let s:register=getreg('s')
79 | elseif exists('s:register')
80 | unlet s:register
81 | endif
82 | normal! qs
83 |
84 | if exists('*execute')
85 | let l:replacements=execute('call s:replacements(l:currentline, l:lastline, a:patterns, l:g)', '')
86 | else
87 | redir => l:replacements
88 | call s:replacements(l:currentline, l:lastline, a:patterns, l:g)
89 | redir END
90 | endif
91 |
92 | if len(l:replacements) > 0
93 | " At least one instance of pattern was found.
94 | let l:last=strpart(s:transcript, len(s:transcript) - 1)
95 | if l:last ==# 'l' || l:last ==# 'q' || l:last ==# ''
96 | " User bailed.
97 | return
98 | elseif l:last ==# 'a'
99 | " Loop around to top of range/file and continue.
100 | " Avoid unwanted "Backwards range given, OK to swap (y/n)?" messages.
101 | if l:currentline > l:firstline
102 | " Drop c flag.
103 | execute l:firstline . ',' . l:currentline . '-&' . l:g . 'ce'
104 | endif
105 | return
106 | endif
107 | endif
108 |
109 | " Loop around to top of range/file and continue.
110 | " Avoid unwanted "Backwards range given, OK to swap (y/n)?" messages.
111 | if l:currentline > l:firstline
112 | execute l:firstline . ',' . l:currentline . '-&' .l:g . 'ce'
113 | execute 'set report=' . s:report
114 | endif
115 | endfunction
116 |
--------------------------------------------------------------------------------
/doc/.gitignore:
--------------------------------------------------------------------------------
1 | tags
2 |
--------------------------------------------------------------------------------
/doc/scalpel.txt:
--------------------------------------------------------------------------------
1 | *scalpel.txt* Scalpel plug-in for Vim *scalpel*
2 |
3 | CONTENTS *scalpel-contents*
4 |
5 | 1. Intro |scalpel-intro|
6 | 2. Installation |scalpel-installation|
7 | 3. FAQ |scalpel-faq|
8 | 4. Website |scalpel-website|
9 | 5. License |scalpel-license|
10 | 6. Development |scalpel-development|
11 | 7. Authors |scalpel-authors|
12 | 8. History |scalpel-history|
13 |
14 | INTRO *scalpel-intro*
15 |
16 | Scalpel provides a streamlined shortcut for replacing all instances of the
17 | word currently under the cursor throughout a file.
18 |
19 | In normal mode pressing `e` (mnemonic: "edit") will display a prompt
20 | pre-populated with the current word and with the cursor placed so that you
21 | can start typing the desired replacement:
22 | >
23 | :Scalpel/\v//
24 | <
25 | Press `` and Scalpel will prompt to confirm each substitution, starting
26 | at the current word (unlike a normal `:%s` command, which starts at the top of
27 | the file).
28 |
29 | Scalpel works similarly in visual mode, except that it scopes itself to the
30 | current visual selection rather than operating over the entire file.
31 |
32 | Screencasts that show Scalpel in action:
33 |
34 | - https://youtu.be/YwMgnmZNWXA: "Vim screencast #13: Multiple Cursors"
35 | - https://youtu.be/7Bx_mLDBtRc: "Vim screencast #14: *Ncgn"
36 | - https://youtu.be/iNVyCPPYFzc: "Vim screencast #21: Scalpel update"
37 |
38 | Note that `:Scalpel` just calls through to an underlying `scalpel#substitute`
39 | function that does the real work, ultimately calling Vim's own `:substitute`.
40 | As such, be aware that whatever changes you make to the command-line prior
41 | to pressing `` must keep it a valid pattern, or bad things will happen.
42 |
43 | The mapping can be suppressed by setting:
44 | >
45 | let g:ScalpelMap=0
46 | <
47 | Or overridden:
48 | >
49 | " Use s instead of default e:
50 | nmap s (Scalpel)
51 | <
52 | In any case, Scalpel won't overwrite any pre-existing mapping that you might
53 | have defined for `e`, nor will it create an unnecessary redundant
54 | mapping if you've already mapped something to `(Scalpel)`.
55 |
56 | The `:Scalpel` command name can be overridden if desired. For example, you
57 | could shorten it to `:S` with:
58 | >
59 | let g:ScalpelCommand='S'
60 | <
61 | Then your Scalpel prompt would look like:
62 | >
63 | :S/\v//
64 | <
65 | The command can be entirely suppressed by setting `g:ScalpelCommand` to an
66 | empty string:
67 | >
68 | let g:ScalpelCommand=''
69 | <
70 | Finally, all plug-in functionality can be deactivated by setting:
71 | >
72 | let g:ScalpelLoaded=1
73 | <
74 | in your `~/.vimrc`.
75 |
76 | INSTALLATION *scalpel-installation*
77 |
78 | To install Scalpel, use your plug-in management system of choice.
79 |
80 | If you don't have a "plug-in management system of choice" and your version
81 | of Vim has `packages` support (ie. `+packages` appears in the output of
82 | `:version`) then you can simply place the plugin at a location under your
83 | `'packpath'` (eg. `~/.vim/pack/bundle/start/scalpel` or similar).
84 |
85 | For older versions of Vim, I recommend
86 | [Pathogen](https://github.com/tpope/vim-pathogen) due to its simplicity and
87 | robustness. Assuming that you have Pathogen installed and configured, and
88 | that you want to install Scalpel into `~/.vim/bundle`, you can do so with:
89 | >
90 | git clone https://github.com/wincent/scalpel.git ~/.vim/bundle/scalpel
91 | <
92 | Alternatively, if you use a Git submodule for each Vim plug-in, you could do
93 | the following after `cd`-ing into the top-level of your Git superproject:
94 | >
95 | git submodule add https://github.com/wincent/scalpel.git ~/vim/bundle/scalpel
96 | git submodule init
97 | <
98 | To generate help tags under Pathogen, you can do so from inside Vim with:
99 | >
100 | :call pathogen#helptags()
101 | <
102 | FAQ *scalpel-faq*
103 |
104 | Why use Scalpel rather than a built-in alternative? ~
105 |
106 | Scalpel is a lightweight plug-in that provides subtle but valuable
107 | improvements to the experience you'd get by using Vim's built-in
108 | functionality.
109 |
110 | Compared to writing a |:substitute| command manually:
111 |
112 | - Scalpel is quickly activated by a mapping.
113 | - Scalpel prepopulates the search pattern with the word currently under the
114 | cursor.
115 | - Scalpel avoids a jarring jump to the top of the file, instead starting
116 | replacements at the current location.
117 |
118 | Compared to a mapping such as "*Ncgn":
119 |
120 | - Scalpel allows you to preview the location at which each change will occur
121 | instead of performing the change blindly.
122 |
123 | WEBSITE *scalpel-website*
124 |
125 | Source code:
126 |
127 | - https://github.com/wincent/scalpel
128 | - https://gitlab.com/wincent/scalpel
129 | - https://bitbucket.org/ghurrell/scalpel
130 |
131 | Official releases are listed at:
132 |
133 | http://www.vim.org/scripts/script.php?script_id=5381
134 |
135 | LICENSE *scalpel-license*
136 |
137 | Copyright (c) 2016-present Greg Hurrell
138 |
139 | Permission is hereby granted, free of charge, to any person obtaining a copy
140 | of this software and associated documentation files (the "Software"), to
141 | deal in the Software without restriction, including without limitation the
142 | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
143 | sell copies of the Software, and to permit persons to whom the Software is
144 | furnished to do so, subject to the following conditions:
145 |
146 | The above copyright notice and this permission notice shall be included in
147 | all copies or substantial portions of the Software.
148 |
149 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
150 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
151 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
152 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
153 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
154 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
155 | IN THE SOFTWARE.
156 |
157 | DEVELOPMENT *scalpel-development*
158 |
159 | Contributing patches ~
160 |
161 | Patches can be sent via mail to greg@hurrell.net, or as GitHub pull requests
162 | at: https://github.com/wincent/scalpel/pulls
163 |
164 | Cutting a new release ~
165 |
166 | At the moment the release process is manual:
167 |
168 | - Perform final sanity checks and manual testing.
169 | - Update the [scalpel-history](#user-content-scalpel-history) section of the
170 | documentation.
171 | - Regenerate the documentation:
172 | >
173 | docvim README.md doc/scalpel.txt
174 | <
175 | - Verify clean work tree:
176 | >
177 | git status
178 | <
179 | - Tag the release:
180 | >
181 | git tag -s -m "$VERSION release" $VERSION
182 | <
183 | - Publish the code:
184 | >
185 | git push origin main --follow-tags
186 | git push github main --follow-tags
187 | <
188 | - Produce the release archive:
189 | >
190 | git archive -o scalpel-$VERSION.zip HEAD -- .
191 | <
192 | - Upload to http://www.vim.org/scripts/script.php?script_id=5381
193 |
194 | AUTHORS *scalpel-authors*
195 |
196 | Scalpel is written and maintained by Greg Hurrell .
197 |
198 | Other contributors that have submitted patches include (in alphabetical
199 | order):
200 |
201 | - Keng Kiat Lim
202 |
203 | This list produced with:
204 | >
205 | :read !git shortlog -s HEAD | grep -v 'Greg Hurrell' | cut -f 2-3 | sed -e 's/^/- /'
206 | <
207 | HISTORY *scalpel-history*
208 |
209 | 1.1 (12 June 2020) ~
210 |
211 | - Automatically escape characters that may have special meaning for |/\v|
212 | (patch from Keng Kiat Lim, https://github.com/wincent/scalpel/pull/11).
213 |
214 | 1.0.1 (6 March 2019) ~
215 |
216 | - Prefer |execute()| when available to avoid potential nested |:redir| issues.
217 |
218 | 1.0 (3 January 2019) ~
219 |
220 | - Perform multiple replacements per line even when |'gdefault'| is on.
221 |
222 | 0.5 (28 July 2018) ~
223 |
224 | - Fix problem with |Visual| mode operation on older versions of Vim (GitHub
225 | issue #8).
226 |
227 | 0.4 (23 July 2018) ~
228 |
229 | - Fix problem with replacement patterns containing the number 1 (GitHub
230 | issue #7).
231 |
232 | 0.3 (10 May 2018) ~
233 |
234 | - Fix compatibility with older versions of Vim that don't implement
235 | |getcurpos()|.
236 |
237 | 0.2 (13 June 2016) ~
238 |
239 | - Support visual mode.
240 | - Do not show "N substitutions on N lines" messages.
241 |
242 | 0.1 (29 April 2016) ~
243 |
244 | - Initial release.
245 |
--------------------------------------------------------------------------------
/plugin/scalpel.vim:
--------------------------------------------------------------------------------
1 | " Copyright 2016-present Greg Hurrell. All rights reserved.
2 | " Licensed under the terms of the MIT license.
3 |
4 | ""
5 | " @header
6 | "
7 | " @image https://raw.githubusercontent.com/wincent/scalpel/media/scalpel.png center
8 | " @image https://raw.githubusercontent.com/wincent/scalpel/media/scalpel.gif center
9 | "
10 |
11 | ""
12 | " @plugin scalpel Scalpel plug-in for Vim
13 | "
14 | " # Intro
15 | "
16 | " Scalpel provides a streamlined shortcut for replacing all instances of the
17 | " word currently under the cursor throughout a file.
18 | "
19 | " In normal mode pressing `e` (mnemonic: "edit") will display a prompt
20 | " pre-populated with the current word and with the cursor placed so that you can
21 | " start typing the desired replacement:
22 | "
23 | "
24 | " ```
25 | " :Scalpel/\v//
26 | " ```
27 | "
28 | " Press `` and Scalpel will prompt to confirm each substitution, starting
29 | " at the current word (unlike a normal `:%s` command, which starts at the top of
30 | " the file).
31 | "
32 | " Scalpel works similarly in visual mode, except that it scopes itself to the
33 | " current visual selection rather than operating over the entire file.
34 | "
35 | " Screencasts that show Scalpel in action:
36 | "
37 | " - https://youtu.be/YwMgnmZNWXA: "Vim screencast #13: Multiple Cursors"
38 | " - https://youtu.be/7Bx_mLDBtRc: "Vim screencast #14: *Ncgn"
39 | " - https://youtu.be/iNVyCPPYFzc: "Vim screencast #21: Scalpel update"
40 | "
41 | " Note that `:Scalpel` just calls through to an underlying `scalpel#substitute`
42 | " function that does the real work, ultimately calling Vim's own `:substitute`.
43 | " As such, be aware that whatever changes you make to the command-line prior to
44 | " pressing `` must keep it a valid pattern, or bad things will happen.
45 | "
46 | " The mapping can be suppressed by setting:
47 | "
48 | " ```
49 | " let g:ScalpelMap=0
50 | " ```
51 | "
52 | " Or overridden:
53 | "
54 | " ```
55 | " " Use s instead of default e:
56 | " nmap s (Scalpel)
57 | " ```
58 | "
59 | " In any case, Scalpel won't overwrite any pre-existing mapping that you might
60 | " have defined for `e`, nor will it create an unnecessary redundant
61 | " mapping if you've already mapped something to `(Scalpel)`.
62 | "
63 | " The `:Scalpel` command name can be overridden if desired. For example, you
64 | " could shorten it to `:S` with:
65 | "
66 | " ```
67 | " let g:ScalpelCommand='S'
68 | " ```
69 | "
70 | " Then your Scalpel prompt would look like:
71 | "
72 | " ```
73 | " :S/\v//
74 | " ```
75 | "
76 | " The command can be entirely suppressed by setting `g:ScalpelCommand` to an
77 | " empty string:
78 | "
79 | " ```
80 | " let g:ScalpelCommand=''
81 | " ```
82 | "
83 | " Finally, all plug-in functionality can be deactivated by setting:
84 | "
85 | " ```
86 | " let g:ScalpelLoaded=1
87 | " ```
88 | "
89 | " in your `~/.vimrc`.
90 | "
91 | " # Installation
92 | "
93 | " To install Scalpel, use your plug-in management system of choice.
94 | "
95 | " If you don't have a "plug-in management system of choice" and your version of
96 | " Vim has `packages` support (ie. `+packages` appears in the output of
97 | " `:version`) then you can simply place the plugin at a location under your
98 | " `'packpath'` (eg. `~/.vim/pack/bundle/start/scalpel` or similar).
99 | "
100 | " For older versions of Vim, I recommend
101 | " [Pathogen](https://github.com/tpope/vim-pathogen) due to its simplicity and
102 | " robustness. Assuming that you have Pathogen installed and configured, and that
103 | " you want to install Scalpel into `~/.vim/bundle`, you can do so with:
104 | "
105 | " ```
106 | " git clone https://github.com/wincent/scalpel.git ~/.vim/bundle/scalpel
107 | " ```
108 | "
109 | " Alternatively, if you use a Git submodule for each Vim plug-in, you could do
110 | " the following after `cd`-ing into the top-level of your Git superproject:
111 | "
112 | " ```
113 | " git submodule add https://github.com/wincent/scalpel.git ~/vim/bundle/scalpel
114 | " git submodule init
115 | " ```
116 | "
117 | " To generate help tags under Pathogen, you can do so from inside Vim with:
118 | "
119 | " ```
120 | " :call pathogen#helptags()
121 | " ```
122 | "
123 | " # FAQ
124 | "
125 | " ## Why use Scalpel rather than a built-in alternative?
126 | "
127 | " Scalpel is a lightweight plug-in that provides subtle but valuable
128 | " improvements to the experience you'd get by using Vim's built-in
129 | " functionality.
130 | "
131 | " Compared to writing a |:substitute| command manually:
132 | "
133 | " - Scalpel is quickly activated by a mapping.
134 | " - Scalpel prepopulates the search pattern with the word currently under the
135 | " cursor.
136 | " - Scalpel avoids a jarring jump to the top of the file, instead starting
137 | " replacements at the current location.
138 | "
139 | " Compared to a mapping such as "*Ncgn":
140 | "
141 | " - Scalpel allows you to preview the location at which each change will occur
142 | " instead of performing the change blindly.
143 | "
144 | " # Website
145 | "
146 | " Source code:
147 | "
148 | " - https://github.com/wincent/scalpel
149 | " - https://gitlab.com/wincent/scalpel
150 | " - https://bitbucket.org/ghurrell/scalpel
151 | "
152 | " Official releases are listed at:
153 | "
154 | " http://www.vim.org/scripts/script.php?script_id=5381
155 | "
156 | " # License
157 | "
158 | " Copyright (c) 2016-present Greg Hurrell
159 | "
160 | " Permission is hereby granted, free of charge, to any person obtaining a copy
161 | " of this software and associated documentation files (the "Software"), to deal
162 | " in the Software without restriction, including without limitation the rights
163 | " to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
164 | " copies of the Software, and to permit persons to whom the Software is
165 | " furnished to do so, subject to the following conditions:
166 | "
167 | " The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
168 | "
169 | " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
170 | " IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171 | " FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
172 | " AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
173 | " LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
174 | " OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
175 | " SOFTWARE.
176 | "
177 | " # Development
178 | "
179 | " ## Contributing patches
180 | "
181 | " Patches can be sent via mail to greg@hurrell.net, or as GitHub pull requests
182 | " at: https://github.com/wincent/scalpel/pulls
183 | "
184 | " ## Cutting a new release
185 | "
186 | " At the moment the release process is manual:
187 | "
188 | " - Perform final sanity checks and manual testing.
189 | " - Update the [scalpel-history](#user-content-scalpel-history) section of the documentation.
190 | " - Regenerate the documentation:
191 | "
192 | " ```
193 | " docvim README.md doc/scalpel.txt
194 | " ```
195 | "
196 | " - Verify clean work tree:
197 | "
198 | " ```
199 | " git status
200 | " ```
201 | "
202 | " - Tag the release:
203 | "
204 | " ```
205 | " git tag -s -m "$VERSION release" $VERSION
206 | " ```
207 | "
208 | " - Publish the code:
209 | "
210 | " ```
211 | " git push origin main --follow-tags
212 | " git push github main --follow-tags
213 | " ```
214 | "
215 | " - Produce the release archive:
216 | "
217 | " ```
218 | " git archive -o scalpel-$VERSION.zip HEAD -- .
219 | " ```
220 | "
221 | " - Upload to http://www.vim.org/scripts/script.php?script_id=5381
222 | "
223 | " # Authors
224 | "
225 | " Scalpel is written and maintained by Greg Hurrell .
226 | "
227 | " Other contributors that have submitted patches include (in alphabetical
228 | " order):
229 | "
230 | " - Keng Kiat Lim
231 | "
232 | " This list produced with:
233 | "
234 | " ```
235 | " :read !git shortlog -s HEAD | grep -v 'Greg Hurrell' | cut -f 2-3 | sed -e 's/^/- /'
236 | " ```
237 | "
238 | " # History
239 | "
240 | " ## 1.1 (12 June 2020)
241 | "
242 | " - Automatically escape characters that may have special meaning for |/\v|
243 | " (patch from Keng Kiat Lim, https://github.com/wincent/scalpel/pull/11).
244 | "
245 | " ## 1.0.1 (6 March 2019)
246 | "
247 | " - Prefer |execute()| when available to avoid potential nested |:redir|
248 | " issues.
249 | "
250 | " ## 1.0 (3 January 2019)
251 | "
252 | " - Perform multiple replacements per line even when |'gdefault'| is on.
253 | "
254 | " ## 0.5 (28 July 2018)
255 | "
256 | " - Fix problem with |Visual| mode operation on older versions of Vim
257 | " (GitHub issue #8).
258 | "
259 | " ## 0.4 (23 July 2018)
260 | "
261 | " - Fix problem with replacement patterns containing the number 1
262 | " (GitHub issue #7).
263 | "
264 | " ## 0.3 (10 May 2018)
265 | "
266 | " - Fix compatibility with older versions of Vim that don't implement
267 | " |getcurpos()|.
268 | "
269 | " ## 0.2 (13 June 2016)
270 | "
271 | " - Support visual mode.
272 | " - Do not show "N substitutions on N lines" messages.
273 | "
274 | " ## 0.1 (29 April 2016)
275 | "
276 | " - Initial release.
277 |
278 | " Provide users with means to prevent loading, as recommended in `:h
279 | " write-plugin`.
280 | if exists('g:ScalpelLoaded') || &compatible || v:version < 700
281 | finish
282 | endif
283 | let g:ScalpelLoaded = 1
284 |
285 | " Temporarily set 'cpoptions' to Vim default as per `:h use-cpo-save`.
286 | let s:cpoptions = &cpoptions
287 | set cpoptions&vim
288 |
289 | let s:command=get(g:, 'ScalpelCommand', 'Scalpel')
290 | if s:command ==# ''
291 | finish
292 | elseif match(s:command, '\v\C^[A-Z][A-Za-z]*$') == -1
293 | echoerr 'g:ScalpelCommand must contain only letters and start with a ' .
294 | \ 'capital letter'
295 | finish
296 | endif
297 | execute 'command! -nargs=1 -range '
298 | \ . s:command
299 | \ . ' call scalpel#substitute(, , , )'
300 |
301 | function! s:GetCurposCompat()
302 | if exists('*getcurpos')
303 | return getcurpos()
304 | else
305 | return getpos('.')
306 | endif
307 | endfunction
308 |
309 | " Need to remember last-seen cursor position because `getcurpos()` is not useful
310 | " in VISUAL modes.
311 | let s:curpos=s:GetCurposCompat()
312 | augroup Scalpel
313 | autocmd!
314 | autocmd CursorMoved * let s:curpos=s:GetCurposCompat()
315 | augroup END
316 |
317 | " Local accessor so that we can reference the script-local variable from inside
318 | " a mapping (as per http://superuser.com/questions/566720).
319 | function! s:GetCurpos()
320 | return s:curpos
321 | endfunction
322 |
323 | " For escaping any characters which could have special meaning under \v (very
324 | " magic) semantics ie. all ASCII characters except '0'-'9', 'a'-'z', 'A'-'Z'
325 | " and '_'.
326 | let g:magic_chars='"' . escape("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", '"/\|') . '"'
327 |
328 | " Change all instances of current word (mnemonic: edit).
329 | execute 'nnoremap (Scalpel) :' .
330 | \ s:command .
331 | \ "/\\v<=escape(expand(''), " . g:magic_chars . ")>//"
332 | execute 'vnoremap (ScalpelVisual) :' .
333 | \ s:command .
334 | \ "/\\v<=escape(scalpel#cword(GetCurpos()), " . g:magic_chars . ")>//"
335 |
336 | let s:map=get(g:, 'ScalpelMap', 1)
337 | if s:map
338 | if !hasmapto('(Scalpel)') && maparg('e', 'n') ==# ''
339 | nmap e (Scalpel)
340 | endif
341 | if !hasmapto('(ScalpelVisual)') && maparg('e', 'v') ==# ''
342 | vmap e (ScalpelVisual)
343 | endif
344 | endif
345 |
346 | " Restore 'cpoptions' to its former value.
347 | let &cpoptions = s:cpoptions
348 | unlet s:cpoptions
349 |
--------------------------------------------------------------------------------