command to select multiple line & move it backward/forward

0 views
Skip to first unread message

mthread

unread,
Dec 10, 2008, 9:57:27 AM12/10/08
to vim_use
Hi,
I would like to know the command to select multiple lines & move
them either backward/forward. I need this functionality to make
indendation in my program.

sc

unread,
Dec 10, 2008, 10:38:58 AM12/10/08
to vim...@googlegroups.com

here's what i use:

" move text and rehighlight -- vim tip_id=224
vnoremap > ><CR>gv
vnoremap < <<CR>gv

to use it, visually select the lines and then use '<' or '>'
to move them one shiftwidth

sc


Tim Chase

unread,
Dec 10, 2008, 10:49:25 AM12/10/08
to vim...@googlegroups.com
> I would like to know the command to select multiple lines & move
> them either backward/forward. I need this functionality to make
> indendation in my program.

"backward/forward" are ambiguous terms -- do you mean "up/down in
the file" or "indent/dedent in the file"?

INDENTING/DEDENTING
====================
You can use the >/< commands to indent/dedent a line, range,
motion, or as an Ex target. You can also use control+[t/d] to
indent/exdent while in insert mode

:help >
:help :>
:help i_CTRL-T

Thus if you have a visually highlighted range (":help V"), you
can just use

>

to indent the lines. Or you can use

>ip

to indent the "inner paragraph" (using the "ip" text object,
":help text-objects"). Or, if you want to indent select lines:

:g/foo/>

will indent all lines matching the pattern "foo" (with an
annoying stream of messages that I usually suppress with a
well-placed "sil!" in the command).

Incidentally, indentation behavior is controlled by your
'shiftwidth' and 'expandtabs' settings.

MOVING LINES UP/DOWN
=====================
To move lines up/down in a file, you can copy (yank) and paste
them. Or you can use the Ex ":m"ove command:

:help :move

For example

:.,+3m'a

will move 4 lines (the current line and the 3 below it) to the
location you've marked with the "a" mark. Any line-reference
(":help :range") will do, so you can do things like

:-3,/foo/m1/bar

(take the range from "3 lines above the cursor, through the next
occurance of 'foo'" and move them to the line after the first
instance of 'bar' after line #1) Or even

:g/foo/m$

will find all lines containing "foo" and move them to the bottom
of the file.

A side-benefit of the ":m"ove method is that it doesn't tromp
your scratch yank/delete register. I frequently use

:m+

to move the current line below the next one without disturbing my
registers.

Hope this gives you plenty of ways to do whichever you need, as
well as showing you some of Vim's power :)

-tim

Reply all
Reply to author
Forward
0 new messages