Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all spaces with an underscore in the selected block

Tags:

vim

I want to replace all spaces with an underscore, in the currently highlighted block i.e. not to apply it on the entire page.

How can I do this?

like image 356
codecompleting Avatar asked Oct 26 '11 21:10

codecompleting


People also ask

How do you replace a space with an underscore in react?

Use the String. replaceAll method to replace all spaces with underscores in a JavaScript string, e.g. string. replaceAll(' ', '_') . The replaceAll method returns a new string with all whitespace characters replaced by underscores.

How do you replace a space underscore in Python?

To replace spaces with underscores in Python:Call the replace() method on the string. Pass a string containing a space and a string containing an underscore to the method. The replace() method will return a string with all spaces replaced by underscores.


2 Answers

When in visual mode type:

:s/\%V /_/g

see http://vim.wikia.com/wiki/VimTip438

like image 199
topek Avatar answered Sep 29 '22 16:09

topek


You could go into visual mode (by typing v while in command mode) and then select the required text and thereafter enter the command mode (by typing ":"). This will automatically insert the selection range and then you could perform the necessary substitution.

  • Go to visual mode by typing v
  • Select the necessary text.
  • Type : to enter command mode. You would find in the prompt below :'<,'>
  • The final command would look like this :'<,'>s/ /_/g
like image 32
Ifthikhan Avatar answered Sep 29 '22 15:09

Ifthikhan