Created
February 23, 2022 18:10
-
-
Save mkropat/8d4b024b59defc9a590b6c1a016b632f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Pattern 1: | |
" | |
" * Operate over a whole file: :call ConvertFile() | |
" * Or operate over a selection: :'<,'>call ConvertLine() | |
" * Supports the same :s// and normal commands you regularly use in Vim | |
" * So minimal Vim Script knowledge needed | |
function! ConvertFile() | |
%global/^/call ConvertLine() | |
endfunction | |
function! ConvertLine() | |
let line = getline(".") | |
if line =~ "SOME_PATTERN" | |
substitute/SOME_PATTERN/SOME_REPLACEMENT/ | |
substitute/ANOTHER_PATTERN// | |
" You can even search ahead | |
normal k$ | |
/SEARCH_PATTERN | |
" Then run a command | |
substitute/SOME_PATTERN/SOME_REPLACEMENT/ | |
elseif line =~ "SOME_OTHER_PATTERN" | |
" Change {} braces to []'s | |
normal $ma%r]`ar[ | |
endif | |
endfunction | |
" Pattern 2: | |
" | |
" * Operate over a selected range: :'<,'>call ConvertRange() | |
" * Supports deleting lines that match a regex | |
function! ConvertRange() range | |
let i = a:firstline | |
while i <= a:lastline && i <= line('$') | |
let line = getline(i) | |
if line =~ "SOME_PATTERN_TO_DELETE" | |
execute i "delete _" | |
continue | |
endif | |
if line =~ "SOME_PATTERN_TO_MODIFY" | |
let line = substitute(line, "SOME_PATTERN", "SOME_REPLACEMENT", "") | |
let line = substitute(line, "ANOTHER_PATTERN", "", "") | |
call setline(i, line) | |
endif | |
let i += 1 | |
endwhile | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment