Skip to main content
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,287
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,096
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

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,953
12 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
  • 14.2k
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,756
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
  • 50.1k
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

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
  • 50.1k
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
  • 16.1k
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
  • 50.1k
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
7 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
  • 26.6k
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
  • 41.2k
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.7k
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.7k
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
  • 50.1k
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,298
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,466
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
5 votes
Accepted

How do I run a shell command periodically and silently from within Vi?

As @ChristianBrabandt mentions in the comments, presuming you're using a reasonably recent version of Vim, you could do this with a timer. In fact, you can virtually just copy-paste the example in ...
Rich's user avatar
  • 32.2k
5 votes

How to apply a register to a command?

This is not exactly what you want, but you could visually select your lines, then run this global command: g/^/exe 'norm! "adiwD' | exe 'norm! '.@a.'p' ├───┘│ ├────┘ ...
user938271's user avatar
  • 5,987
5 votes

Execute command for multiple files

You can use the -c argument in the command-line to run commands in Vim after opening a file. You can pass it multiple times to run the several separate commands. Once you run the editing commands, you'...
filbranden's user avatar
  • 29k
4 votes

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

If you have access to standard UNIX commands, you could use AWK: :%!awk '{print $0" "$1}'
romainl's user avatar
  • 41.2k
4 votes

Bundling command sequences together for repeat (.)

Vim only stores the last executed command, and only that gets repeated by .; in my opinion, anything else would be difficult to handle. If you feel the need to repeat the Yp combo as a single entity, ...
Ingo Karkat's user avatar
  • 17.9k
4 votes
Accepted

Run a command on every match of a search (not on the line)

You can use the expression register, see :h sub-replace-expression: :s/search/\=MyFunction(submatch(0))/ The submatch() function can also be used directly inside the function, which would make the ...
Karl Yngve Lervåg's user avatar
4 votes

Repeat motion easily

As the comments to your post indicate, the best solution to this question is usually to find a different approach, like vim-easymotion*. However, on the general topic of repeating a normal mode ...
Lstor's user avatar
  • 201

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