Insert space before last character

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Insert space before last character

Adrian Johnson-6
Dear group:

I am trying to insert space after first character in a line and a
space before last character in same line using command mode. I could
introduce space before first character but could not insert space
before last character.


F783S
Y625D
A1705V

:%s//\(^.\)/\1 /g   results in

F 783S
Y 625D
A 1705V


now I want to give space before last character to get following:

F 783 S
Y 625 D
A 1705 V

I tried different ways and could not introduce space. I am successful
in remove the last character. Could anyone help please.


Thanks
Adrian

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Insert space before last character

Edwin Miller
Hello Adrian,

This should work:

:%s#\(.*\)\(.\)#\1 \2

Like in your solution \( \) is defined to use \1 and \2 in the substitution.

Hope this works fine,

regards

Edwin




Am 24.10.2017 um 18:58 schrieb Adrian Johnson:

> Dear group:
>
> I am trying to insert space after first character in a line and a
> space before last character in same line using command mode. I could
> introduce space before first character but could not insert space
> before last character.
>
>
> F783S
> Y625D
> A1705V
>
> :%s//\(^.\)/\1 /g   results in
>
> F 783S
> Y 625D
> A 1705V
>
>
> now I want to give space before last character to get following:
>
> F 783 S
> Y 625 D
> A 1705 V
>
> I tried different ways and could not introduce space. I am successful
> in remove the last character. Could anyone help please.
>
>
> Thanks
> Adrian
>

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Insert space before last character

Tim Chase
In reply to this post by Adrian Johnson-6
On 2017-10-24 12:58, Adrian Johnson wrote:

> I am trying to insert space after first character in a line and a
> space before last character in same line using command mode. I could
> introduce space before first character but could not insert space
> before last character.
>
>
> F783S
> Y625D
> A1705V
>
> :%s//\(^.\)/\1 /g   results in
>
> F 783S
> Y 625D
> A 1705V
>
>
> now I want to give space before last character to get following:
>
> F 783 S
> Y 625 D
> A 1705 V
>
> I tried different ways and could not introduce space. I am
> successful in remove the last character. Could anyone help please.

You can do

  :%s/.$/ &/

which will add a space before the last character.  If there's
trailing whitespace, you may have to tweak it:

  :%s/\S\s*$/ &/

In the future, you can do both in one pass by capturing the parts you
want and reassembling them:

  :%s/\(.\)\(.*\)\(.\)/\1 \2 \3/

Hope this helps,

-tim




--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Insert space before last character

Adrian Johnson-6
Thanks Tim and Edwin for quick reply
Much appreciated 
Adrian

On Tuesday, October 24, 2017, Tim Chase <[hidden email]> wrote:
On 2017-10-24 12:58, Adrian Johnson wrote:
> I am trying to insert space after first character in a line and a
> space before last character in same line using command mode. I could
> introduce space before first character but could not insert space
> before last character.
>
>
> F783S
> Y625D
> A1705V
>
> :%s//\(^.\)/\1 /g   results in
>
> F 783S
> Y 625D
> A 1705V
>
>
> now I want to give space before last character to get following:
>
> F 783 S
> Y 625 D
> A 1705 V
>
> I tried different ways and could not introduce space. I am
> successful in remove the last character. Could anyone help please.

You can do

  :%s/.$/ &/

which will add a space before the last character.  If there's
trailing whitespace, you may have to tweak it:

  :%s/\S\s*$/ &/

In the future, you can do both in one pass by capturing the parts you
want and reassembling them:

  :%s/\(.\)\(.*\)\(.\)/\1 \2 \3/

Hope this helps,

-tim




--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:;" onclick="_e(event, &#39;cvml&#39;, &#39;vim_use+unsubscribe@googlegroups.com&#39;)">vim_use+unsubscribe@....
For more options, visit https://groups.google.com/d/optout.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.