I want a command mapping to detect whether the current line is blank, and perform a different action for each case it detects: true of false. How do I do this?

1 Answer 1


It is not entirely clear when you say to "trigger some mapping"... Do you want to create a mapping with nnoremap if current line is empty? I guess not. Or you want to have a mapping that does different things depending on whether current line is empty?

If so then there are many possibilities, for example:

func! DoSmth()
    if getline('.') =~ '^\s*$'
        echo 'Do something if empty line!'
        echo 'Do something if NOT empty line!'

nnoremap <space>a :call DoSmth()<CR>
  • Yes I meant the second one you mentioned. Apr 23, 2021 at 6:27
  • May I also ask you that how to detect the previous line exists? Apr 23, 2021 at 6:52
  • 1
    @job_start it would be better to create another question for this. But in general, check :h function-list in vim to get an idea of what API vim exposes to vimscript.
    – Maxim Kim
    Apr 23, 2021 at 8:07

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.