Search code examples
vimfold

Calling hjkl keys from vim's command line


Whenever I am browsing folded code in vim and press the l key I want it to open that fold recursively. For that I did the following: nmap l lzO. Which worked ok, apart from the fact of getting a E490: No fold found whenever I would press l not in a fold. I used that an excuse to learn about Vimscript and write a function for that and avoid the error.

Now, I am missing the part of how can I call a vim command like l or lzO from inside a function?

function! OpenFoldOrNot()
    if foldclosed(line(".")) == -1
        echo "just l"
        l # TODO
    else
        echo "open fold"
        lzO # TODO
    endif
endfunction

nmap l :call OpenFoldOrNot()<CR>

Solution

  • Try the normal command. For this case you will need normal! to avoid recursive execution of your function.