8

Screenshot
(Click image to enlarge)

Look at the image above, it is one line that declares a huge list. Now suppose my cursor is on the first element's 'i' and I press 'j' then the cursor directly goes to the line new_s='' but what I want is the cursor go to the 's' of the word 'she' below 'i'

Is there any way to do this?

5 Answers 5

12

j moves the cursor down one physical line. You can use gj to move down one displayed line.

8

The existing answers are spot on.

I wanted to add that j/k and gj/gk are strictly equivalent when you are working on non-wrapped text. Thus, the following mappings are as harmless (if you don't :set wrap) as they are useful (if you :set wrap):

nnoremap j gj
nnoremap k gk
2
  • Vim rookie here. I'll have to look into what nnoremap means :)
    – Programming Noob
    Nov 2, 2012 at 8:56
  • It can be called "non-recursive mapping", see this page for an explanation.
    – romainl
    Nov 2, 2012 at 9:58
2

I like the script from here: http://vim.wikia.com/wiki/Move_cursor_by_display_lines_when_wrapping " mapping to make movements operate on 1 screen line in wrap mode

function! ScreenMovement(movement)
  if &wrap && b:gmove == 'yes'
    return "g" . a:movement
  else
    return a:movement
  endif
endfunction
onoremap j ScreenMovement("j")
onoremap k ScreenMovement("k")
onoremap 0 ScreenMovement("0")
onoremap ^ ScreenMovement("^")
onoremap $ ScreenMovement("$")
nnoremap j ScreenMovement("j")
nnoremap k ScreenMovement("k")
nnoremap 0 ScreenMovement("0")
nnoremap ^ ScreenMovement("^")
nnoremap $ ScreenMovement("$")
vnoremap j ScreenMovement("j")
vnoremap k ScreenMovement("k")
vnoremap 0 ScreenMovement("0")
vnoremap ^ ScreenMovement("^")
vnoremap $ ScreenMovement("$")
vnoremap j ScreenMovement("j")
" toggle showbreak
function! TYShowBreak()
  if &showbreak == ''
    set showbreak=>
  else
    set showbreak=
  endif
endfunction
let b:gmove = "yes"
function! TYToggleBreakMove()
  if exists("b:gmove") && b:gmove == "yes"
    let b:gmove = "no"
  else
    let b:gmove = "yes"
  endif
endfunction
nmap ,b TYShowBreak()
nmap ,bb TYToggleBreakMove()

You can then use ,b to display if the line wraps (above example uses '>' character). Then ,bb turns off screen movement if you have wrap set

Additionally I have wrapping set to a binding in my VIMRC as well:

"--- wrap toggle with f2 key---"
function! ToggleWrap()
set wrap!|set wrap?
endfunction

"F2 toggles wrap
nmap <f2> :call ToggleWrap()<CR>
vmap <F2> <C-C>:call ToggleWrap()<CR>
imap <F2> <C-O>:call ToggleWrap()<CR>i
2

My behavior with Vim was a bit different, perhaps because of the options it is compiled with. On this version, line navigation with j and k would navigate one physical line by default, i.e. what typing gj and gk would do. Unexplained, and possibly for some other reason, it appears to only to do so on Unix files, DOS files would still navigate the full line, i.e. the multiple-physical, word-wrapped ones.

To get j and k to navigate only physical lines for all files, I had to set all these in my .vimrc to get the expectant behavior:

nnoremap j gj
nnoremap k gk
vnoremap j gj
vnoremap k gk
1

If you type gj the cursor will move one line down, gk brings you one line up. I assume that you are in a normal console window?

Cheers

You must log in to answer this question.