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

6,602

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.

Share:
6,602

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    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?

    • Paul
      Paul over 12 years
    • Paul
      Paul over 12 years
      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.
    • Admin
      Admin over 12 years
      Hi 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.
    • Paul
      Paul over 12 years
      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.
    • Admin
      Admin over 12 years
      Yes. 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.
    • Paul
      Paul over 12 years
      Yes, 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.
    • Tog
      Tog over 12 years
      Since you seem to be starting out with vi, you might want to take a look at some very useful tips & tricks here:- viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html
    • Ƭᴇcʜιᴇ007
      Ƭᴇcʜιᴇ007 over 12 years
      @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.
    • Admin
      Admin over 12 years
      techie007: 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.
    • Rook
      Rook over 12 years
      @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.
    • Paul
      Paul over 12 years
      @Idigas this is not the place for discussions, take it to meta
  • Abdelhamid EL MAROURI
    Abdelhamid EL MAROURI over 12 years
    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?
  • user1686
    user1686 over 12 years
    @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.
  • Rook
    Rook over 12 years
    @gojira - Try "P". Yes, that is the ok behaviour.
  • Rob
    Rob over 12 years
    @grawity, mouse support, PAH. I use my tiny little netbook with a tiny little trackpad. I'm glad I don't use a mouse.
  • user1686
    user1686 over 12 years
    @Rob: Okay, that's just ... I'm sorry, but how the hell is your despisal of computer mice relevant to this discussion?