New answers tagged

2

To add to the list of plugins allowing this, vim-abolish features the Subvert function which can be leveraged to achieve multiple replaces: %Subvert/{1,0}/{0,1}/g


1

There has no rmap, replace mode map is created in imap with a mode test: inoremap <expr> <a-k> mode() ==# 'R' ? 'replace' : 'insert' :h mode() is transient, you must test it in :h map-expr. Note it won't work for r.


2

Here's a simple macro-based solution. First place your cursor at the beginning of bunch: 0W. Then type: qqqqqd/\v($|"https)Enterlhxf"xl@qq This records a recursive macro into the "q register that will remove everything from the current position to the end of the line, apart from the URLs. Then undo the changes you made while recording the macro: 3u Now ...


2

Here's a quick and dirty solution. You can certainly find/create input HTML that will break this, but it works fine for simple input such as your example: Add a " at the end of the commas: :norm! 0Ea" This is simply using a :normal command to execute the equivalent of typing 0Ea" in normal mode. Split the line on the URLs: :s/"\zehttps/\r/g Here we use ...


0

Use this substitution command: %s/\(\d*,,,\).*href="\(https:.*\)" .*/\1 \2 / explanation: % means "all lines in the buffer" \d*,,, is any number of digits followed by 3 "," .*href=" is anything following up to https: " .* is all the text from closing quote to the end of the line including the quote (note the space between quote and dot. enclosing ...


0

Use the :normal command with a sequence of steps This is similar to the answer using a macro, but instead of recording the steps, we'll pass them to the :normal command and use a range to execute the sequence on every line. We can use the following command: :.,.+3normal ^"adW"bd3l$"bp"ap The .,.+3 part is a range and it means from this line until 3 lines ...


2

Record and replay a macro You can record a macro to handle each line, and then replay it to handle the following lines using the same sequence of commands. Let's make a somewhat more interesting example, with varying lengths of variable names, with unaligned =s, but fixed single space around the =s. Let's also add some indentation to the lines: v1 = ...


0

Use blockwise Visual mode on aligned columns of text If the = signs are aligned, you can use blockwise Visual mode to execute this replacement. Let's make a somewhat more interesting example, with varying lengths of variable names, but still aligned =s. Let's also add some indentation to the lines: v1 = a1 v2 = a2 v345 = a345 ...


1

Use a :substitute command with a regex Using a :substitute command is pretty Vimish! This method will be good for you if you feel comfortable with Vim regexes, or if your input is uniform enough that you can use a simple regex to match it. A simple way would be to use Visual mode to select a range of lines where to apply the transformation, then use the ...


1

:%s/\([01]\)/\=1-submatch(1)/g The following is taking place in the substitute command Search for 1 or 0, remember it as submatch 1 (between the first / and second /) Replace with the value by subtracting from 1 (between the second / and third /) 1 becomes 0 and 0 becomes 1 \= is interpreted as the beginning of a mathematical expression in the substituted ...


6

I'm actually surprised there is no native tr in vi/vim, but if your platform is UNIXy enough, there's an external command for just this: :%!tr 01 10 This is what I would personally use, but it may not be portable enough for you.


5

Similar to @LucHermitte's answer, the SwapStrings plugin lets you do :%SwapStrings 0 1 My own PatternsOnText plugin provides (among many others) a :SubstituteMultiple command that can do swaps as a corner case of doing multiple substitutions atomically (that is, without introducing a temp replacement): :%SubstituteMultiple /0/1/ /1/0/ g


5

I'm quite certain I've already gave an answer to the general case either here on vi.SE, or on stackoverflow, but impossible to find the Q/A... In that answer I provide the command CycleSubstitute that could be used in your case in the following way :[range]CycleSubstitute/0/1 I've packaged it here. And it's implemented in the following way: :command! -...


14

There's a bunch of excellent answers here already, but for the sake of completeness I feel like I should point out that, in most practical respects, if you concatenate multiple ex commands with the bar character |, they act like a single operation. In particular, they will lead to a single item in each of the undo tree and changelist. As such, the ...


Top 50 recent answers are included