-2

I have text:

I want to select nest and not nestlé in this text. Only the word nest!

and a list :

myList = ["nest"]
function MyFunction()
  myList = ["nest"]
  for elem in MyList
        if search("=/elem", 'W') == 0
           exe '%s/'elem '\{0\}/REPLACE/g'
        endif
  endfor

endfunction

Can you help me, please, to get strightly the word \<nest\>?

  • 3
    The question is quite difficult to parse – Maxim Kim Mar 25 at 9:40
  • 1
    What is “strightly”? That’s not an english word I’m familiar with... perhaps you meant strictly? – D. Ben Knoble Mar 25 at 12:49
  • The if search("=/elem", 'W') == 0 part doesn't make sense. It's literally searching for the exact string =/elem (no replacement for that variable) and why would you want to skip the next command if you find that anywhere in your text? – filbranden Mar 25 at 14:08
2

You have a text (where? in a buffer? in a variable?) and you want replace words given in a list in the text with REPLACE?

TEXT:

I want to select nest and not nestlé in this text. Only the word nest!

LIST OF WORDS:

['nest', 'this']

RESULT:

I want to select REPLACE and not nestlé in REPLACE text. Only the word REPLACE!

Is this correct?

Assuming TEXT is in buffer on a first row:

for word in ['nest', 'this']
    " replace text in the first row
    exe printf('1s/\<%s\>/%s/g', word, 'REPLACE')
endfor

enter image description here

If text is in variable, use substitute function instead:

let s:text = 'I want to select nest and not nestlé in this text. Only the word nest!'

for word in ['nest', 'this']
    let s:text = substitute(s:text, '\<'.word.'\>', 'REPLACE', 'g')
endfor

enter image description here

Here of course you can be creative, for example use for loop to generate regex with all words combined and apply substitute only once or smth else.

PS if you want to substitute text in a whole buffer you can do following:

for word in ['nest', 'this']
    " replace text in the whole buffer
    exe printf('%%s/\<%s\>/%s/g', word, 'REPLACE')
endfor

NOTE: %% is to escape percent char, otherwise printf will substitute %s command with word which is not what you want.

| improve this answer | |
  • instead of a loop, why not add the words to the :s command directly? something like this: %s/\<nest\>\|\<this\>/FOO/g, you can even do it in one line (although this is ugly): exe '%s/'.join(map(list, '''\<''.v:val.''\>'''), '\|'). '/FOO/g' – Christian Brabandt Mar 25 at 18:15
  • I didn't know what is really needed thus a simplified answer that might be what author wanted. And there is a note that it is possible to do for loop to generate a list of words. join and map also was in my mind but I am not sure author is ready for it. – Maxim Kim Mar 25 at 18:28
  • Hey, how did you run the code block selected? – 110100100 Mar 25 at 18:50
-2

I have found :

exe printf('1,'.line('$').'s/\<%s\>/%s/g', elem, 'REPLACE')
| improve this answer | |
  • 3
    if you need to substitute text in a whole buffer use :%s/word/toword/g, see :h :% – Maxim Kim Mar 25 at 12:00
  • 3
    Arsene: Instead of posting your own answer, you should just accept @MaximKim's answer. Please stop posting self-answers that are a rehash of what others have said, but often without enough explanation or context. If someone's answer fixed your problem, accept the answer. If you like it, upvote it. If you think it didn't get 100% right for some reason, leave a comment. (But understand that if answers don't get everything right, it might be your question that's too vague or under-specified, revisit and edit your question as needed to clarify it as much as possible.) Thanks! – filbranden Mar 25 at 14:18

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.