find and replace a String in vi editor on linux? - linux

i am trying to replace String with new String in Linux vi editor
:s/totel_email_count/total_email_count/g
but getting following error.
E486: Pattern not found: totel_email_count

I'd guess the reason you're getting that error message is because you intend to replace the string on all lines, not just the current one. In order to search all lines, add a % to your command:
:%s/totel_email_count/total_email_count/g

To find and replace with vi editor type following:-
Type : (colon) followed by %s/foo/bar/ and hit [Enter] key.
:%s/foo/bar/
for example:-
:%s/old_string/new_string/

Related

VI read line not giving desired output

I have a file called test, I open it using vi as such:
vi test
Now I want to insert a line through a shell command, for simplicity I use a printf:
:r! printf %s hello
However the line that is entered is
tests
i.e. the name of the file with a s appended.
If I enter the same command in terminal directly, it works fine.
What I want to do is ultimately be able to encode a string in base64 and enter it on the same line as where my cursor is in vi, so that I won't have to copy the string in a separate terminal, encode it, and copy it back into vi. How can I do this? What am I doing wrong?
The first stage of processing a command line in vim is expanding it. % is expanded to the name of the current file — test in your case. %s is expanded to tests.
To avoid expanding protect the special character with a backslash:
:r! printf \%s hello

How do you replace a line with forward slashes in it in EX?

I'm running a script for vim EX mode I've tried every escape character and word identifier I can find.
it needs to find the string "/etc/walker" and replace it with "/etc/runner"
% s/\</etc/walker\>/\</etc/runner\>/g
wq
same issue with a script to append at the end of the file. It doesn't do anything. I'm trying to append "/etc/walker"
$
a
\</etc/walker\>
.
wq
what I've tried on regex editors seems to work there but not in EX
Thanks for your help
Try this:
:s#/etc/walker#/etc/runner#
Notice the use of # as a delimiter, that way you don't have to add back slashes.
You could also use:
:s#/etc/walker#/etc/runner#
For appending at the end of the line:
:s#$#/etc/walker#
In EX mode just remove the : at the beginning.

Vi character encoding issue

I am trying to see the contents of a Finnish text on vi. However, it replaces some letters with numbers. For example, <8a> stands for ä, etc.
I changed the character encoding to ISO8859-1, however still when viewing the file, it cannot show the umlaut letters. I also tried :set encoding=latin1 on vi, however it changes to different letters but not umlaut letters. Finally, I tried to replace those numbers with the original letters, but I am getting 'pattern not found' error. I am not sure whether I am doing the substitution correctly though: %s/<8a>/ä
Are there any more solution ideas?
vi screenshot
Try from command line:
vim -c "set encoding=utf8" -c "set fileencoding=utf8" -c "wq" filename
being filename the original file you need to open with right encoding.

Forcefully quit vim even if pattern not found

I am using command :%s/foo/bar/g | wq to find and replace using vim. It works fine if pattern is available but if pattern isn't available it Error detected while processing command line:
E486: Pattern not found: foo
How can I forcefully exit even if pattern isn't found? I tried wq! in above command but didn't help
The problem is with substitute issuing the error. You can set the flag e
:%s/foo/bar/ge | wq
which should prevent the "No match" error from breaking a mapping or commands.
:h :s_flags
Every time I tried to exit with :q it gave me an error message "E486: Pattern not found:..."
If I press ":" it appears "?" and instead of :q appeared ?q and gave me the error message: "E486: Pattern not found:..."
Cause? Instead of the US keyboard, I had set the Romanian (Standard) keyboard.
And instead of ":" type "ș" (special Romanian character).
I set back to US keyboard and everything was solved.

Does LightTable's vim mode support regex substitution?

In LightTable, I've installed the vim plugin and activated it in user.behaviors. I can perform basic navigation and text editing with vim bindings, but when I try to do a command like:
:%s/test/prod/g
... this doens't work. Instead, typing ":" opens a right-hand bar sidebar like:
But typing a "substitute" command into this box has no effect (can't press enter on completion):
I've confirmed that the "substitute" is supported by CodeMirror's vim mode on which LightTable's vim mode is based.
Would appreciate any advice on how to do this properly!
If your using something like vim, I believe you need to declare sed before making a regex expression like s/test/prod/g. Try typing this in your script:
sed 's/test/prod/g'
EDIT: When I was researching LightTable, i found the syntax to be slightly different:
sed([options ,] search_regex, replace_str, file)
For example:
sed('-i', 'PROGRAM_VERSION', 'v0.1.3', 'source.js');
-i: Replace contents of 'file' in-place. Note that no backups will be created!

Resources