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 7043667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:19:04+00:00 2026-05-28T02:19:04+00:00

I want to map \c to comment the current line in vim (add ‘//

  • 0

I want to map \c to comment the current line in vim (add ‘// ‘ to before the first non-blank to the line) and \d to delete the ‘// ‘ at the beginning of line(uncomment).

I’ve added these two lines in my vimrc:

imap \c <Esc>:s/^/\/\/ <CR>j$a

imap \d <Esc>:s/^\/\/ /<CR>j$a

but when I call this in this line:

    for (int i = 0; i < n; i++)

then it is converted to:

//    for (int i = 0; i < n; i++)

but I want to convert it to:

    // for (int i = 0; i < n; i++)

one of the reasons I want this is that when I want vim to indent this line, it will add an extra tab at the beginning of the line and convert it to:

    //    for (int i = 0; i < n; i++)

what should I do? I thought that ^ refers to first non-blank character of line but it wasn’t true.

P.S: I’ve found these two commands, but I don’t know what are they doing. could you please explain them to me (because I need to remember them and they’re easier to remember when I know what are they). if any part of them is unnecessary please tell me:

imap \c <ESC>:s,^\(\s*\)[^/ \t]\@=,\1// ,e<CR>j$a

imap \d <ESC>:s,^\(\s*\)// \s\@!,\1,e<CR>j$a
  • 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-28T02:19:05+00:00Added an answer on May 28, 2026 at 2:19 am

    About those commands you found:

    imap \c :s,^(\s*)[^# \t]\@=,\1// ,ej$a

    Let’s start with the pattern match, comparing it with your :s/^/\/\/ <CR>j$a.

    • :s, is exactly equivalent to :s/ – you can choose a nonstandard seperator where it makes the expression more readable, so here using , instead of / means less escaping, and no confusion between the comment // and the seperator
    • ^(\s*) matches the beginning of the line (same as yours) and any leading whitespace, capturing the whitespace so we can reuse it later
    • [^# \t]\@= just requires some character other than space, tab or # before the line end, but doesn’t capture it (ie, that character won’t be replaced)
    • ,\1// , replaces the matched string with the leading whitespace (\1 is whatever was captured by the (\s*) group) followed by //

    For example:

    ^    void foo();
    

    should match like so: ^(\s*)[^# \t]\@= -> ^( )(v@=)void foo();, ie, the first group matches the leading whitespace, and the next expression matches but doesn’t consume the v from void.
    Then, \1// expands to:

    ^    // 
    

    and the final substitution result is:

    ^    // void foo();`
    

    (I’ve marked the start of line with ^ since the indentation is ambiguous otherwise)

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

Sidebar

Related Questions

I want to map a INI file as a python object. So if the
I want to map DbConnection to an un-opened SqlConnection using Ninject . This is
I want to map a Tag entity using declarative method with SQLAlchemy. A tag
My User table I want to map to aspnet_Users: <class name=User table=`User`> <id name=ID
I have a situation where I want to map a pair of objects to
I have one very general object that I want to map to a destination
For instance, if I want to map property Title I use: > Map(x =>
I have this object graph, I want to map: abstract Account (username, password, ...)
I have a database field called abCode , that I want to map to
I haev an object with a DateTime property. I want to map the time

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.