Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Another approach is to use normal mode to delete a block of text you want. For instance, if you want to delete up to the line before the line that contains "This is a line of text", you could run

    q:
    .,/This is a line of text/- d
. is the current line. /This is a line of text/ is the line that contains it and the - after the regex refers to the line immediately above. You could use _ to refer to the line immediately after, or -x or +x where x refers to the number of lines above or below the line that the regex matched.


Never knew about the - or _ as flags for the search regex. This is awesome!


It's not actually a flag to the s command. It's a way to modify the address. So, for instance, if you want to delete from the third line to the third to last line in the file, you could run:

    :1+2,$-2 d
1 is the address of the first line in the file and $ is the address of the last line in the file. Add 2 to the first to get the third line and subtract 2 from the last to get the third to last line. The d command deletes the lines in the address range.

You can also address lines with a regex, like I did in the example in my previous comment. You can even use regex addressing for ranges. If you wanted to delete a python method definition in between other method definitions, you could do something like:

  :/def method_to_delete/,/def/- d
which would start the range with the line that contains def method_to_delete and delete lines up to the line before the next line that contains the def keyword that starts the next method definition.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: