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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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> ...
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 ...
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 ...
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'. ...
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 ...
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 . ...
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"]
...
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 &....
7
votes
repeat insert at beginning of line
Just hit the enter key. It will take you to the start of the next line.
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 ...
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 ...
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 [...
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 ...
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 ...
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
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 ...
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 ...
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 ...
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 ...
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 ^...
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:
...
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 ...
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 ...
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 \...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
repeated-commands × 106normal-mode × 14
macro × 14
key-bindings × 13
replace × 8
command-line × 7
cut-copy-paste × 7
register × 7
vimrc × 5
substitute × 5
vimscript × 4
search × 4
visual-mode × 4
functions × 4
cursor-motions × 4
undo-redo × 4
operator × 3
neovim × 2
regular-expression × 2
formatting × 2
ex-mode × 2
global-command × 2
command-history × 2
count × 2
indentation × 1