LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   tap character in vi (https://www.linuxquestions.org/questions/linux-newbie-8/tap-character-in-vi-662035/)

ufmale 08-11-2008 12:32 PM

tap character in vi
 
How do I replace the first space of each line with tap in vi?
For example, i have

Name phone zip
John 243-3244 44234
Tim 523-2342 55242

I want the space between Name and phone number to be replace by tap.
is there an easy way in vi?

custangro 08-11-2008 12:39 PM

Assuming that it's ALWAYS going to be "Name phone" then I will try...

Code:

%s/Name phone/Name tap phone/g

Mr. C. 08-11-2008 12:43 PM

I think the OP means TAB (not tap) character.

Use \t (backslash t) as the tab character.

:%s/Name phone/Name\tphone/g

tronayne 08-11-2008 12:44 PM

Do you mean a tab character in the first space on each line? If so, start the editor then
Code:

:g/ /s//<tab>/
where <tab> is the tab key on your keyboard.

If you wanted to replace all (not just the first)
Code:

:g/ /s//<tab>/g

ufmale 08-11-2008 01:33 PM

Thank you for the quick response.. I meant <tab> :)

The method from tronayne seems to work, but I want to replace only the first space of each line. How do I do that? do I have to write a script to do it?

if I also want to have a index line number as well, is that a way to do it?

Here is an example of output i really need. Can anyone help?

1 <tab> John <tab> 243-3244 44234
2.<tab> xxx <tab> xxx xxx

Mr. C. 08-11-2008 01:52 PM

Remove the g flag from the end of the substitution.

Here's one way to number a range of lines:

Go to the first line that you want numbered. Type ma to place a Mark labeded a.
Go to the last line you wanted numbered. Type !'acat -n and hit Enter. This will send all the lines between the first and current line to cat -n, replacing your output.

tronayne 08-11-2008 02:19 PM

Quote:

The method from tronayne seems to work, but I want to replace only the first space of each line. How do I do that? do I have to write a script to do it?
Code:

:g/ /s//<tab/
replaces (or substitutes, which is what the s in the expression stands for) only the first space (as indicated); no, you do not have to write a script or anything else to do that (the additional g at the end of the line in the second example given would replace all spaces in every line).

If you want to insert a <tab> at the beginning of a line
Code:

:g/^/s//<tab>/
will do that.

If you wanted to insert a <tab> at the end of a line,
Code:

:g/$/s//<tab>/
would do that.

The caret (^) stands for beginning of line, the dollar sign ($) stands for end of line.

The vi editor uses regular expressions for editing; take a look at http://www.regular-expressions.info/reference.html for examples.

chrism01 08-11-2008 07:07 PM

Replace first space on each line:

:%s/ /<tab>/

where <tab> means hit tab key once


All times are GMT -5. The time now is 10:37 PM.