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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:12:15+00:00 2026-06-09T12:12:15+00:00

I am trying to do a comment remap in Vim with an inline if

  • 0

I am trying to do a comment remap in Vim with an inline if to check if it’s already commented or not. This is what I have already and of course it’s not working ha ha:

imap <c-c> <Esc>^:if getline(".")[col(".")-1] == '/' i<Delete><Delete> else i// endif

What I want to do is check the first character if it’s a / or not. If it’s a / then delete the first two characters on that line, if it’s not a / then add two // in front of the line.

What I had originally was this:

imap <c-c> <Esc>^i//

And that worked perfectly, but what I want is to be able to comment/uncomment at a whim.

  • 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-06-09T12:12:17+00:00Added an answer on June 9, 2026 at 12:12 pm

    I completely agree with @Peter Rincker’s answer warning against doing this in insert mode, and pointing you to fully-featured plugins.

    However, I couldn’t resist writing this function to do precisely what you ask for. I find it easier to deal with this kind of mapping with functions. As an added bonus, it returns you to insert mode in the same position on the line as you started (which has been shifted by inserting or deleting the characters).

    function! ToggleComment()
        let pos=getpos(".")
        let win=winsaveview()
        if getline(".") =~ '\s*\/\/'
            normal! ^2x
            let pos[2]-=1
        else 
            normal! ^i//
            let pos[2]+=3
        endif
        call winrestview(win)
        call setpos(".",pos)
        startinsert
    endfunction   
    
    inoremap <c-c> <Esc>:call ToggleComment()<CR>
    

    Notice the modifications to pos to ensure the cursor is returned to the correct column. The command startinsert is useful in this type of function to return to insert mode. It is always safer to use noremap for mappings, unless there is a very good reason not to.

    This seems to work well, but it is not very Vim-like, and you might find other plugins more flexible in the long run.

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

Sidebar

Related Questions

This is trying to insert null into Comment.BlogArticleID. The following GenericADOException appeared: could not
I am trying to get the comment count for a video entry but this
I'm trying to figure out how I can have a comment header automatically added
Please do NOT comment on bad practices used here. I am simply trying to
I'm trying to figure out why this works: >>> comments = Comment.objects.all() >>>[c.content_object for
I'm trying to have a comment input field that will show the submit button
Trying to create a comment application on my website. Data is not inserted properly
I'm trying to understand this comment from AES implementation code: /** * This program
I am working on a comment form for a website and trying to secure
I have been trying to implement a comment engine in my app (UItableView) but

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.