'Moving the cursor through long soft-wrapped lines in Vim

I'm sorry if my question isn't very clear. I'm not sure how to phrase it.

I'd like to use VIM to write papers for some classes I'm in. The problem I'm having is with the formatting of lines in the editor. If I don't explicitly break the end of a line with the enter key, when I try to move the cursor through the text, it skips multiple lines if I have a sentence that spans more than one line. Is there any way to make it so that the cursor will be able to move through the text akin to the way it does in most word processors?



Solution 1:[1]

Have you tried the following in vim command line:

:set nowrap

Solution 2:[2]

The problem with the often used

noremap j gj  
noremap k gk 

option is, that it breaks the <vcount> functionality, if you have lines in your text, which span across multiple lines.
Example: you want 10k (go UP 10 lines), because you use relative numbers in the sidebar, but theres a multiline with 4 lines height. Therefore you end up effectively at 6 lines (6k) above your desired line, which you read from your relative numbers. You'd have to calculate manually! Annoying... Especially if you have multiple multiline between your current position and your desired position - not Vim-istic!

I like my <vcount> function together with my :relativenumber, which is why I wrote the following functions & mapping to solve all problems related to this.
These functions let you use commands like 10j or 10k as expected, despite the presence of multilines with all the advantages of using gj and gk as your default movement mappings:

Edit: I just found the following on reddit, which is so much better then my own solution. This is shortest possible version:

nnoremap <expr> j v:count ? 'j' : 'gj'
nnoremap <expr> k v:count ? 'k' : 'gk'

(If you use noremap instead of nnoremap, then this works in both visual and normal modes)

"Longer" version for better understanding and completeness:

nnoremap <expr> k (v:count == 0 ? 'gk' : 'k')
nnoremap <expr> j (v:count == 0 ? 'gj' : 'j')

source: http://www.reddit.com/r/vim/comments/2k4cbr/problem_with_gj_and_gk/


My old solution:

nnoremap <silent> j :<C-U>call Down(v:count)<CR>
vnoremap <silent> j gj

nnoremap <silent> k :<C-U>call Up(v:count)<CR>
vnoremap <silent> k gk

function! Down(vcount)
  if a:vcount == 0
    exe "normal! gj"
  else
    exe "normal! ". a:vcount ."j"
  endif
endfunction

function! Up(vcount)
  if a:vcount == 0
    exe "normal! gk"
  else
    exe "normal! ". a:vcount ."k"
  endif
endfunction

Solution 3:[3]

That's because the default j and k motions move across physical lines, not the visible, soft-wrapped screen lines (when you have :set wrap). You can use the gj and gk commands for that.

If you want to default to that behavior, you can remap the default keys by putting this into your ~/.vimrc:

noremap j gj
noremap k gk

Solution 4:[4]

I have found another version of this solution that does more than moving through physical or virtual lines, it also adds jumps bigger than 5 lines to the jump list, allowing us to use Ctrl-o and Ctrl-i.

" source: https://www.vi-improved.org/vim-tips/
nnoremap <expr> j v:count ? (v:count > 5 ? "m'" . v:count : '') . 'j' : 'gj'
nnoremap <expr> k v:count ? (v:count > 5 ? "m'" . v:count : '') . 'k' : 'gk'

It uses a nested ternary operator to add the jump to the jump list

Solution 5:[5]

Solution that I found on internet that works with up and down arrow:

imap <silent> <Down> <C-o>gj
imap <silent> <Up> <C-o>gk
nmap <silent> <Down> gj
nmap <silent> <Up> gk

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nikpel7
Solution 2 Stabledog
Solution 3 Ingo Karkat
Solution 4 SergioAraujo
Solution 5 Trismegistos