"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

관련 정보