New answers tagged

0

I use the following plugin to search for visually selected text: function! VisualSelection() " Save register content and type. let old_reg = getreg('"') let old_regmode = getregtype('"') " Calling this function has ended visual mode, so it must be started " again before the selection can be yanked into the unnamed register. ...


2

One possible solution is to use what I call register recall: on the :-command line, Ctrl-r followed by any of the registers (the named ones, like a or 3, or the others, such as - or ") will insert the contents of said register. (This works in insert mode as well.) So you could Yank the text Substitute with :%s/<C-r>"/replacement/g It’s also ...


0

Use & in your replacement string, which replaces the whole match, so you can easily add to it. :%s/inc\d/&_new/g See :help :s& for more special sequences you can use in substitutions. In particular, if you want to use parts of the match, you can group parts with pairs of \( and \) and refer back to them with \1, \2, etc. Finally, for most ...


1

The main issue you're having here is that your match includes the beginning of the next line. After the replacement, Vim will keep looking for the next match, but it will start right after the last match ended, so it will miss the second line, since it won't match a regex that is anchored at the start of a line. In the specific example you gave, the first ...


8

You don't need to escape them: you could use a different separator for the search pattern and the replace part: %s;mvn;/opt/maven/bin/mvn;g But, if you really want to escape them, you can use a backslash (\): %s/mvn/\/opt\/maven\/bin\/mvn/g (much harder to read, IMO)


2

I just noticed a disadvantage That's because you're trying to remap a command. While "auto-replacements" in Vim are supposed to be done with abbreviations (see :help abbreviations). You can create an abbreviation like that: :inoreabbrev KDF \ac{KDF} Unlike commands, the abbreviations are typed as usual. And the substitution takes place when Vim knows ...


1

After some searching, it turns out that single character replacements work just as well for words. The answer is: :inoremap KDF \ac{KDF} As the help text says: "Map the key sequence {lhs} to {rhs}". When typing the specified word ("lhs") in insert mode, it will automatically be replaced (by the "rhs"). For example, when simply typing When using a KDF for, ...


5

Since you mentioned visual mode... You can also use the \%V match to only match inside the current visual selection (or last one, if not in visual mode.) Using \%V would allow you to use the same command, with minimal modification: :'<,'>s/\%V_/ /g That's assuming you have a visual selection around the quoted string, where you want to replace ...


Top 50 recent answers are included