Easier Long Lines Navigation in Vim

Simple mappings to improve navigation in markdown files and others.

jovica
vim drops
2 min readSep 2, 2020

--

"I was getting frustrated editing markdown files with long lines…"

Said one of my newsletter subscribers. So I gave him a tip.

The Problem

If you write something other than code in Vim — like documentation, or any other text, this one tip might be useful.

If you have a wrap option enabled, with a very long sentence, Vim will stretch your current sentence over the multiple rows.

But that will still be a single “physical” line. It might be tricky to move between those rows/lines.

Hitting arrow-down or j will move the cursor down one physical line.

The Solution

By hitting gj will move the cursor down one displayed line.

This will enable you to easily move around on a long sentence wrapped in multiple rows/lines.

The easiest way to go with this is to simply add the following mapping:

“ move vertically by visual line with j and k
nnoremap j gj
nnoremap k gk

Try it out.
It makes navigating much easier.

You'll notice that you can still jump the visual lines using the arrows.

I share tips like this via Mastering Vim Quickly Newsletter.

Useful links & references:

Footnotes from vim drops publication:

  • This post is an awesome contribution by Jovica Ilic !
  • If you have any doubts or tips about this article, we’d appreciate to know and to discuss it on the comments section.
  • Do you have any other vim tips? Would you like to publish that on this blog? Please send me and email at ‘vimdrops AT pm DOT me’.
  • As English is not my first language, I apologize for errors. Corrections are welcome at ‘vimdrops AT pm DOT me’.

--

--