2

To format text in LaTeX you need to surround it with curly brackets, e.g. \emph{} to render it emphasized. I often need to add commands like that to selected text fragments, nothing that could be covered with a simple :%s/foo/bar/g substitution. I already figured out how to do the substitution for one selection with the help of Replace in Visual Selection, e.g.:

:'<,'>s/\v%V.*%V/\\emph{&}/

To do this for multiple words, I repeat:

  • select
  • type :
  • press followed by Enter

Take this as an example, let’s say I want to replace “bayc’” in the first, “minč’der̊” in the second and “oč’ t’e” and “ayl” in the third sentence – and can’t type those special characters easily:

Irenk’ miayn bołok’ kar-oł en nerkaya-c’n-el bayc’ da hayec’akarg č’-i kar-oł {hamar-v-el. (Ar̊avot 06.04.2006)} \\
Agah mecaharust-n ayr-v-um ēr džoxk’-i krak-ner-i meǰ minč’der̊ Ałk’at Łazaros-ě bazm-el ēr hayr Abraham-i {kołk’-i-n. (Nazaryan 2006: 170)} \\
Na naew ěndgc-el ē or hakamartut’yun-ě kar-oł en luc-el oč’ t’e mijnord-ner-ě ayl {kołm-er-ě. (Armenpress 25.03.2006)} \\

Is there an easier way to either do a substitution for multiple selections at once or repeat the command faster (something like . for ex-mode commands)?

  • I'm not sure I get your problem. Why can't you search with /, use cgn to replace the text and then use . to repeat or n to skip? – ChatterOne Dec 11 '18 at 12:35
  • @ChatterOne Because that’s not efficient, I need to replace this word in that sentence and then another one (and not the first!) in the next, see the example. – dessert Dec 11 '18 at 12:41
2

Vim offers at least a couple of different solutions to this problem.

Repeating an Ex Command

When you issue an ex command, Vim stores that command in the ": register. You can then repeat this command in the same way you play back a macro: by typing @:.

Subsequently, just like when playing back macros, you can repeat the command even faster @ by typing @@.

Using a Macro

An alternative is not to use a :substitute command at all, and instead to make the edits with a recorded macro. When you are in visual mode, type:

qqc\emph{}<Esc>Pq

Then, make another visual selection and press @q to playback the macro. Like in the above, subsequent playbacks can be initiated with @@.

  • Thanks, that helps a lot! A different approach might be possible with the vim-multiple-cursors plugin, but I don’t want to install a plugin and couldn’t test it. – dessert Dec 11 '18 at 16:04

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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