Sabuncu
Sabuncu

Reputation: 5264

Decode Vim find/replace string that calls a function

I am using the following Vim command line that inserts line numbers to the beginning of lines:

:let i = 1 | %s/^/\='LINE_' . Inc()/g

Inc() is a function that increments the i variable.

This is all working fine. My questions:

1) What does the dot do in the replacement part?:

:let i = 1 | %s/^/\='LINE_' . Inc()/g
                            ^        

2) What does the pipe character do? Is there actual piping going on, or is it just syntax?

3) What does the \= do? I think it is used to call the function, but Vim help only shows information for \= as being a quantifier in regex.

4) I have not been able to insert a space after the line number and the first character of the actual line. How can I do this? Anything I place after Inc() in the replacement part is either being ignored or causing an E15 invalid expression error.

I am using Vim 7.3 on Windows 7.

Upvotes: 0

Views: 133

Answers (1)

Peter Rincker
Peter Rincker

Reputation: 45117

Some explanation:

  1. . expression will concatenate two strings. See :h expr-.
  2. | will separate to ex-commands. See :h :bar
  3. A replacement starting with \= in :s command means the rest of the replacement is to be treated as an vim expression. See :h :s\=
  4. Concatenate a string with a space after the Inc() function call. :let i = 1 | %s/^/\='LINE_' . Inc() . ' '/g

Upvotes: 1

Related Questions