0

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.

Community
  • 1
  • 1
user_rak
  • 85
  • 4
  • 17

1 Answers1

1

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/^% //'
meuh
  • 11,500
  • 2
  • 29
  • 45
  • Both these commands deletes my last line. Any fix ? Note! I store these commands in my external tools manager in gedit. – user_rak Aug 14 '15 at 13:01
  • what OS are you on? what does `echo -n a | sed 's/^/%/' | cat -vet;echo` show? I have version 3.10.4 of gedit on ubuntu 14.04. – meuh Aug 14 '15 at 13:02
  • I am using gedit 2.28.4 and Gnome 2.28.2 Linux. I get %a on terminal – user_rak Aug 14 '15 at 13:09
  • sorry, I meant if you type the command in the terminal shell, not in the gedit scripts. – meuh Aug 14 '15 at 13:10
  • The difference in versions must account for the problem. I am not able to reproduce it. I edited my answer with a version with xargs that might work for you. – meuh Aug 14 '15 at 13:27
  • `xargs -i -d'\n' echo {} | sed 's/^% //'` Does this remove the % symbol for you ? Because it didn't remove for me, just only new line on the final line. – user_rak Aug 14 '15 at 14:29
  • yes. I used `'s/^% //'` supposing that you had a space after "%". if you dont have a space, use `'s/^%//'`. Similarly in the comment script I added a space after the added "%". if you dont want it use `sed 's/^/%/'` instead. The xargs version always adds an extra line on my version. Without xargs I get the correct edit, and I dont lose the last line like you do. – meuh Aug 14 '15 at 14:38