"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.
I share tips like this via Mastering Vim Quickly Newsletter.
Useful links & references:
- Vim Documentation (
:help
) - “Mastering Vim Quickly” book (by Jovica Ilic).
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’.