Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4077598
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:34:13+00:00 2026-05-20T17:34:13+00:00

Recently I create a mapping to toggle highlight search. That’s simple but quite useful,

  • 0

Recently I create a mapping to toggle highlight search. That’s simple but quite useful, something like nmap ,m :set hlsearch!. The only problem I’ve been facing is that sometimes I just get lost after pressing ,m.

“Did it work?”, “Is it now on or off?”… Common question from the panic of not receiving feedback 🙂

So I thought that echoing a “turned on” or “off” would make me calmer. The big question here now arrived: is it possible to include a little script inside the mapping? I know I could create a function, but that’s not my intention for this simple script:

if (&hlsearch)
    echo "Search Highlight On"
else
    echo "Search Highlight Off"
endif

I guess that I need to “escape” the newlines like in a preprocessor directive, maybe:

nmap ,m :set hlsearch!              \
    if (&hlsearch)                  \
        echo "Search Highlight On"  \
    else                            \
        echo "Search Highlight Off" \
    endif

But probably that’s not the proper way, doesn’t work and I don’t even know if it is possible.

Another thing I notice is that typing things like :if (1) echo "works" doesn’t work either, even though both if and echo are “colon” commands. It gives an error with echo. So do I need to separate each command in some way? Preceding echo with another colon didn’t solve the problem.

Thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:34:13+00:00Added an answer on May 20, 2026 at 5:34 pm

    You can do it, but I’d recommend against it. A mapping links one or two key presses (,m) in your case to a sequence of key presses. Therefore, to do what you want to do, you just have to type in the commands as you would if you were doing it interactively:

    nmap <silent> ,m :set hlsearch!<CR>:if (&hlsearch)<CR>echo "Search Highlight On"<CR>else<CR>echo "Search Highlight Off"<CR>endif<CR>
    

    Each <CR> is the equivalent of pressing ENTER. The <silent> stops it from echoing the whole contents of the mapping to the screen, so you only see “Search Highlight On/Off”.

    The other way you can join commands (in some cases) is with | so for example:

    :if (1) | echo "works" | endif
    

    See :help :bar for more information on this.

    Do it with a function! It’ll make things much more maintainable in the long run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.