Question Locked

VI Editor problems

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi All,
I just want to ask about vi editor:

1. How to remove colors and highlightings from a vi editor text file.

2. I am having ^M character at end of each line in vi editor how to remove it.

8 replies

by apotheon

"[i]1. How to remove colors and highlightings from a vi editor text file.[/i]"

From within a vi (or Vim) session, you can (in command mode) enter this command:

[b]:syntax off[/b]

In a configuration file for vi (or Vim), such as [b].vimrc[/b] in the user's directory on a Unix (or Linux) system, you could include this line to have syntax highlighting turned off by default:

[b]syntax off[/b]

"[i]2. I am having ^M character at end of each line in vi editor how to remove it.[/i]"

In command mode, just enter this command:

[b]:%s/\r//g[/b]

The ^M you're seeing is not a normal carat symbol "^" followed by a normal "M" symbol. Instead, it's the visual representation of the "+" character sequence (holding down the Ctrl key and pressing the M key). The reason this is showing up is related to a mismatch between how Unix and DOS deal with linebreaks in text files.

With the command [b]:%s/\r//g[/b], you're using a regular expression to replace all instances of the carriage return character (the "^M" you see, represented by the \r escape character squence) with nothing. The "%" symbol says, basically, "operate on every line in the file". The "s" before the first slash stands for "substitution": "s///" is the format of a substitution regular expression. Substitution regular expressions search for an instance of whatever appears between the first and second slashes, and replaces it with whatever appears between the second and third slashes. The "g" at the end tells it to search "globally" within each line on which it operates.

In other words, "%s/\r//g" means:

1. For every line,
2. substitute
3. carriage return
4. with an empty string
5. globally.

Hope that helps.

edit: typo

by masoodsharif

Thanks.
^M character is removed and colours from vi file has been removed but highlighting is still there.

by apotheon

"[i]highlighting is still there.[/i]"

What do you mean by "highlighting" in this case?

by masoodsharif

The tag or word which we searched last time is filled with a background colour (highlighted) in whole vi file.

by apotheon

To turn it off from within the application (until you turn it back on or until you exit then restart the application):

[b]:set nohlsearch[/b]

To turn it off permanently for every single use of the application ever by setting a configuration option in the .vimrc file:

[b]set nohlsearch[/b]

Hope that helps.

by masoodsharif

Thanks for your great favor. Problems have been resolved by your precious help.

Thanks again.

by apotheon

Upvote my response(s), then. People like getting feedback when they provide help.

edit: Thanks.

This discussion is closed to new replies.