¿Cómo puedo añadir una cadena al final de cada línea en Vim?


Quiero añadir * al final de cada línea en Vim.

Probé el código sin éxito

:%s/\n/*\n/g
Author: Ionică Bizău, 2009-02-27

10 answers

:%s/$/\*/g

Debería funcionar. Así debería :%s/$/*/g como MrWiggles señala correctamente.

 293
Author: dirkgently,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:26:38

Incluso más corto que el comando :search:

:%norm A*

Esto es lo que significa:

 %       = for every line
 norm    = type the following commands
 A*      = append '*' to the end of current line
 296
Author: Leonardo Constantino,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-03-01 07:52:45

También:

:g/$/norm A*

También:

gg<Ctrl-v>G$A*<Esc>
 37
Author: Brian Carper,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-02-27 19:37:45

Creo que usar el modo de bloque visual es un método mejor y más versátil para lidiar con este tipo de cosas. He aquí un ejemplo:

Esta es la Primera línea.
Este es el segundo.
Tercero.

Para insertar " Hola mundo."(espacio + portapapeles) al final de cada una de estas líneas:

  • En un carácter de la primera línea, presione Ctrl-V (o Ctrl-Q si Ctrl-V es pegar).
  • Presione jj para extender el bloque visual sobre tres líneas.
  • Presione $ para extender el bloqueo visual al final de cada línea. Pulse A a continuación, el espacio a continuación, escriba Hola mundo. + luego Esc.

El resultado es:

Esta es la Primera línea. Hola mundo.
Este es el segundo. Hola mundo.
Tercero. Hola mundo.

(ejemplo de http://vim.wikia.com/wiki/Inserting_text_in_multiple_lines)

 23
Author: nicole,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-08 19:08:10

Si desea agregar Hola mundo al final de cada línea:

:%s/$/HelloWorld/

Si desea hacer esto para un número específico de línea, diga, de 20 a 30 use:

:20,30s/$/HelloWorld/

Si desea hacer esto al comienzo de cada línea, use:

:20,30s/^/HelloWorld/
 9
Author: utkarsh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-16 09:59:25

...y para anteponer (añadir el principio de) cada línea con*,

%s/^/*/g
 5
Author: JohnnyCoder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-18 17:28:44

Realmente no necesitas el g al final. Así que se convierte en:

:%s/$/*

O si solo quieres el * al final de, digamos las líneas 14-18:

:14,18s/$/*

O

:14,18norm A*
 5
Author: Pedro Norwego,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-15 12:57:07

Una opción es:

:g/$/s//*

Esto encontrará cada anclaje final de línea y lo sustituirá por *. Digo "sustituto" pero, en realidad, es más un apéndice ya que el ancla es una cosa especial en lugar de un carácter regular. Para más información, vea Power of g - Examples .

 4
Author: paxdiablo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-11 11:12:13
:%s/\n/*\r/g

El primero es correcto en cualquier otro lugar, pero Vim tiene que tener un manejo diferente de la nueva línea por alguna razón.

 2
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-02-27 12:05:37
%s/\s*$/\*/g

Esto hará el truco, y asegurará que los espacios iniciales sean ignorados.

 1
Author: ng.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-27 05:12:56