Menu

Comment a block

Help
2007-07-30
2022-08-24
  • Jack Malmostoso

    Jack Malmostoso - 2007-07-30

    Hello joe developers,

    first of all thanks for this editor... it's the FIRST thing I install whenever I put my pawns on a machine!
    I had a question: is there a way to automagically comment (i.e. add a "#" at the beginning of a line) a selected block of text?

    It's be useful when I edit files like /etc/network/interfaces or xorg.conf.

    Thanks for your help and keep up the good work!

     
    • Charles J. Tabony

      As far as I know, there is no comment/uncomment command.  Two ways you could do it are:

      ^[B
      ^KF
      \^\*\$
      rk
      #\&
      r

      or

      ^K/
      sed 's/^.*$/#&/'

      The second method might be faster if you do the same operation many times in a file since you can hit up to recall the sed command after entering it the first time.  Unfortunately the history for filter commands does not persist between invocations of joe.  The history for search and replace commands does, so that might be faster if you open and close joe frequently.

      Charles J. Tabony

       
    • Danny

      Danny - 2007-08-18

      Any way of integrating this into Joe?  Or making a semi-permanent shortcut defined for the operation?

       
      • Charles J. Tabony

        This certainly could be, and probably should be, made a command.  For some types of files, JOE knows the syntax of comments and uses that knowledge to ignore the contents of comments when searching for matching delimiters.  However, the different styles of comments are hard coded.  See this snippet of code from uedit.c:

                                } else if ((bw->o.pound_comment && d == '#') ||
                                           (bw->o.semi_comment && d == ';') ||
                                           (bw->o.vhdl_comment && d == '-' && brch(p) == '-')) {
                                        while ((d = pgetc(p)) != NO_MORE_DATA)                 
                                                if (d == '\n')
                                                        break;
                                } else if ((bw->o.c_comment || bw->o.cpp_comment) && d == '/') {

        It would be good if JOE had some more general way of handling comment styles, such as being able to specify the string that starts a comment and the string that terminates a comment (which in most cases is a newline).

        For comments that end with a newline, a "comment block" operation would be simple.  Other types of comments, like C style comments would be trickier.  Every person has their own style of commenting a block of text.  Also, what would you do if the block already contained a comment?  If you want an uncomment block command, then how would it undo your solution to the last question?

        If you (or someone reading this) want to implement this, then you can use urindent or ulindent in ublock.c as a starting point.  Speaking of which, you can actually comment a block with pound signs by going to options (^T), selecting "Indent char", entering 35 (the ASCII code for '#'), and then using the indent block command.  Just make sure you set the indent character back before you try to actually indent something.

        You can make a semi-permanent shortcut for any sequence of commands using macros.  Hit "^K[", enter a number from 0 through 9, then enter one of the sequences of commands above, then hit "^K]".  Later, you can hit ^K followed by the number you entered to perform the sequence of commands.  The macro will be remembered between sessions of JOE.  If you use the first sequence and you don't want your cursor to move when you invoke the macro, then you can add two "^K-"s to the end of the sequence like so:

        ^K[
        0
        ^[B
        ^KF
        \^\*\$
        rk
        #\&
        r
        ^K-
        ^K-
        ^K]

        uncomment block macro:

        ^K[
        1
        ^[B
        ^KF
        \^#\*\$
        rk
        \0
        r
        ^K-
        ^K-
        ^K]

        Charles J. Tabony

         
  • Arpad Jakab

    Arpad Jakab - 2021-03-02

    Hi!

    A little late reply, but I thought I'd share my solution. It might be useful for some of you.
    I created two macros for commenting and uncommenting selected lines (Ctrl + arrow), and bound them to the keyboard combinations: ^K ^C and ^K ^U respectrively. I made it so that this works only for files with the extension "sh", thus the comment indicator: #. For other file types it will not be active.

    Just add this to your ~/.joerc

    Cheers,
    Arpad

    :include /etc/joe/joerc
         This is Joe's editor user config file. By Arpad JAKAB 2021-03-01
         Indented lines are comments!!!!
    
         -------------------------------------------------------------
         My named macros:
         -------------------------------------------------------------
         comment block
    :def cblk execmd,"mode",rtn,"istep",rtn,"1",rtn,
    execmd,"mode",rtn,"indentc",rtn,"35",rtn,
    rindent,
    execmd,"mode",rtn,"istep",rtn,"4",rtn,
    execmd,"mode",rtn,"indentc",rtn,"9",rtn
    
         uncomment block
    :def ucblk execmd,"mode",rtn,"istep",rtn,"1",rtn,
    execmd,"mode",rtn,"indentc",rtn,"35",rtn,
    lindent,
    execmd,"mode",rtn,"istep",rtn,"4",rtn,
    execmd,"mode",rtn,"indentc",rtn,"9",rtn 
    
         -------------------------------------------------------------
         File type specific comment block keystrokes and other settings
         -------------------------------------------------------------
    [mybash]
         Settings for bash (.sh) files.
    *.sh
    -syntax sh
    -keymap mybash
    
    :mybash
    :inherit main
    :delete ^K ^C
    :delete ^K ^U
    cblk    ^K ^C
    ucblk   ^K ^U
    
     

    Last edit: Arpad Jakab 2021-03-02
    • Duke Normandin

      Duke Normandin - 2022-05-12

      Hello Arpad! JOE editor patron as well!
      Thanks for the macro! I too was looking for a similar to emacs for commenting regions.

      I'm trying to get your macro to work with a file-type other than .sh - so I modified your macro in my ~/.joerc file. Unfortunately, I cannot get it to work at all. However, it may be because I use jmacs. Any ideas? Thanks again!

      Duke

       

      Last edit: Duke Normandin 2022-05-12
  • Nathan Middleton

    I have personally found for block commands using small shell scripts and sed/awk to a be a life saver and a simple way to extend Joe without macros.

     

Log in to post a comment.