Delete from the current cursor position to a given line number in vi editor
Solution 1
You could use something like d63G
to delete from the current line until line 63.
Solution 2
To delete from a to b use
:a,bd
from current to b use
:,bd
(where a and b in code are replaced by your numbers)
Solution 3
Same as the accepted answer, but slightly faster to type:
d63gg
deletes from the current line to line 63.
Solution 4
Why count lines? Go to the last line you want to delete and mark it by typing ma
which "marks" it with identifier "a". Then go up to the top line that you want to delete and type d'a
for delete to mark "a". Bam!
Solution 5
To delete a block of lines in Vi:
n
: is from line number
m
: is to line number
From current line until a given line number-
:,md
from/to specific line numbers
:n,md
seanhodges
A senior software developer with an extensive background in Web and mobile technology. Has a passion for creative and modern product development, working with small to medium-sized teams in an Agile environment. Practical experience in project and team management.
Updated on May 25, 2020Comments
-
seanhodges almost 4 years
How do I delete a block of text from the current cursor row to a given line number in vi?
For example:
49 <j:set var="changeSet" value="${build.changeSet}" /> <----- delete from here (cursor position) 50 <j:if test="${changeSet!=null}"> 51 <j:set var="hadChanges" value="false" /> 52 <TABLE width="100%"> 53 <TR><TD class="bg1" colspan="2"><B>CHANGES</B></TD></TR> 54 <j:forEach var="cs" items="${changeSet.logs}" varStatus="loop"> 55 <j:set var="hadChanges" value="true" /> 56 <j:set var="aUser" value="${cs.hudsonUser}"/> 57 <TR> 58 <TD colspan="2" class="bg2">${spc}Revision <B>${cs.revision}</B> by 59 <B><j:choose> 60 <j:when test="${aUser!=null}">${aUser.displayName}: </j:when> 61 <j:otherwise>${cs.user}: </j:otherwise> 62 </j:choose></B> 63 <B>(${cs.msgAnnotated})</B> <----- to here (line 63) 64 </TD> 65 </TR> 66 <j:forEach var="p" items="${cs.paths}"> 67 <TR> 68 <TD width="10%">
In Vim I would usually use the visual selection mode for this, but I don't have Vim at my disposal on this server. It would also be quicker to specify a line number rather than counting how many lines are within the block in some cases.
-
seanhodges almost 13 yearsExcellent. I was able to do ":.,63d" to perform the deletion example above. Thanks borrible!
-
seanhodges almost 13 yearsOooh, a solution in normal mode - even better!
-
ThinkCode over 10 yearsTo add to this, if you want to delete lines 45 through 64, you do :45,64d
-
German Khokhlov almost 6 yearsDoesn't work (vim 7.4.160, CentOS). Perhaps it needs some extension or special config.