Wikia

Vim Tips Wiki

Navigate to the next open fold

Talk0
1,613pages on
this wiki
Revision as of 06:12, July 13, 2012 by JohnBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Tip 1194 Printable Monobook Previous Next

created 2006 · complexity basic · author Ethan Mallove · version 6.0


I think it makes more sense for ]z and [z to navigate to the next/previous open fold, so I added the following to my vimrc:

function! GoToOpenFold(direction)
  let start = line('.')
  if (a:direction == "next")
    while (foldclosed(start) != -1)
      let start = start + 1
    endwhile
  else
    while (foldclosed(start) != -1)
      let start = start - 1
    endwhile
  endif
  call cursor(start, 0)
endfunction
nmap ]z :cal GoToOpenFold("next")
nmap [z :cal GoToOpenFold("prev")

CommentsEdit

Add the normal command "zj/zk" if you'd prefer to have the mapping jump out of the current open fold and into the next open fold.

" set ]z and [z go to find open folds
function! GoToOpenFold(direction)
  if (a:direction == "next")
    normal zj
    let start = line('.')
    while foldclosed(start) != -1
      let start = start + 1
    endwhile
  else
    normal zk
    let start = line('.')
    while foldclosed(start) != -1
      let start = start - 1
    endwhile
  endif
  call cursor(start, 0)
endfunction

Around Wikia's network

Random Wiki