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
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
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 ...
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
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 ...
9
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 ...
8
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 [...
7
votes
Accepted
Repeat complex actions over a number of lines (in Vim)
I like using a macro recording for this kind of task, since that allows me to use the Vim commands I know already to move and edit, rather than having to write (and debug!) regexps to accomplish the ...
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
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 ...
6
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'...
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 ...
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'
├───┘│ ├────┘
...
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 ...
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, ...
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 ...
4
votes
Accepted
Down and . X times?
You can use a macro, though it does require a (small) bit of planning ahead:
qqdt}jq4@q
will work, assuming your cursor is starting on the first line on the space after value:. Here's an explanation:...
4
votes
Down and . X times?
I would use a macro:
Juste before the deletion do:
qqdf)jq
Which means:
qq start to record a macro in the register q
df) your deletion
j go down one line
q stop recording the macro
Then you can ...
4
votes
Accepted
Repeat complicated motions
Plugin repmo-vim by Andy Wakula
The plugin #2174 listed on https://www.vim.org/scripts is, according to the description, outdated:
2016 Nov 22 started a new script at https://github.com/Houl/vim-...
4
votes
Accepted
Valid usecases for `=` macro register
So let's begin by quoting the doc :h @=
When typing the '=' after " or CTRL-R the cursor moves to the command-line,
where you can enter any expression (see |expression|). All normal
command-line ...
4
votes
Delete n lines skip 1 line script
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'
4
votes
How to apply a macro for every single matching pattern
Search for your pattern using /, then play your macro @@—you can record a new macro for this by doing
/pattern<CR>qqn@aq
Assuming @a holds your original macro. Then @q will jump to the next ...
4
votes
Accepted
Is it possible to properly repeat a visual replacement?
Explanation of the problem
The visual put + repeat pattern is quite broken in vim. Internally, vim implements visual puts as a literal visual delete followed by a normal mode put. However, only the ...
4
votes
Accepted
Understanding the use of ., in the global command
The . and , here are not Normal-mode commands for repeating last change and repeating last line motion...
Instead, the command executed by a :g is actually an Ex command, same as those you type in the ...
3
votes
Unexpected behavior using "<count>J" to join multiple lines
It is the described behaviour.
:help J states
Join [count] lines, with a minimum of two lines
3
votes
Accepted
remap ':' to maintain last command, and erase all char if any key other than <CR> was pressed
The following should solve your issue:
nnoremap <expr> : ShowLastCmd()
function! ShowLastCmd()
echohl ModeMsg
echo ':' . getreg(':')
echohl None
while 1
try
let char = ...
3
votes
Repeat not only last command
From what I understand Vim doesn’t have a way of repeating normal mode history use a while back, unless you write it out to a file:
https://vi.stackexchange.com/a/587/13848
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
repeated-commands × 111macro × 16
key-bindings × 14
normal-mode × 14
cut-copy-paste × 8
replace × 8
command-line × 7
register × 7
vimscript × 5
substitute × 5
vimrc × 4
neovim × 4
search × 4
visual-mode × 4
functions × 4
cursor-motions × 4
undo-redo × 4
operator × 3
regular-expression × 2
formatting × 2
ex-mode × 2
global-command × 2
command-history × 2
count × 2
indentation × 1