how to block select, move, copy, paste in vi?

4

1

I am trying to migrate from Notepad++ to vi. In N++, I constantly rely on block edit (or "column mode"), as it is a great increase in productivity: I can select a rectangular area of text by pressing ALT before marking with the mouse, and copy, move, paste the rectangular area anywhere. Also, it is possible to drag the cursor vertically so that I can type simulatenously on several subsequent lines at the same time.

Is there any similar functionality for vi? If yes, how can I do that?

user50105

Posted 2011-11-30T03:51:42.917

Reputation:

possible duplicate of How to select a rectangular range in VIM?

– Paul – 2011-11-30T04:03:24.340

Note that the visual block feature works the same for inserts too - create a vertical visual block, hit I, type what you want, hit Escape, and the typed entry will appear in the same place on each line. – Paul – 2011-11-30T04:08:52.740

1Hi Paul, not really a duplicate. I admit a I am kind of asking two question at the same time, but my question was also about multi-line edit. – None – 2011-11-30T04:17:39.290

The visual block feature of vim is for operations on a block, including multi-line edit - this is kind of implied with vim, as that is the way it works for all operations. – Paul – 2011-11-30T04:19:32.957

2Yes. It's just that this is not mentioned in that other thread which you linked to, therefore I wouldn't say this thread is a duplicate. I pre-emptively mentioned this before this thread gets closed down. – None – 2011-11-30T04:23:20.577

1Yes, but we all need to make an effort to reduce the number of duplicates or near duplicates from the site. To zero where possible. Had you searched first, you would have found the answer to the block question, then you could have just asked just the multiline edit question (but you would have probably figured it out in exploration). You could just edit your question to make it less dupey. – Paul – 2011-11-30T04:27:59.197

Since you seem to be starting out with vi, you might want to take a look at some very useful tips & tricks here:- http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

– Tog – 2011-11-30T08:19:58.200

@gojira "this is not mentioned in that other thread" - These are not threads, they are questions and answers. Please edit your question down to the one that isn't the duplicate. Afterwards, perhaps go up-vote the (other) question and answer(s) that are useful to you. – Ƭᴇcʜιᴇ007 – 2011-12-01T02:22:16.583

1techie007: the answer in that other thread just sucks compared to the one Jacob gave below. I don't see why I should up-vote another answer which is much worse than the one given here. – None – 2011-12-01T02:34:31.503

1@Paul - No we don't. What we need is to try to improve the quality of information available on this site. Duplicates are unrelated to quality. Often a better answer gets closed because there was already some (crappy) answer that answered the similar question. – Rook – 2011-12-01T17:28:41.323

@Idigas this is not the place for discussions, take it to meta – Paul – 2011-12-01T20:59:09.073

Answers

5

Really, vi? You sure you didn't mean, vim or gVim?

I'm gonna take a shot here and assume you ment the latter. gVim has got several ways of selecting text, the most similar to block edit in Notepad++, is conveniently also called block edit in gVim too. You start it up with Ctrlv and then select.

If you wish to "go over the blanks" as well, put this down

:set virtualedit=block

then repeat the part with Ctrlv. x cuts the selection, y yanks it (another word for copy), p pastes it. All of these have some variations on the theme. Vim really has a plethora of text manipulating features - I dare say, I don't know of any other editor that comes close to it in that respect.

Ups, just saw the other question in there as well. Typing in several lines, that is. Goes like this (one method): select the vertical column with Ctrlv, press I, type something down, press escape ... it should copy itself to all other lines as well.

Note that if you're using GVim on Windows, Ctrlv will attempt to put the system clipboard buffer by default. Ctrlq is an alternative keybinding to perform the same action on Windows.

Rook

Posted 2011-11-30T03:51:42.917

Reputation: 21 622

Well I meant vi obviously including derivatives and modern versions; I thought that is understood when just saying "vi". Thank you for your answer. It works great! But one follow-up question: if I select a block of text as described, and then move the cursor to the beginning of a line and press "p", the block is inserted not at the very first position of the line but starting from the second position. Is this the default behaviour? Or what do I need to do to insert the block at the very beginning of the line? – None – 2011-11-30T04:15:46.410

@gojira: You wouldn't believe how many people say "vi" but mean the modern "vim" and expect to have colors, mouse support and everything in vi. – user1686 – 2011-11-30T10:16:15.437

@gojira - Try "P". Yes, that is the ok behaviour. – Rook – 2011-11-30T11:31:55.423

@grawity, mouse support, PAH. I use my tiny little netbook with a tiny little trackpad. I'm glad I don't use a mouse. – Rob – 2011-11-30T20:52:13.273

1@Rob: Okay, that's just ... I'm sorry, but how the hell is your despisal of computer mice relevant to this discussion? – user1686 – 2011-12-01T14:14:14.420

Honestly, I have no idea. I was very tired yesterday. – Rob – 2011-12-01T16:36:19.243