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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:04:45+00:00 2026-05-31T09:04:45+00:00

So, lets say I have a chunk of code int i = 0; //

  • 0

So, lets say I have a chunk of code

int i = 0; // Do some junk here<cursor is here>

if(i == 0){
  blahblahblahblah;
}

blahblahblah;

Is it possible to tell vim that when I hit enter, I want it do result in the following:

int i = 0; // Do some junk here
           // <cursor is here>

if(i == 0){
  blahblahblahblah;
}

blahblahblah;

I know it will do this for a comment who is on a line all on its own, but I can’t figure out this way.

  • 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-31T09:04:46+00:00Added an answer on May 31, 2026 at 9:04 am

    I don’t know whether there is a plugin for this (but there may be one), but the following mapping should do the trick for adding a line by pressing enter (there are much more ways to add the line though):

    " Function that adds new line starting with comment symbol if line does not 
    " start with comment, but contains it.
    function! s:NewLine(comsymb)
        let line=getline('.')
        " Check whether we are in comment. Assumes syntax highlighting is working 
        " correctly. Remove these lines if you never write “//” in a string literal
        if empty(filter(synstack(line('.'), min([col('.'), col('$')-1])),
                    \   'stridx(tolower(synIDattr(v:val, "name")), "comment")!=-1'))
            return "\n"
        endif
        let cidx=stridx(line, a:comsymb)
        if cidx==-1
            " No comments
            return "\n"
        elseif cidx==0 || line[:(cidx-1)]!~#'\S'
            " This assumes that vim own continuation works correctly: do not do work 
            " that can be done by something else
            return "\n"
        endif
        " Preserve tab indentation if any, correctly replace non-indent tabs with 
        " spaces
        let nextline=substitute(line[:(cidx-1)], '\v^(\s*)(\S.*)$',
                    \           '\=submatch(1).'.
                    \             'repeat(" ", strdisplaywidth(submatch(2), '.
                    \                                          indent('.').'))',
                    \           'g').a:comsymb
        " Preserve presence of a space after comment start mark
        if line[cidx+len(a:comsymb)] is# ' '
            let nextline.=' '
        endif
        return "\n".((col('.')<col('$'))?("\e\"_c0"):("\<C-\>\<C-o>\"_d0")).nextline
    endfunction
    
    inoremap <expr> <CR> <SID>NewLine('//')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have the following chunk of code: <div id=container> <someelement>This is any
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Lets say I have the following code snippet below, how do I also apply
Lets say I have this amputated Person class: class Person { public int Age
Lets say I have some interfaces: public interface IFoo { IBar DoesStuff(); } public
Let's say I have some code like this: <html> <head><title>Title</title></head> <body> <?php if (!$someCondition){
Lets say I have an abstract base class and a derived class with some
Lets say I have several web sites on my web server, all as applications

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.