3

I want to remap a key, but I want the mapping to be active only when I'm in the last line of my file. Is there a simple way to do this? I would also like to do a similar thing when I'm in the first line, but that is not essential to me at the moment.

My specific use case is that I want to remap j to <C-E> when I'm in the final line, but I also want to know if this is possible for curiosity's sake.

2
  • 3
    you can make use of expression mappings for this Aug 16, 2022 at 7:44
  • @ChristianBrabandt you mean as in ref ? I didn't know this was a possibility but it looks promising. Can I remap a key to itself then? Do you think you can sketch a function that does something like what I'm looking for? I'm not super familiar with vimscript, but if there is an api to get the current line this should be a simple if statement right?
    – Ignacio
    Aug 16, 2022 at 7:59

1 Answer 1

5

Expression mappings allow you to map to a dynamic rhs:

Try this:

:nnoremap <expr> j line('.') == line('$') ? '<C-E>' : 'j'

This makes use of a ternary if-then-else expression to map to <C-E> on the last line or j otherwise.

Your Answer

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

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