The Stack Overflow podcast is back! Listen to an interview with our new CEO.

New answers tagged

3

Using a :global command You can use a :global command to run a command on every line that matches a pattern. Try the following: :g/^int .*() {$/put You can equally easily run a macro once on each line that matches the pattern: :g/^int .*() {$/normal! @q Using a macro An alternative solution is to alter your macro so that: Instead of just moving down ...


3

The brackets are for chars. Use subpattern (parentheses) instead. \%(a\|the\)\@3<! book \%( ... \) --- a (unnumbered) submatch a\|the --- "a" or "the" \@3<! --- NO match behind (3 bytes lookback)


1

Try using \0 to insert the contents of the match in the substitution: :%s/\<[a-zA-Z0-9.]\+@[a-zA-Z0-9.]*\>/"\0"/g Note that, given that you gave an example of an email address, I added . to your character classes so that the expression will match these, but it won't only match email addresses. e.g. It would match a@a. I also changed the white spaces ...


2

@Rich's solution is a perfect general solution, however maybe your question is a good example of an XY problem. For your specific use case Vim has :h CTRL-A and :h CTRL-X: CTRL-A Add [count] to the number or alphabetic character at or after the cursor. CTRL-X Subtract [count] from the number or alphabetic character ...


3

One way to do this is by using \zs to set the "start" of the match, so that everything before \zs is untouched by the replacement: :%s/RESULT=\zs.*/200/g Your original attempt using a lookbehind was also on the right track: you just put the .* part in the wrong place: :%s/\(RESULT=\)\@<=.*/200/g This replaces anything that comes after a RESULT=, ...


1

an alternative: you want to keep any lines: 1, 4, etc. You could use: awk '!((FNR-1)%3)' file > newfile # awk: when a condition is given without action: # prints the lines for which the condition returns 0. # FNR designates the line number in the current file. # a%3 : takes the modulus of a and 3. ( 0%3=0, 1%3=1, 2%3=2, 3%...


4

I know this is the vi channel, but to me this is a sed problem. sed -ne 'p;n;n;n' <file >newfile So you can wrangle it into a vi solution: :0 !Gsed -ne 'p;n;n;n'


17

The easiest solution to me would be: :%norm j3dd That is: %: for every line norm: run the following keys as if in normal mode j3dd: go down on line then delete 3 lines So from the first line, go down to the second one and delete the next 3 lines. The second Text I want to keep. is now on the second line. Go down one line, delete 3. Rinse and repeat. ...


6

You have some great solutions already available. Here is another one: :g/^/if line('.')%4!=1|:s/^/DELETE ME/|endif :g/^DELETE ME/d First, we perform an action on every line (:g matching against the ^ (start of line)) and for every line number perform the result of linenumber % 4. If the result is unequal to 1, we add DELETE ME at the beginning of the line, ...


3

An alternative (I suppose we start on the line 1, and delete the lines 2-4, 6-8 and so on, as per an example text): while line(".") < line("$") silent +1delete _ 3 endwhile If prefer doing this interactively, you can make use of the "command-line register". That is, press :+1del 3<CR> and then 1000@:


19

Very simple approach: Move to the first line you want to delete. Record a macro: qa3ddjq Repeat it with a high number: 1000@a Step three will repeat the macro a thousand times or until an error is encountered. Hitting end of file (hence no lines to delete) produces an error and repetition of the macro is canceled. See :help recording.


Top 50 recent answers are included