I have a list of file names I'm trying to create insert statements out of. In each file name is the ID I need for the primary key, that I just need to copy from inside the file name to the front of the line (or rear really) so I can continue to build the statement.

[0-9][0-9][0-9] will find the string I need, I am just not sure how to "store" it and place it at the front of the line. I just need to know what goes in the place of "whatgoeshere" I guess? Or am I going about it all wrong?


EDITED for examples:



Desired Output:

001, 'ABC001.jpg'
002, 'ABC002.TIF' 
066, 'DEF66.JPG'
011, '\\server\subfolder\XYZ011.png'
1234, 'JKL1234.png'

Which I will then turn into (which I know how to do once the previous step is done):

insert into table_name(ID, file_loc) values (001, '\\server\subfolder\ABC001.jpg');
insert into table_name(ID, file_loc) values (002, '\\server\subfolder\ABC002.TIF');
---etc and so on
  • So these file names are all in a single text file that you are manipulating? Please provide examples of your inputs and outputs. Input meaning a sample of the line you want to process and output meaning the result of the processing (an insert statement, I guess).
    – B Layer
    Aug 15, 2017 at 19:36
  • All of these are on separate lines as examples (and it's stripping my spacing and line breaks :\ "ABC001.jpg" "ABC002.TIF" "\\server\subfolder\XYZ011.png" output I would need to start building the rest of the insert statement (which I already know how to do) 001, 'ABC001.jpg' 002, 'ABC002.TIF' 011, '\\server\subfolder\XYZ011.png' Aug 15, 2017 at 19:55
  • Examples should go in your question. The more information you provide there the more likely you are to get a quality answer.
    – B Layer
    Aug 15, 2017 at 19:59

1 Answer 1


If I understand correctly, what you're looking for is \(\) and \1 to capture the match and then use it later.

As an example, you could do something like this:

:%s/\([0-9][0-9][0-9]\)/^other stuff \1/

The \( and \) around the search pattern captures it and the \1 goes where ever you want the pattern to go in the replacement part (after "stuff " in my example). You can also capture multiple groups and refer to them with \2, \3, etc. See :help \( for more info.

As a side note, you should be able to simplify your search pattern with \d\{3} to find three consecutive digits.

EDIT based on your examples:

Based on your examples, this is what I would do:

%s/"\(\D*\(\d\+\)\D*\)"/\2, '\1'/

This will extract the number and put it at the beginning of the line as well as change the double quotes to single quotes. This assumes that each line only contains a string within double quotes and that there is only one set of numbers within the line. This doesn't handle adding the 0 at the start of your third example, but this stackoverflow post might help with that: https://stackoverflow.com/questions/1316963/vim-padding-out-lines-with-a-character

Explanation of the search pattern:

  • " - Search for the first double quote.
  • \( - Start capturing the string within the double quotes so we can print it later. It will be \1.
  • \D* - Find as many non-digit characters as possible at the start of the double quote.
  • \( - Start a second capture group of just the digits. This will be \2 in the replacement.
  • \d\+ - Find as many digit characters as possible (at least one).
  • \) - End the capture group for the digits.
  • \D* - Find the remaining non-digit characters within the double quotes.
  • \) - End the capture group of the entire string within the double quotes.
  • " - Find the ending double quote.
  • So that replaces the 3 digit number with "^other stuff || 3 digit number". For some reason the ^ is being treated literally. The \1 is definitely what I'm looking for. The number should be 3 digits 0 filled but can be 1 or 2 digits. Aug 15, 2017 at 17:28
  • Oh, yeah. I wasn't really thinking about the ^. I believe that only represents the beginning of the line in the search pattern (not the replacement). You can use \d\{1,3} to match 1 to 3 digits. Check out :help \{ for some more variations.
    – Pak
    Aug 15, 2017 at 17:37
  • I think you'll need to include the beginning (or end) of the line in the search pattern in order to place the number at the beginning. If you update your question with an example line and the desired result, then I can update my answer to explain it better.
    – Pak
    Aug 15, 2017 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.