I'd like to pull out a variable something like this to save repeating the same thing

let s:twoLinesStartingWithWords = '^\w\+.*\n\w\+.*'
"next/previous definition
nnoremap <Leader>n /\=s:twoLinesStartingWithWords<cr>
nnoremap <Leader>N ?\=s:twoLinesStartingWithWords<cr>

Do I need to use the search register or is there a simpler way?

  • @user786441 I don't think you should have deleted your answer. It was a good description of a useful technique! It's good to have alternative solutions for future readers who may have slightly different requirements.
    – Rich
    Jan 19, 2018 at 15:51

1 Answer 1


You can include variables in mappings by building the map command as a string and executing it with the :execute command:

let s:twoLinesStartingWithWords = '^\w\+.*\n\w\+.*'

execute 'nnoremap <leader>n /' . s:twoLinesStartingWithWords . '<CR>'
execute 'nnoremap <leader>N ?' . s:twoLinesStartingWithWords . '<CR>'
  • awesome, exactly what I was after :)
    – JonnyRaa
    Jan 24, 2018 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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