0

Background:

  • Vim :help states that a section begins after a form-feed (^L) in the first column [*].
  • In normal mode ]] or [[ can be used to navigate to the next or previous section (respectively).
  • However, it also stops at the next/previous { in the first column.

[*] I am ignoring that a section also starts at some troff macros

My problem:

  • I want to navigate between sections only, i.e., I want motions like ]] and [[ that only stop at the beginning of a section.

Questions:

  1. Is there already a built-in motion that solves my problem?
  2. Sure, I could just map some key combination to /^<C-L><CR>), but is there a better way? In particular, I don't like the idea of it messing with my search history.

PS:

  • To be honest, my real problem is that sometimes it is easier for me (and gcc!) to work with one HUGE file instead of splitting into several translation units. So I divide it into sections and I want to jump fast between sections without stopping at each function block. If you have a better approach, I would appreciate a suggestion as a comment.
  • Even though I am mainly editing C files, I guess I wouldn't care (that much) if a solution overrides the default behavior of [[ and ]]. Not overriding would be a plus, though.

1 Answer 1

0

Yes, one problem with [[ and ]] is that they kind of try to do several things at the same time. Another problem is that redefining what a section is is not exactly simple.

Now, :help [[ hints at making your own mappings and there are a few default ftplugins that map [[ and ]] to work in a specific way. So you should probably consider something like this:

" in after/ftplugin/c.vim
nnoremap <buffer> [[ <Cmd>call search("^\<C-l>", 'b')<CR>
nnoremap <buffer> ]] <Cmd>call search("^\<C-l>")<CR>

Note the use of :help search(), which doesn't pollute register /.

If you want to use them outside of C, remove the <buffer> attribute and put them in your vimrc:

" in .vimrc
nnoremap [[ <Cmd>call search("^\<C-l>", 'b')<CR>
nnoremap ]] <Cmd>call search("^\<C-l>")<CR>
1
  • Worked perfectly without polluting register /. I did have to replace <Cmd> with : but I imagine <Cmd> is a new notation not available on 8.2 (sorry I have no reputation to upvote you) Commented Apr 11, 2023 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.