So I followed the instructions here: http://vimcasts.org/episodes/bubbling-text/
The idea is that I enter visual mode and select a few lines of text. Then I hit CTRL-k or CTRL-j to move the selected text up or down in my file. The mappings in my .vimrc file are as follows:
nmap <C-k> ddkP
nmap <C-j> ddp
vmap <C-k> xkP`[V`]
vmap <C-j> xp`[V`]
The first two mappings are for normal mode (nmap) and they work as expected. For going down, the dd command deletes a line and the p command moves the cursor down and inserts the line. For going up the dd command deletes the line, the k command moves the cursor up a line, and then the P command inserts the line above.
In visual mode, however, it doesn’t work. Take the case of moving several lines of selected text down. Once the text is selected the x command deletes it. But the p command inserts the text exactly where the cursor is. So if I have a file with the following contents:
one
two
three
and the cursor is on the “o” of one. I hit vj$ to select the first two lines. Then I hit x to delete them. Then I hit p to insert them below. The result looks like this:
tone
two
hree
Right before I hit p, there is a single line in the file and the cursor is sitting on the “t” in “three”. So the behavior makes sense to me. What I don’t understand is why the expert VimCast tutorial is expecting me to see different behavior. Also, I would like to get the behavior I would like.
Those mappings work perfectly.
If we take the
<C-j>visual mode mapping:xdeletes the visual selection which puts the cursor on the line directly below the the delete lines.pputs the deleted lines under the current line, that’s the intended effect.`[moves the cursor to the first character of the changed text, here it’s the first character of the lines that you moved.Vstarts visual-line selection`]extends the selection toward the last character of the moved text, ready for further moving.From your description, I think that your problem is twofold:
You seem to be using the sequence of commands on the right hand of the mapping instead of the mapping itself.
You are using
v(visual mode, lowercasev, character-wise) instead ofV(visual-line mode, capitalv, line-wise). The purpose of these mappings is to “bubble” lines, not words so, if you really want the desired effect, you must use the right visual mode.The correct sequence of commands is:
or, if you insist on not using Drew Neil’s mapping: