2

I would like to know how to add a space and then text at the end of a line. For example, adding a space and then /abc/aaa/abc/. I am trying :%s/$/ /abc/aaa/abc/ but that does not work. I know I am missing something.

1
  • 2
    Hint: How should the :s command know which of the many slashes is meant as delimiter between search pattern and replacement? So you can either escape all other slashes to make them part of the string (see answer by Shadoath), or use another character than slash as delimiter (see :help :s) And have a look at how to format code in your postings here. Have fun.
    – Philippos
    Mar 31 '17 at 6:29
3

Alternatively, since you're appending and repeating, you could use a macro:

Press qqA /abc/aaa/abc/ESCq Note: "ESC" means to press your escape key, don't literally type ESC. Also, notice the literal space after A.

qq and q are to start recording a macro to register q, and end the macro, respectively. From there, just go to any single line you'd like to append this text to and type @q

Macros can also be repeated, so if you had say 4 lines in a row that you wanted to make this change to, you could add a j at the end of the macro like so: qqA /abc/aaa/abc/ESCjq Then, with the cursor on the first (top) of the four lines, you could do 4@q and all 4 lines will be appended to.

1
  • Macros are great for larger and more complex substitutions, but they might be a bit overkill for this simpler example. But, if you know macros and are comfortable with them, by all means. Whatever works best for each user.
    – 0xSheepdog
    Apr 3 '17 at 17:44
2

You are very close. The reason :%s/$/ /abc/aaa/abc/ does not work here is that you are not escaping the / character in your output string.

Run it like this %s/$/ \/abc\/aaa\/abc/ Leaving only the last / unescaped.

The substitute command has some serious power that you can play with once the nuances of the command are learned.

EDIT: All the escaping is not necessary. By changing the substitute delimiter from the default / to _, #, or most non-alphanumeric characters. Your substitution could read as: :%s_$_ /abc/aaa/abc_ or :%s#$# /abc/aaa/abc#

Thank you @Philippos for pointing this out in the comments.

3
  • 4
    Or less confusing: You can use a different delimiter for :s, if your patterns contain slashes: :%s_$_ /abc/aaa/abc/_
    – Philippos
    Mar 31 '17 at 6:19
  • That is the correct one to use. %s/$/ \/abc\/aaa\/abc/. Found the solution this afternoon along with a coworker. thank you again !!
    – Morgan
    Mar 31 '17 at 23:32
  • 1
    @Philippos makes a very good point. To be clear for those who may not get it, the suggestion is to use the "underbar" character _ as the substitute field delimiter, instead of the slash / character, since slash is part of the string substitute is working with.
    – 0xSheepdog
    Apr 3 '17 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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