3

I like using ed for small edits. Currently I just press the spacebar manually to indent blocks of code in ed. Is this how the authors of UNIX indented their code in ed? Or is there some shortcut they used I don't know about?

slm
  • 369,824
dan
  • 4,077
  • 5
  • 27
  • 34
  • In older versions of UNIX software, such as the files at tuhs.org or the 6th edition kernel as printed in the Lyons book, C code was indented using tab characters with tabs set every 8th column. – Mark Plotnick May 13 '14 at 17:05

3 Answers3

3

I think the most likely way it was intended by "the authors of UNIX" had been the good ol' "one job, one tool" approach: Write your code using ed, use indent afterwards to have it indented properly.

terdon
  • 242,166
Andreas Wiese
  • 10,400
  • 2
    indent appeared in 4.2BSD decades after the first Unix (and ed predates Unix). Also, nobody would waste precious storage space to indent with space characters at the time, you'd indent with tab and only need one per indent level. – Stéphane Chazelas May 13 '14 at 17:30
1

Being a line editor, ed does not keep track of indentation between lines.

You may use e !command to call an external code formatter on the file.

A typical editing session, where a simple C program is created, edited and indented, could look like this:

$ rm test.c
$ ed -p'> ' test.c
test.c: No such file or directory
> H
cannot open input file
> i
#include <stdlib.h>

int main(void)
{
/* There is no place else to go.
 * The theatre is closed.
 */

return EXIT_SUCCESS;
}
.
> /void/
int main(void)
> s/void/int argc, char **argv/
> %p
#include <stdlib.h>

int main(int argc, char **argv)
{
/* There is no place else to go.
 * The theatre is closed.
 */

return EXIT_SUCCESS;
}
> w
142
> e !clang-format test.c
158
> %p
#include <stdlib.h>

int main(int argc, char **argv)
{
    /* There is no place else to go.
     * The theatre is closed.
     */

    return EXIT_SUCCESS;
}
> w
158
> q
$

Notice writing the file before and after calling the code formatter (clang-format in this case). We're writing the file to test.c and then we're reading in the result of running the command on this file.

Kusalananda
  • 333,661
0

As far as I know, ed does not have a specific command for indenting a line. It doesn't indent automatically, and doesn't have a primitive command for adding some fixed amount of whitespace to the beginning of a line.

However, you can use s/^/ /, for instance, to add two spaces to the beginning of a line without otherwise changing it.

Here's an example editing session with a simple C program entered without indentation or a space between the #includes and main. # before a command introduces a comment.

$ ed '-p> ' hello_world.c
hello_world.c: No such file or directory
# print the buffer
> ,n
?
# insert text until "." from the beginning of the buffer.
> 0a
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("%d\n", 47);
return 0;
}
# print the buffer
> ,n
1   #include <stdio.h>
2   #include <stdlib.h>
3   int main() {
4   printf("%d\n", 47);
5   return 0;
6   }
# indent lines 4 and 5
> 4,5s/^/  /
# print the buffer again, see if it makes sense.
> ,n
1   #include <stdio.h>
2   #include <stdlib.h>
3   int main() {
4     printf("%d\n", 47);
5     return 0;
6   }
# add a blank line after line 2.
> 2a

.
# print the buffer again out of paranoia.
> ,n
1   #include <stdio.h>
2   #include <stdlib.h>
3   
4   int main() {
5     printf("%d\n", 47);
6     return 0;
7   }
# looks good, write and quit.
> wq
# size of file in bytes.
89
Greg Nisbet
  • 3,076