`ed`에서 코드 블록을 어떻게 들여쓰나요?

`ed`에서 코드 블록을 어떻게 들여쓰나요?

나는 ed작은 편집에 사용하는 것을 좋아합니다. 현재는 스페이스바를 수동으로 눌러 코드 블록을 들여쓰기만 합니다 ed. 이것이 UNIX 작성자가 에서 코드를 들여쓰기한 방법입니까 ed? 아니면 내가 모르는 어떤 단축키가 사용된 걸까요?

답변1

내 생각에 "UNIX 작성자"가 의도한 가장 그럴듯한 방식은 "하나의 작업, 하나의 도구" 접근 방식이었습니다. 를 사용하여 코드를 작성하고 나중에 적절하게 들여쓰기하는 데 ed사용하십시오 .indent

답변2

줄 편집기이기 때문에 ed줄 사이의 들여쓰기를 추적하지 않습니다.

e !command파일에 대해 외부 코드 포맷터를 호출하는 데 사용할 수 있습니다 .

간단한 C 프로그램을 만들고 편집하고 들여쓰기하는 일반적인 편집 세션은 다음과 같습니다.

$ 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
$

코드 포맷터를 호출하기 전과 후에 파일을 작성합니다( clang-format이 경우). 우리는 파일을 쓴 test.c다음 이 파일에 대해 명령을 실행한 결과를 읽습니다.

답변3

내가 아는 한 에는 ed줄 들여쓰기에 대한 특정 명령이 없습니다. 자동으로 들여쓰기되지 않으며 줄 시작 부분에 고정된 양의 공백을 추가하는 기본 명령이 없습니다.

그러나 s/^/ /예를 들어 를 사용하면 달리 변경하지 않고 줄 시작 부분에 두 개의 공백을 추가할 수 있습니다.

#include다음은 들여쓰기나 s와 사이에 공백 없이 입력된 간단한 C 프로그램을 사용한 편집 세션의 예입니다 main. #명령이 주석을 소개하기 전에.

$ 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

관련 정보