4

How would one vertically rotate a visual block of text?

For example given this:

h
e
l
l
o

Select those characters in visual block and rotate them once:

o
h
e
l
l

As you see, every character is moved down one line, and the last character (o) is wrapped around and put on top

Is this possible?

7

You can do it like this:

  • select the lines you want to rotate (with V or <C-v>)
  • type :
  • type <C-w> to get rid of the '<,'> that appeared after the :
  • type '> move '<-1, or the short version '>m'<-
  • press <Enter>

Explanation

The move command accepts a range to move and an address to where the content should be moved (see :help move).

In this case '> is the range. It translates to "the last line of the visual selection". On the other hand, '<- is the address where we want the selected range to be moved to. It means "the line before (-) the first visually selected line ('<)".

This can be difficult to type and you have to get rid of the '<,'> that appears when you type : in Visual mode.

Solution 2

It is much easier to just select all the lines but the last and type dp. This will delete the selected lines (hell) and put them after the o.

  • 1
    Solution 2 only works in Visual Line mode, though. – Doorknob Mar 27 '15 at 11:40
  • This does not work as expected (no, not even solution 1). I believe you missed that this is to be executed on a visual block, not in visual line mode. – Wildcard May 3 '16 at 3:24
5

Here's a solution that works even if there are surrounding characters:

kdjP`]lx`<P

Which can be mapped to a key like so:

vnoremap <C-r> kdjP`]lx`<P

Explanation (here, bolded letters symbolize the cursor / selected text):

Initial text:

.......
...h...
...e...
...l...
...l...
...o...
.......

kdjP: cut all but the last letter and paste it one row down:

.......
......
...h...
...e...
...l...
...lo...
.......

`]lx: go to the end of the text that was just pasted (`]), move to the character next to it (l), and cut it (x)1:

.......
......
...h...
...e...
...l...
...l...
.......

`<P: go back to the beginning of the original visual selection (`<), and paste the last character you just deleted (P):

.......
...o...
...h...
...e...
...l...
...l...
.......

1: the dot to the right of the l is bolded, although it's hard to see.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy