1

I can define a vimscript function, for example

function GoUp()
    while line(".") > 1 && (strlen(getline(".")) < col(".") || getline(".")[col(".") - 1] =~ '\s') 
        norm k
    endwhile
endfunction

and then assign a shortcut to it

nnoremap gU :call GoUp()<CR>

, so gU will go up until hit a non-space character.

However, such gU can't be repeated by pressing dot (.) as it is only movement, not a change.

Is there a way to walk around it? for example, set a flag in the function to "cheat" the Vim to take it as a change, or, make it possible to be repeated by n?

2
  • Stating the obvious: creating a mapping for gU shadows the very useful functionality to convert text to uppercase.
    – Friedrich
    Mar 14 at 8:22
  • @Friedrich indeed, though it's not to the question.
    – athos
    Mar 14 at 11:39

1 Answer 1

1

If I've understood your particular function correctly, the following command searches backwards for a non-space character in the same column as the cursor:

?\%.c\S

This would be repeatable with n.

10
  • just to be clear, from normal mode, shall i type : or / before I type ?\%.c\S, or shall i directly start with ?\%.c\S? i tried (in windows gVim) but hit error "E867: (NFA) Unknown operator '\%.'"
    – athos
    Mar 13 at 21:36
  • None of them, you have to type directly ? which search backward as opposed to / that search forward. Mar 13 at 22:54
  • 1
    Indeed the operator \%.c has been introduced during the 8.2 live. It is present at the end of the 8.2 live but not with 8.2.1 (I don't know yet when exactly it has been introduced). Mar 14 at 13:45
  • 2
    Looking at the GitHub repository of Vim I believe the feature has been introduced in version 8.2.3110 (on 5th of July 2021). Mar 14 at 13:57
  • 1
    @VivianDeSmedt thank you, i installed vim 9.1 and now it's there :)
    – athos
    Mar 15 at 8:38

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.