Como um código de recuo é bloqueado em 'ed'?

3

Eu gosto de usar ed para pequenas edições. Atualmente, basta pressionar a barra de espaço manualmente para recuar blocos de código em ed . É assim que os autores do UNIX indentaram seu código em ed ? Ou há algum atalho que eles não conhecem?

    
por dan 13.05.2014 / 15:56

3 respostas

2

Acho que a maneira mais provável que foi pretendida pelos "autores do UNIX" foi a abordagem boa de "um trabalho, uma ferramenta": Escreva seu código usando ed , use indent para tê-lo recuado corretamente.

    
por 13.05.2014 / 16:23
1

Sendo um editor de linhas, ed não acompanha o recuo entre as linhas.

Você pode usar e !command para chamar um formatador de código externo no arquivo.

Uma sessão de edição típica, em que um programa C simples é criado, editado e recuado, poderia ter esta aparência:

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

Aviso ao gravar o arquivo antes e depois de chamar o formatador de código ( clang-format neste caso). Estamos escrevendo o arquivo para test.c e, em seguida, estamos lendo o resultado da execução do comando neste arquivo.

    
por 08.07.2016 / 15:34
0

Tanto quanto sei, ed não possui um comando específico para recuar uma linha. Ele não recua automaticamente e não possui um comando primitivo para adicionar uma quantidade fixa de espaço em branco ao início de uma linha.

No entanto, você pode usar s/^/ / , por exemplo, para adicionar dois espaços ao início de uma linha sem alterá-la de outra forma.

Veja um exemplo de sessão de edição com um programa C simples inserido sem recuo ou um espaço entre #include s e main . # antes de um comando apresentar um comentário.

$ 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
    
por 04.11.2018 / 18:47

Tags

yii\base\ErrorException
Copied! Copy Stacktrace Search Stackoverflow Search Google Error

PHP Core Warning – yii\base\ErrorException

PHP Startup: Unable to load dynamic library 'memcached.so' (tried: /usr/lib/php/20190902/memcached.so (/usr/lib/php/20190902/memcached.so: undefined symbol: igbinary_serialize), /usr/lib/php/20190902/memcached.so.so (/usr/lib/php/20190902/memcached.so.so: cannot open shared object file: No such file or directory))

$_GET = [
    'id' => '808317',
    'url' => 'how-does-one-indent-code-blocks-in-ed',
];

$_COOKIE = [
    '_csrf-frontend' => 'f427b37e5126dcb1b857757f5cf9e582f8a19624fdf99a913e01224c5e9f86c8a:2:{i:0;s:14:"_csrf-frontend";i:1;s:32:"vJ89bljGljB2yHVAiOPf7Kp-H6d8K4fO";}',
    'advanced-frontend' => '80jls9h7eou7sl6mgld671k4bc',
];

$_SESSION = [
    '__flash' => [],
];