Search code examples
bashgeditcomments

commenting and uncommenting in gedit using bash


I am novice to bash. I am setting up my gedit tool with a new bash command to comment and uncomment selected lines using the solution as given in this link Since my commenting style is '%' , so i have written like this compared to original solution given in that link.

comment="%"

xargs -i -d\\\n echo $comment{} 

I have two questions:

  1. when I do the commenting as shown above, the selected lines are commented, but the cursor enters to the next line from the last commented line. Thus leaving a blank space if there exist other statements after the last commented line. How to avoid it this cursor jumping to the next line.

  2. The bash command to uncomment the commented lines given in that link removes the selected lines rather than removing the comments. how to fix it for my case ?

Thank you.


Solution

  • I'm not sure why the example you cite is using xargs in this way, both in the comment and un-comment parts. I tried a simple sed and it was much better. So simply use for the comment script:

    #!/bin/bash
    sed 's/^/% /'
    

    and for the un-comment script:

    #!/bin/bash
    sed 's/^% //'
    

    This worked for me with version 3.10.4 of gedit on ubuntu 14.04.


    Otherwise restore the xargs to ensure there is a newline on the final line and use the 2 versions:

    #!/bin/bash
    xargs -i -d'\n' echo {} | sed 's/^/% /'
    

    and

    #!/bin/bash
    xargs -i -d'\n' echo {} | sed 's/^% //'