Frage

let's say I have following block of code:

int main(void)
{
    return 0;
}

int abc(void)
{
    printf("First line\n");
    printf("Druhy radek\n");
    printf("tuna fish\n");
}

...and I want to move content of function abc to function main. I tried to use visual selection, but when I select those three lines and paste them, this happens:

int main(void)
{
    return 0;
    printf("First line\n"); }
    printf("Druhy radek\n");
    printf("tuna fish\n");  int justAregularFunction(void)
{

Instead of this, I want to create a space in main function for the deleted block of text and then paste it. I tried to use ]p instead of regular p for pasting text, but it didn't work.

Solution would be to use d2j to delete the three lines and then I am able to paste the block like I wanted. But it is not very helpful when you have code with a lot of lines. Also commands :set paste and :set nopaste didn't help. I'm learning VIM and have no experience with vi, so I am probably missing something. Vim tutor was very helpful, but it explained only moving single lines. Also I found a lot of material about moving lines in vim here at stack overflow, but anything helped. I would like to have something like >> << for moving text, but vertically.

Second thing I can't figure out is how to delete text and space in visual select. When I select text and try to delete it, it leaves empty space. Button D deletes only text without space, also X does the same(and other variants with shift and control). Again, using d number j would help, but I want to achieve it in visual select.

I hope it's not totally dumb question, but I really didn't find a way to do it. Thanks for your help.

War es hilfreich?

Lösung

It looks like you've used a visual blockwise selection; when pasting that, no new lines are created, but instead the contents are inserted (at the cursor position) inside the existing lines. (That also explains your second issue, the remaining empty lines.)

If you want to move lines, be sure to start linewise selection with Shift + V; you'll see a -- VISUAL LINE -- indication in the status line at the bottom. In your example, that would be (with the cursor anywhere on the printf("First line... part) Vjjd. Of course, with such small amount of lines, it's easier to skip visual selection and use a count: 3dd.

And beyond

Once you have the basics right (did you try vimtutor, also look at the excellent built-in :help, or the plethora of Vim tutorials on the web, especially http://vimcasts.org/), there are several ways to improve on this workflow: text objects, plugins that can select function bodys or indented lines, etc.

Andere Tipps

With the cursor on printf("First line\n");, I would select the block of code with v{motion} then :move it just under the return 0; line.

vjj
:m?ret<CR>

Without visual selection:

d2j
?ret<CR>
p

Ex command-only:

:,+2m?ret<CR>

or

With the cursor on return 0;:

:9,10m.

Well, there are a million ways…

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top