Solved Using vi to delete large blocks

I'm trying a freebsd-update and get presented with a lot of changes which involve manual intervention, ie editing with vi.

One of the changes involves removing a large block from the likes of /etc/ssh/moduli although there are others.

Not being an expert on vi. I only 'dd' for deleting the current line. How can I select a block and then delete it?
 
Several ways, depends on how you want to select what you want to delete.
Every vi command can be combined with a number of how many times it shall be repeated, or a move command, (until) where to go.
So for example starting at the line your cursor currently is 30dd would delete 30 lines, ddG deletes all lines til the end.
. (the point) repeats the last command; so after using dd, you simply can use . to repeat the deletion of a line instead of pressing dd over and over again.
Important to know is also u for undo, if you deleted more than you wanted.
There are other ways, especially when using vim there are more options, but this shall not become a beginners class for vi. 😁
 
Vi can add 26 different marks from a to z.
ma on the line to mark.
To move to the marked line, use 'a
You can delete from the current line to the marked line with d'a.
 
How can I select a block and then delete it?
Without selecting: 'd' followed by a movement command that brings to the end of where you want to delete.
It can be a search command (e.g / followed by string).

You can mark a position with 'm' followed by a letter between a and z naming it.
And move to the position with ` followed by the name,
or ' followed by the name if the position is the line where you put the mark.
 
I only 'dd' for deleting the current line. How can I select a block and then delete it?
With vim(1) you can use SHIFT-V and select a whole bunch of lines, then delete then with d.

With vi(1) you can dd to delete a single line, then use . (dot) to repeat the previous command. That's what I typically use. But I usually just set EDITOR to vim(1), which allows me to use SHIFT-V to select a whole block of lines.

My ~/.cshrc (I'm using tcsh(1)/csh(1), but you can do the same for sh(1)) usually has:
Code:
setenv EDITOR vi
if ( -x /usr/local/bin/vim ) then
  alias vi vim
  setenv EDITOR vim
endif
 
Several ways, depends on how you want to select what you want to delete.
Every vi command can be combined with a number of how many times it shall be repeated, or a move command, (until) where to go.
So for example starting at the line your cursor currently is 30dd would delete 30 lines, ddG deletes all lines til the end.
Thanks for that, it's what I was looking for, but it turned out to be dG. ddG ends up as dd then G
 
This is related, sorta. On Linux, in terminal I could just select text in nano by holding Shift+ArrowUp/Dn/PgDn/PgUp. Not so in KDE's konsole? Or is it FreeBSD-related?
 
By the way, the deleted content is stored in an unnamed buffer and a numbered buffer.
The deleted content from the past 9 times can be pasted using "1p to "9p.

You can check the deleted content with
"1p
u
and at this time u will increase the value of the numeric buffer it references internally.
Therefore, you can check the contents of the numeric buffer as follows
"1p
u
.
u
.
u
.
Unfortunately it works in vim and editors/2bsd-vi but for some reason doesn't work in nvi.
 
The trouble with vi[m] is, without a minimum of learning effort you simply cannot do shit with it (not even close the damn thing 😂), but under unix[like] it's unavoidable sooner or later you have to do some things with it. While insert, [Esc] back to normal (editing) mode, undo, ZZ save & quit, or :q! quit without saving, (you are allowed to use the arrow keys instead of h,j,k,l :cool:) are not really that much you need to learn, and is all you need to deal with in emergency situations, like comment a wrong line in your /etc/rc.conf in single user mode when your machine won't come up, there are tons of vi cheat sheets (even printed on coffee mugs) which is a mandatory part of the standard emergency kit of every unix[like]-root-but-not-vi-user. 🤓

However, when you took a bit training into vi[m], which cannot hurt anyway even if you don't want to master it, it may attract your attention that in fact its whole structure is very logical, few commands in fact, but combinable. That's where its power comes from. You have to learn a certain basic set of commands, and the way how to combine them first, before you can do anything at all with it. But once you got this, you see: vi[m] actually is a very powerful editor. It may not become your favorite, main production editor, though for many it is, but maybe this will give you bit of a taste:

learning_vi_and_vim.jpg

[from Robbins, Hannah & Lamb, Learning the vi and Vim Editors, O'Reilly]
 
I tend to use mm to "bookmark" the current line number in a slot called m.

Then from anywhere in the document I can use 'm to jump back to that bookmark.

Combined with d for delete, d'm will delete from the current line to your bookmarked line.

So all together:

Code:
mm
<navigate to where I want to delete to>
d'm

Done.
 
Back
Top