Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way in vi(m) to target one specific line in a file for a string substitution?

I need to frequently add SSL SHA fingerprints from Web servers to my Mercurial .hgrc. I can grab the required signature from Chrome pretty easily by double-clicking the padlock and looking at the certificate details, but I get the sig with space delimiters rather than the colon delimiters desired by Mercurial

06 32 1C C5 8A 49 77 85 C5 89 6F 67 75 96 F0 43 77 96 EB 90

Example from Google Code.

Is there a way to:

  1. Copy said fingerprint to my clipboard
  2. Edit my .hgrc with vi(m)
  3. Go into insert mode to paste said fingerprint
  4. Target just that line with a

    :s/ /:/g

  5. Save the file

Right now I can paste the fingerprint into a new file, do the substitution, and pull it into my .hgrc with a :r filename, but that's an extra step that I wouldn't mind skipping.

share|improve this question
up vote 4 down vote accepted

Figured it out:

Move your cursor to the line you want to mod, and do this:

.s/ /\:/g

The '.' in front of the substitute command tells vi(m) that you only want to mod the current line.


share|improve this answer
After reading the link you posted I see that the dot is not required. My actual original expression works fine, since "The default range of lines is the current line only." I think I was misunderstanding reach of "g", figuring it would replace all the spaces in the document. – bpanulla Jul 29 '11 at 18:38

If you want to insert it after line N :

:N put =substitute(@+, ' ', '', 'g')

End of file: N=$ ; beginning of file: N=0. @+ is the clipboard register (when GUI is running).

share|improve this answer
Nice. Only downside is the extra step of having to know the explicit line number. But at least I see it displayed in the lower right of the editor. – bpanulla Jul 29 '11 at 15:38
you can state . or /pattern/ instead of N to refer to current line or next line matching pattern. – Benoit Jul 29 '11 at 15:43

are you meaning to do this?

echo "06 32 1C C5 8A 49 77 85 C5 89 6F 67 75 96 F0 43 77 96 EB 90" >> <filename>
share|improve this answer
Not really. Looking for answer that involves vim. – bpanulla Jul 29 '11 at 15:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .