Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

Bubbling text


Run time:

TextMate has a few built in commands for moving selected text around a document. The end end result of using these commands can be achieved by cutting and pasting, but the visual feedback they provide is quite helpful. In this episode, I will show how to replicate these commands in Vim.


In TextMate, the commands for moving selected text can be found under the “Text > Move Selection” menu. I find “move selected line up” to be an acurate but dull description. Personally, I prefer to call this feature text bubbling.

TextMate's Move selection commands

In the video, I describe two different ways of reproducing the text bubbling feature.

Text bubble with Vim primitives

By putting the following in your .vimrc file, you can create maps for bubbling text up and down:

" Bubble single lines
nmap <C-Up> ddkP
nmap <C-Down> ddp
" Bubble multiple lines
vmap <C-Up> xkP`[V`]
vmap <C-Down> xp`[V`]

Edge cases

These mappings have a flaw which is only apparent when you use them at the boundaries of a document. For example, if the cursor is placed on the last line of a file and you try to move it up by one line, instead it moves up by two lines. Remember that these mappings are blind, they just replay a series of keystrokes, without any understanding of context.

Text bubble with unimpaired.vim

A better solution is available if you install Tim Pope’s unimpaired plugin. This provides the exchange commands, which are mapped to [e and ]e respectively:

[e  -  Exchange the current line with [count] lines above it.
]e  -  Exchange the current line with [count] lines below it.

These commands can be prepended with a count, and they both also work in Visual mode. Whatsmore, they handle edgecases more gracefully, by raising an error message: Invalid address.

If you have the unimpaired plugin installed, then you can use the “square bracket e” commands to bubble text up and down. But if you are used to the TextMate key commands, you could always recreate those mappings and hook into the commands from unimpaired. This excerpt of Vimscript should do it:

" Bubble single lines
nmap <C-Up> [e
nmap <C-Down> ]e
" Bubble multiple lines
vmap <C-Up> [egv
vmap <C-Down> ]egv

Visually select the last edited text

You may already know that gv can be used to start visual mode with the same selection as last time visual mode was used. If you have cut and pasted a visual selection, then this doesn’t work the way that you would expect. Fortunately, you can visually select the text from the last edit by jumping to the first and last characters with the motions \[and`]`.

I keep the following mapping in my .vimrc:

" Visually select the text that was last edited/pasted
nmap gV `[v`]

So with gv I can reselect the last visual selection, and with gV I can visually select the text that was most recently edited or pasted.

Further reading


Level-up your Vim


Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify


Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..