*SrchRplcHiGrp.txt* Search and Replace Restricted to a Highlighting Group Author: David Fishburn August 25, 2015 ============================================================================== 1. Contents *srchrplchigrp* *srchrplchigrp-contents* 1. Contents......................: |srchrplchigrp-contents| 2. Commands......................: |srchrplchigrp-commands| SR............................: |SR| SRDispHiGrp...................: |SRDispHiGrp| SRChooseHiGrp.................: |SRChooseHiGrp| SRHiGrp.......................: |SRHiGrp| 3. Examples......................: |srchrplchigrp-examples| 4. History.......................: |srchrplchigrp-history| ============================================================================== 2. Commands *srchrplchigrp-commands* SR = Search and Replace *SR* SRDispHiGrp *SRDispHiGrp* Displays the syntax id and name the of the syntax group which has been chosen. SRChooseHiGrp[!][id] *SRChooseHiGrp* Before you can run the search and replace command (:SRHiGrp), you must choose which syntax group id you want to operate on. The top level syntax id of the current cursor position is chosen (ie. the top-level one versus the final one). The optional bang lets SRChooseHiGrp use the translated syntax ID. This is final one versus the top-level one. Assuming we were using a SQL file and placed the cursor on any word in a Comment section. Typically SQL comments can use any of these formats: -- This is a comment // This is also a comment /* * This is a multi * line comment */ Examples: > :SRChooseHiGrp < SRHiGrp - Group ID: 497 Name: sqlDashComment Will operate on only sqlDashComment syntax groups > :SRChooseHiGrp! < SRHiGrp - Group ID: 46 Name: Comment Will operate on all Comment syntax groups. Looking at the syntax file for this we see: > hi def link sqlDashComment Comment hi def link sqlSlashComment Comment hi def link sqlMultiComment Comment < Therefore SRChooseHiGrp! will operate over all of the above syntax groups: > sqlDashComment sqlSlashComment sqlMultiComment > :SRChooseHiGrp 46 < SRHiGrp - Group ID: 46 Name: Comment Instead of choosing the syntax group the cursor is on this allows you to programmatically choose the exact group id you want. SRSearch[!] *SRSearch* This command will perform a forward search starting at the current cursor position for text in the specified highlight group name. The range defaults to the entire file. It supports all visual modes, characterwise (v), linewise (V) and blockwise (). First choose a highlight group using SRChooseHiGrp. Providing no arguments will search until it finds text highlighted in that syntax. > :SRSearch < Using the bang (!) it will search for the next text that is not using the syntax group: > :SRSearch! < It optionally takes takes a regex parameter. You can supply a hightlight group name: > :SRSearch some text :SRSearch \(first\|second\|word\) < Running SRSearch a second time will ensure the cursor is positioned on the next separate highlight matched text. There must be a gap between the two groups. SRHiGrp[!] *SRHiGrp* This command will perform a search and replace over a range. The range defaults to the entire file. It works in all visual modes, characterwise (v), linewise (V) and blockwise (). It optionally takes takes 2 parameters. Parameter 1 controls what characters are matched. The default for this value is \(\w\+\>\). This expression is appended to the \%# which starts the match from the current cursor position. This expression must specify a submatch \(...\). Parameter 2 controls what to do with the matched string. The default for this value is \U\1. This expression will cause the matched string to be UPPER cased. The \1 refers to the submatch from the first parameter. If the parameters are not supplied, the user will be prompted to enter the expression(s). The optional bang (!) works the same as SRHiGrp, but will operate on all syntax groups that are NOT the chosen one (SRChooseHiGrp). Syntax: > [range]SRHiGrp[!] 'from-pattern','to-string' < ============================================================================== 3. Examples *srchrplchigrp-examples* SRHiGrp ------- First place your cursor on an item that is syntax colored the way you want. Notice the command takes an optional !. If your first search and replace doesn't work, you can undo the change and reselect the group using the ! and try again. > :SRChooseHiGrp SRHiGrp - Group ID: 562 Name: sqlStatement or :SRChooseHiGrp! SRHiGrp - Group ID: 51 Name: Statement < Next, visually select a block of text (all visual modes are supported) > :SRHiGrp or :SRHiGrp '\(\w\+\>\)' or :SRHiGrp '\(\w\+\>\)', '\U\1' < If you had the following in a SQL file: > if exists( select 1 from sys.sysprocedure sp key join sys.sysuserperm sup where sp.proc_name = 'sp_http_course_detail' and sup.user_name = user_name() ) then drop procedure sp_http_course_detail; end if; < Where the keywords (if, exists, select, from ...) are all highlighted yellow (based on my colorscheme). After I visually select the area and run the command taking default prompts: > :'<,'>SRHiGrp < The result is: > IF EXISTS( SELECT 1 FROM sys.sysprocedure sp KEY JOIN sys.sysuserperm sup WHERE sp.proc_name = 'sp_http_course_detail' AND sup.user_name = user_name() ) THEN DROP PROCEDURE sp_http_course_detail; END IF; < Where the keywords (if, exists, select, from ...) are all highlighted yellow (based on my colorscheme). After I visually select the area and run the command taking default prompts: > :'<,'>SRHiGrp! < The result is: > if exists( select 1 from SYS.SYSPROCEDURE SP key join SYS.SYSUSERPERM SUP where SP.PROC_NAME = 'SP_HTTP_COURSE_DETAIL' and SUP.USER_NAME = USER_NAME() ) then drop procedure SP_HTTP_COURSE_DETAIL; end if; < SRSearch -------- SRSearch simply does a forward search for the specified highlight group. You must first use SRChooseHiGrp to choose a highlight group. Find the next item highlighted as that syntax group: > :SRSearch :1,5SRSearch :'<,'>SRSearch < Find the next item highlighted that is NOT that syntax group: > :SRSearch! :1,5SRSearch! :'<,'>SRSearch! < Find the next item highlighted as that syntax group and matches the regular expression supplied: > :SRSearch something :SRSearch \(first\|second\|word\) < Find the next item highlighted that is NOT that syntax group and matches the regular expression supplied: > :SRSearch! \(first\|second\|word\) < The results of the search is displayed in the command line and is highlighted in the color of the syntax group. This will remind you which group was searched for. > SRSearch - Match found - Group ID: 171 Name: sqlKeyword SRSearch - Match NOT found - Group ID: 171 Name: sqlKeyword SRSearch - Match found - Group ID: 171 Name: sqlKeyword Regex: \(first\|second\|word\) SRSearch - Match NOT found - Group ID: 171 Name: sqlKeyword Regex \(first\|second\|word\) < ============================================================================== 4. History *srchrplchigrp-history* Version 7 (August 25, 2015) - Changed SRSeach. It will first look for the next item that has the syntax group chosen via SRChooseHiGrp. It will also take an optional regular expression and not only find that syntax group, but also match the regular expression. Added SRSearch!, which will find the next item that is NOT what was chosen via SRChooseHiGrp. Same applied with the regular expression, so if I put my cursor on a comment and :SRChooseHiGrp. Then ran :SRSearch! something, it will find the word "something" that is NOT in a comment. Version 6 (July 27, 2015) - Changed to save and restore cp options on load - Changed to use Vim's autoload mechanism to load only when required (speeds Vim's load time and memory usage) - When using SRHiGrp! (operate over non-matching areas) the plugin always reported "Did not find highlight group" (Mathieu Westphal) - SRHiGrp now defaults the range to the entire file, instead of the current row vim:tw=78:ts=8:ft=help:norl: