Given this search and replace expression, which works fine on one or multiple visually selected lines:
:'<,'>s/\v.{-}(`.{-}`).{-}(\\'.{-}\\').*/$this->execSQL('DELETE FROM \1 WHERE id = \2;');\r&/
I would like to stick this in a function to be call
'd or mapped to a key.
Something like this works for a single line, but behaves differently on multiple lines than when I run the search and replace directly:
function! CreateDeleteStatements()
:'<,'>s/\v.{-}(`.{-}`).{-}(\\'.{-}\\').*/$this->execSQL('DELETE FROM \1 WHERE id = \2;');\r&/
endfunction
Edit #1: By behaves differently on multiple lines
, it seems to be a cursor position problem.
By removing the leading :'<,'>
from the command within the function:
function! CreateDeleteStatements()
s/\v.{-}(`.{-}`).{-}(\\'.{-}\\').*/$this->execSQL('DELETE FROM \1 WHERE id = \2;');\r&/
endfunction
If I then select 4 lines, it will run 4 times, but only at the position of the first line (I end up with 4 lines above this, rather than interleaved between each line of the selection).
Have tried getpost()
and setpos()
and winsaveview()
and winrestview()
, but doesn't seem to be changing the cursor position after the substitution.
Edit #2: Adding sample text to work against
Example input text:
$this->execSQL('INSERT INTO `table_lowercase_and_underscores1` (`id`, `field1`, `field2`, `field3`, `field4`, `field5`) VALUES (\'first-guuid-lowercase-numbers-hyphens\', \'guuid-lowercase-numbers-hyphens\', \'guuid-lowercase-numbers-hyphens\', 0, \'2018-04-09 03:58:59\', 0);');
$this->execSQL('INSERT INTO `table_lowercase_and_underscores2` (`id`, `field1`, `field2`, `field3`, `field4`, `field5`) VALUES (\'first-guuid-lowercase-numbers-hyphens\', \'guuid-lowercase-numbers-hyphens\', \'guuid-lowercase-numbers-hyphens\', 0, \'2018-04-09 03:58:59\', 0);');
$this->execSQL('INSERT INTO `table_lowercase_and_underscores3` (`id`, `field1`, `field2`, `field3`, `field4`, `field5`) VALUES (\'first-guuid-lowercase-numbers-hyphens\', \'guuid-lowercase-numbers-hyphens\', \'guuid-lowercase-numbers-hyphens\', 0, \'2018-04-09 03:58:59\', 0);');
And expected output
$this->execSQL('DELETE FROM `table_lowercase_and_underscores1` WHERE id = \'first-guuid-lowercase-numbers-hyphens\';');
$this->execSQL('INSERT INTO `table_lowercase_and_underscores1` (`id`, `field1`, `field2`, `field3`, `field4`, `field5`) VALUES (\'first-guuid-lowercase-numbers-hyphens\', \'guuid-lowercase-numbers-hyphens\', \'guuid-lowercase-numbers-hyphens\', 0, \'2018-04-09 03:58:59\', 0);');
$this->execSQL('DELETE FROM `table_lowercase_and_underscores2` WHERE id = \'first-guuid-lowercase-numbers-hyphens\';');
$this->execSQL('INSERT INTO `table_lowercase_and_underscores2` (`id`, `field1`, `field2`, `field3`, `field4`, `field5`) VALUES (\'first-guuid-lowercase-numbers-hyphens\', \'guuid-lowercase-numbers-hyphens\', \'guuid-lowercase-numbers-hyphens\', 0, \'2018-04-09 03:58:59\', 0);');
$this->execSQL('DELETE FROM `table_lowercase_and_underscores3` WHERE id = \'first-guuid-lowercase-numbers-hyphens\';');
$this->execSQL('INSERT INTO `table_lowercase_and_underscores3` (`id`, `field1`, `field2`, `field3`, `field4`, `field5`) VALUES (\'first-guuid-lowercase-numbers-hyphens\', \'guuid-lowercase-numbers-hyphens\', \'guuid-lowercase-numbers-hyphens\', 0, \'2018-04-09 03:58:59\', 0);');