The help page for mode() only mentions normal mode
That's not true. The help page (:h mode()) is quite complete. You have to supply an optional argument for the mode() function.
let s:mode = mode(1)
if s:mode ==# 'n'
if s:mode ==# 'i'
" normal using i_CTRL-O
elseif s:mode ==# 'o'
" normal Operator-pending
The other answer doesn’t give the full command (note the equals sign):
even though the default is supposed to be inclusive. On my debian box I had to add it to .vimrc because it was exclusive.
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%...
The easiest solution to me would be:
%: 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.
You have some great solutions already available. Here is another one:
:g/^/if line('.')%4!=1|:s/^/DELETE ME/|endif
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, ...
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
If prefer doing this interactively, you can make use of the "command-line register". That is, press
:+1del 3<CR> and then 1000@:
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.