LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-09-2013, 05:31 PM   #1
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
vimscript: how delete a variable number of lines


In a vimscript I need to repeatedly delete lines from the buffer, but the number of lines to delete will vary with each iteration of the loop.
:d is the only command I know of that will remove entire lines, but how can I use it with a variable specifying the number of lines to be deleted?
 
Old 09-09-2013, 06:31 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
You can do it with a loop, like this:
Code:
:let linesToRemove =3
:while linesToRemove > 0
:  d
:  let linesToRemove -=1
:endwhile
 
1 members found this post helpful.
Old 09-09-2013, 06:48 PM   #3
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by TobiSGD View Post
You can do it with a loop, like this:
Code:
:let linesToRemove =3
:while linesToRemove > 0
:  d
:  let linesToRemove -=1
:endwhile
Many thanks. I also did it by setting marks and deleting between the marks, i.e
Code:
    :normal ma  
    :call cursor(lno, 1)  
    :normal mb  
    :'a,'bd
where lno is the line number of the last line of the group to be deleted
 
Old 09-09-2013, 07:53 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Looks more elegant than a loop, definitely, but it may be not a good idea to change marks, especially when you deploy your script to other users. I thought about that and with a little reading in the Vim documentation I found this way to do it:
Code:
"Get the line number of the current cursor position
"for info on that look at http://vimdoc.sourceforge.net/htmldoc/eval.html#line()
:let cLine = line(".")

"Since in your case lno already contains the number of the last line 
"in the block more math is not necessary, of course any kind of arithmetic
"to determine the last line can be done here

"Okay, now we need to create the command to delete the block and execute it
"More info: http://www.cs.csubak.edu/docs/vim/eval.html#:execute
:execute cLine . "," . lno . "d"

"For some reason you can't execute the command directly like
":cLine,lnod
"This will throw an error E488
This solution looks more complicated, but will not touch marks (that are possibly already set by the user).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] vimscript: from current line only return list of every awk variable reference porphyry5 Programming 4 09-07-2012 04:10 PM
Parse log file for errors and fetch variable number of lines before and after error jgm27 Linux - Newbie 4 07-18-2012 05:26 PM
[SOLVED] repeating lines X number of times based on variable using awk(?) captainentropy Linux - Newbie 2 06-25-2012 02:19 PM
[SOLVED] bash: sort lines in 2 files so that equal lines are at the same line number... masavini Programming 10 06-21-2012 01:58 PM
[SOLVED] Vimscript: using substitute() on a variable porphyry5 Programming 2 10-09-2011 04:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration