7

I'm used to set marks with m<char> where <char> is any character I choose and then reaching the marks with ]` or [`.

I'm wondering if it is possible to use a command to set a mark with an incremental index instead of a character, avoiding to override marks previously set.

2 Answers 2

6

There is nothing built in to vim, but you can script something like this:

fu! s:IncrementalMark() abort
    let b:index_mark=get(b:, 'index_mark', -1)+1
    " configure, which marks to use
    let marks='abcdefghijklmnopqrstuvwxyz'
    return 'm'.marks[b:index_mark%strlen(marks)]
endfu
nnoremap <silent><expr> <leader>m <sid>IncrementalMark()

Use the marks variable, to configure which marks are actually allowed to be overwritten by this function. If you e.g. want to set the mark a yourself, then remove the a from this variable.

Note, that since marks are buffer-local, so will this function start with the first mark again, once you switch buffers, so you can have the same (lowercase) mark in several buffers.

1
  • 1
    Great answer! I remapped it to mm, and using a recursive mapping it works as expected with plugins like Vim-Signature. nmap <expr> mm <SID>#IncrementalMark() Feb 10, 2016 at 16:47
3

I'm not sure I understood what you wanted. But if you want to cycle through all the available local marks from a to z, then the following mapping should allow you to do so:

nnoremap <silent> <leader>m
            \ :<c-u>if !exists('marklist') \|\| marklist == [] <bar>
            \     let marklist = map(range(98, 122), "nr2char(v:val)") <bar>
            \     execute 'normal! ma' <bar>
            \ else <bar>
            \     execute 'normal! m' . marklist[0] <bar>
            \     let marklist = marklist[1:] <bar>
            \ endif<cr>

If you hit <leader>m, the first time it should set the mark a where the cursor is, the second time the mark b, etc.

0

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.