124

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
  • 17,426
  • 15
  • 71
  • 93
  • Related: https://vi.stackexchange.com/questions/1915/how-do-i-delete-a-large-block-of-text-without-counting-the-lines – J. Chomel Dec 15 '17 at 12:44

5 Answers5

254

You could use something like d63G to delete from the current line until line 63.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
120

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)

borrible
  • 17,120
  • 7
  • 53
  • 75
23

Same as the accepted answer, but slightly faster to type:

d63gg deletes from the current line to line 63.

apostl3pol
  • 874
  • 8
  • 15
11

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!

Gary_W
  • 9,933
  • 1
  • 22
  • 40
7

To delete a block of lines in Vi:

n: is from line number

m: is to line number

  1. From current line until a given line number-

    :,md

  2. from/to specific line numbers

    :n,md

Community
  • 1
  • 1
SteveScm
  • 545
  • 1
  • 7
  • 15