How to do a case sensitive search inside vi editor in Linux

VI editor can sometimes be very tricky and irritating if you are not fully aware of those small arguments which could make your life much easier while working on the editor. One such thing when are trying to search a string and you want the search to be case sensitive.

Let me tell you some special arguments which can be used for this purpose

 

How to search a string in vi editor?

  • Exit the editing mode i.e. INSERT mode by pressing "Esc" from your keyboard
  • Then press Forward Slash "/"
  • Next type the string you want to search which should appear at the lower left bottom of the editor
  • Hit Enter

If the searched string exists in the file, then it would be  highlighted  in some color.

 

To perform a Case sensitive search

Using the above method the search is normal and not case sensitive so there is a probability that you might miss out any word with a different case. To make sure you get everything as required follow the below steps

  • Press "Esc" to exit the INSERT mode
  • Then type ":set smartcase"

Now if you search for "The" string then you will get all the words having "The" with capital "T" and others would be skipped i.e. "the".

 

To remove a case sensitive search

  • Press "Esc" to exit the INSERT mode.
  • Then type ":set ignorecase"

Using this if you search "THE", then all the words ir-respective of their case would be highlighted

You can also use ":set ic" to set ignore space and ":set noic" to set smartcase.

NOTE: Once all the search strings are highlighted you can go the next matched word by pressing "n" and in case you want to search backward then press "N". To go the first search in the file press "ggn"

 

I hope this article was useful.

 

Leave a Comment