20 votes

Delete n lines skip 1 line script

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 ...
Ralf's user avatar
  • 9,097
18 votes
Accepted

How to copy first four characters of every line to the end of that same line?

:% s/\v^(\d{4})(.*)$/\1\2 \1/ is one way to do it \v magic option, to avoid having to escape grouping () ^ start of line \d{4} match exactly four digits .* rest of line \1 \2 has the matched ...
Sundeep's user avatar
  • 1,076
18 votes

Delete n lines skip 1 line script

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 one line then delete 3 lines So from the first line, go ...
Marth's user avatar
  • 356
12 votes
Accepted

repeat insert at beginning of line

Just use uppercase I instead of lowercase i The i and a Insert mode commands have uppercase versions I (insert at beginning of line) and A (append at end of line) So, if you use I##<Esc>, you ...
Sundeep's user avatar
  • 1,076
12 votes
Accepted

How can I insert a character repeatedly until even with next line?

Short answer v $ r * :help v_r Longer answer y y p / P     Duplicate line to below/above 2 l                    Move to position from where to overwrite (in this case 3rd column ← 2 to ...
Aaron Thoma's user avatar
12 votes

Repeat motion easily

Since you mentioned cursor movement but not ; there is of course ; Repeat latest f, t, F or T [count] times. , Repeat latest f, t, F or T in opposite direction [count] times. This is very ...
laktak's user avatar
  • 2,898
11 votes

How to copy first four characters of every line to the end of that same line?

And a solution with a macro: qqyiwA <Esc>pj0q Which means: qq Record the macro in the register q yiw Yank the text described by the text object iw (inner word): The date A <Esc> ...
statox's user avatar
  • 49.4k
11 votes

Replace . with -> in specific places

You could use the s operator, which deletes the character under your cursor and puts you in insert mode. You can then type the replacement and later use . to repeat the whole replacement. Workflow ...
Tumbler41's user avatar
  • 7,706
11 votes
Accepted

Repeat last yank

If you include the flag y in the cpoptions option; set cpoptions+=y then yanks may be repeated using .. See :help cpo-y: *cpo-y* y A yank command can be redone ...
Mass's user avatar
  • 14k
10 votes
Accepted

How to repeat last action with movement?

From the repeat documentation : . Repeat last change, with count replaced with [count]. Also repeat a yank command, when the 'y' flag is included in 'cpoptions'. ...
fruglemonkey's user avatar
  • 1,530
8 votes
Accepted

Repeat replace one character (r) for next occurence

Well, <Shift>v:s/"/'/<Enter> is certainly less efficient than :s/"/'<CR>. Here are various ways to perform the same task: f" jump to next " r' replace it with a ...
romainl's user avatar
  • 39.3k
8 votes
Accepted

Replace . with -> in specific places

The cw(change word) command will solve your problem. Put your cursor on the period and type cw-><Escape> something.somethingelse turns into something->somethingelse and the . ...
DoYouEvenCodeBro's user avatar
7 votes

How to insert repetitive text with minor difference in vim?

You can use the following function: function! GenerateLines() let firstpart="similar text part 1" let secondpart="similar text part 2" let words=["wordA","wordB","wordC","wordD","wordE"] ...
statox's user avatar
  • 49.4k
7 votes
Accepted

How can I repeat last Ex-mode command in normal mode?

Use the /c flag at the end of your substitution in order to confirm each substitution: :%s/foo/bar/gc See :help s_flags. Use & to repeat the last substitution without the flags. See :help &....
romainl's user avatar
  • 39.3k
7 votes

repeat insert at beginning of line

Just hit the enter key. It will take you to the start of the next line.
Herb's user avatar
  • 1,203
7 votes

Replace . with -> in specific places

I recommande using the substitute command: :%s/\./->/gc This will: :%s substitute over all lines /\. match dot /-> replace by arrow /g replace all on lines c ask for ...
nobe4's user avatar
  • 15.9k
7 votes

Replace . with -> in specific places

If you don't want to use a substitution or the dot command as suggested by the other answers your best bet is probably to create a macro which you can use when your cursor is next to a dot. You can ...
statox's user avatar
  • 49.4k
7 votes

Unexpected behavior using "<count>J" to join multiple lines

After further research and office discussion, I realize this is indeed the expected behavior. According to the vim help: J Join [count] lines, with a minimum of two lines. I've always thought [...
Eric Burgess's user avatar
6 votes
Accepted

Repeating the last move using . (w followed by .)

As stated in :h . the dot command only repeat changes, but commands like w are motions not changes. You can repeat motions like f, F, t and T with the commands ; and g; (see :h ;). And for more ...
statox's user avatar
  • 49.4k
6 votes
Accepted

Is there a way to encapsulate a variable with a method with a repeatable action?

If you already have the surround plugin, you can do ysiwfverify<CR>. Tim Pope has another plugin called repeat that lets you repeat surround commands with .. Without any plugin or setup, you ...
romainl's user avatar
  • 39.3k
6 votes
Accepted

Delete multiple lines by address

The bar | command can be used to separate multiple commands in a single command statement. Your example could be written as :1d|4d|32d http://vimdoc.sourceforge.net/htmldoc/cmdline.html#:\bar
ewatt's user avatar
  • 226
6 votes

How to copy first four characters of every line to the end of that same line?

Here is the way that I would do it: :%norm y4lA <C-o>p Explanation: :%norm "Apply the following keystrokes to every line: y4l "Yank 4 letters. You ...
DJMcMayhem's user avatar
  • 17.4k
6 votes

Is there a way to repeat the pænultimate (or older) change?

No, this is not possible. After some searching on the web, it appears other people have asked about this very feature before on other stack exchange sites: Repeat the nth last command in vim And ...
DJMcMayhem's user avatar
  • 17.4k
6 votes
Accepted

How to delete blocks of text which contain a specific string?

You can use a macro: Once your search register is set to Subject: Bar you can empty a register (q in this example) and record the following macro: qqq qqndap@qq The first line empties the q ...
statox's user avatar
  • 49.4k
6 votes

Repeat last normal command

. repeats the last change, and usually does what you want (combine with, e.g., cgn for a great sequence). For registers, @@ repeats the last register. @: repeats the last ex command. This is ...
D. Ben Knoble's user avatar
  • 25.4k
6 votes

Delete n lines skip 1 line script

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 ^...
Christian Brabandt's user avatar
6 votes

How to apply a macro for every single matching pattern

Another way that also works across files is to use :h :vimgrep with g flag: Without the 'g' flag each line is added only once. With 'g' every match is added. The workflow is: ...
dedowsdi's user avatar
  • 6,188
6 votes
Accepted

Replay a vim macro until specified line number

I think you can use this for specific line :10,15 norm! @d If you want something like until line, you can use this instead :.,15 norm! @d Or a more visual way, by selecting the lines you want to ...
Rickyson's user avatar
6 votes

Execute command for multiple files

First, you are running Vim, not Vi, because the latter does not offer the retab command. Assuming the file should be modified (that is what x does below), you can pipe the commands to Ex improved mode ...
Quasímodo's user avatar
  • 2,416
5 votes

How to insert repetitive text with minor difference in vim?

My strategy in such cases will vary, but it often follows the following steps: Write the unique word(s), e.g. wordA wordB ... wordK Either a) do a search and replace, e.g. :%s/\w\+/similar text \...
Karl Yngve Lervåg's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible