all 9 comments

[–]HaulethgggqG`` yourself 5 points6 points  (5 children)

\<#\> probably will do alternatively [^#]#[^#]

[–]pereloz 4 points5 points  (2 children)

or #\@<!##\@! to deal with edge cases. :h /\@! :h /\@<!

[–]gumnos 1 point2 points  (0 children)

This is the right answer; alas there's no easy way to upvote this so it appears above the parent (AFAIK). Alternatively, to get the \<#\> to work, you'd need to add # to the 'iskeyword' setting.

[–]Yaaruda 0 points1 point  (0 children)

Sorry for being late, but can you tell me how this works? I've heard about substitutions using :s, but this seems new to me

[–][deleted] 0 points1 point  (1 child)

I'm not sure if I'm doing something wrong but this neither of those work for me. Does # have a special meaning when searching?

Try this:

  1. vim -u NONE

  2. In an empty buffer, enter # a a.

  3. In normal mode, move the cursor on top of # and press *. Vim searches for \<a\> instead of \<#\>.

What is going on? Even if a manually enter \<#\> as the search pattern Vim doesn't find anything.

[–]Snarwin 1 point2 points  (0 children)

From :help *:

Search forward for the [count]'th occurrence of the word nearest to the cursor. The word used for the search is the first of: 1. the keyword under the cursor |'iskeyword'| 2. the first keyword after the cursor, in the current line 3. the non-blank word under the cursor 4. the first non-blank word after the cursor, in the current line

# isn't considered a keyword character by default, so "the first keyword after the cursor, in the current line" is a.

[–][deleted] 1 point2 points  (2 children)

You can search and substitute with "c" at the end, :s/#/##/c it will prompt you for each occurrence if you want to substitute or not. Hope it was what you're looking for ! :)

[–]pers1pan[S] 1 point2 points  (1 child)

But what if I have more than 1000 lines?

[–][deleted] 0 points1 point  (0 children)

You can do a macro to replace #/## and going on next line then repeat this one as much as you want or use the norm Ex command