How do I substitute in vim editor? - vim

I want to substitute with vim editor a word but only on a portion of a code, that is to say the word "[iteration]" by "[index]". I know how to select the text to replace (visual mode) but I don't know how to apply a substitution on the text selected. The only thing I know to do is global substitution by typing "%s/\[interation\]/\[index\]/g: how can do the same on only selected text?

After you select a block in visual mode, press :, and you'll get a prompt with '<,'>, representing the block you've selected. You can then add instructions to be performed on that block like s/[interation]/[index]/g (without the %!). I.e., you'll have '<,'>s/[interation]/[index]/g. Press Enter and you're good to go.

Related

Append to visual block only on specific lines

Lets say i have the following text.
this.is.some.text
this.is.emos.text
this.is.some.text
this.is.emos.text
I want to edit this text in 'Visual Block' mode so that the text looks as follows.
this.is.some.text
this.is.emos_suffix.text
this.is.some.text
this.is.emos_suffix.text
It should work like this:
Select a visual block
If the visual block contains emos append '_suffix' to the visual
block (Just like the 'A' command would do)
The only native way to accomplish that from visual-block mode or any other visual mode is to use a substitution:
:'<,'>s/emos/&_suffix<CR>
where…
you press :,
Vim inserts the range '<,'> for you, meaning "from the fist selected line, :help '<, through the last selected line, :help '>`,
s/emos/&_suffix substitutes every first occurrence of emos on each line of the given range with itself, :help s/\&, followed by _suffix.
Visual selection is often an unnecessary step and, in this case, visual-block mode is totally useless because A or I is going to operate on every line of the selection anyway.
Another method:
/emos/e<CR>
a_suffix<Esc>
n
.
Another one:
/emos<CR>
cgn<C-r>"
_suffix<Esc>
.
Another one, assuming the cursor is on the first line of your sample:
:,'}s/emos/&_suffix<CR>
Etc.

vim: how to replace text in visual mode selection?

I've got a text file:
hello world ssh!
this is your destiny
Oh my goodness
In visual mode, I select from "world" to "my": I wish to change letter 's' into 'k' in my selection.
I definition cannot use line-mode because in that way the 3rd line's "goodness" will be changed.
So how to do this replacement in visual mode?
To achieve the same in normal mode (after you've made a selection and escaped from it), use the \%V atom along with the % range modifier together in a substitute query
:%s/\%Vs/k/g
:s/foo/bar - a substitute query
% - makes sure it applies to all lines in the file
\%V - limits the range to the last visual mode selection (i.e. the one you get with gv)
g - a global flag which makes sure all occurrences will be affected (instea of the first one in each line, which is the default behavior)
More at http://vim.wikia.com/wiki/Search_and_replace_in_a_visual_selection
You can use \%V to restrict the search to the visual selection:
:'<,'>s/\%Vs/k/g
See :help \%V

Decorate a block of text with a digraph box

Is there a vim scrip to allow me to visually select a box, then put a box around it using digraphs?
For example, input:
Hello World
And the output after visual selection (and the calling the script)
┌─────────────┐
│ Hello world │
└─────────────┘
Thanks!
Interesting... I had to do a lot of those re-formatting, thus I wrote a script called "BlockIt", do exactly what you want I guess. And it can do more than that.
Check it out:
https://github.com/sk1418/blockit
Not tested but DrawIt (command \b) should do that.
Look at www.vim.org for other drawing scripts (link to my search results).
You can use visual block mode for this purpose.
Press Ctrl+V to enter visual mode.
Move arrows left, right, up and down to select your box. (If you wanted to interchange the ends, you can press Ctrl+O. I mean, we usually select through the bottom right end. If you want to switch to top left corner and select topside, you can use this)
Now, after making the selection to fit your desired box size, you can fill it with a character like #,* or whatever you want.
Press r# to replace the entire box with a #. You can put any character after r.
Now, your box is full of characters.
You want to type something inside! You can enter replace mode by pressing Insert twice.
Then, start typing your text . press Esc once done.

Substitute for vim replace in Sublime

I'm looking for a replacement for vim's "replace with character" command--specifically, I want to be able to select some text and replace each character with some character that I type (difficulty: No "vintage" mode)
Example:
Starting with
I am some text with an arbitrary number: 12358998281
I want an easy way to select 12358998281 and turn it into 99999999999, to make the result
I am some text with an arbitrary number: 99999999999
(in vim, this would be done by moving the cursor to the beginning of 12358998281, selecting with ve, then pressing r9)
I can do this by selecting the text, bringing up the "find" dialog, making sure "in selection" and "by regex" are enabled, searching for ., then typing my character into the resulting multiselect. This is incredibly laborious, however, and it prevents me from doing this process on a multiselect (for example, if 12358998281 exists in multiple parts of the file, I might want to quickly replace all instances of it with 99999999999, rather than performing the process above, getting the substitution, copying it to the clipboard, and then replacing with that).
Does Sublime have a command that acts like vim's "replace" that I can bind to something, or do I have to write a macro to get what I need? Or, am I approaching this from entirely the wrong direction?
A more generalized way of thinking of this is "how can I break a select into a multiselect on all characters", if that helps.
By using this package https://sublime.wbond.net/packages/RegReplace you can create regex patterns and bind them to shortcuts.
Also if there are multiple occurrences of one word, you can put cursor on whatever part of the word and press CTRL+D multiple times. One CTRL+D press will select the word under the cursor, every other press will select next occurrence of the word.
You could also use https://sublime.wbond.net/packages/Expand%20Selection%20to%20Whitespace to expand the selection to whitespace if your word contain some random characters, and then press CTDL+D to select next occurrences of the word.
Edit: With the package regex shortcuts indeed you have to create regexes before binding them. But the CTRL+D does work without it.
I don't see a problem with using "Expand selection to whitespace" and than doing the CTRL+D as I wrote in the answer.
I've checked the usage of visual as you wrote in the question and this solution seems much faster to do. You don't have to place cursor in the beggining of the word as It can be whereever in the word and no find/replace is needed, since you'll multiselect all occurrences by holding CTRL+D for a sec and You'll be free to edit it.
You can also use https://sublime.wbond.net/packages/Expand%20Selection%20to%20Quotes to select text inside quote and combine it with CTRL+D if standard CTRL+D doesn't work with some text, or if select to whitespace selects too much text.
I ended up solving this with a simple (if inelegant) plugin:
import sublime_plugin
import sublime
class MultiSelectWithinSelectedCommand(sublime_plugin.TextCommand):
def run(self, edit):
selection = self.view.sel()
new_regions = []
for selected_region in selection:
if selected_region.empty():
selection.add(self.view.word(selected_region))
for selected_region in selection:
if selected_region.a > selected_region.b:
region_begin = selected_region.b
else:
region_begin = selected_region.a
for pos in range(selected_region.size()):
subregion_begin = region_begin + pos
subregion_end = subregion_begin + 1
new_regions.append(sublime.Region(subregion_begin, subregion_end))
selection.clear()
selection.add_all(new_regions)
Once I've stuck this in my plugins directory, I would bind a command in the keymap file like usual:
{ "keys": ["alt+f"], "command": "multi_select_within_selected" }
(with alt+f chosen arbitrarily), and lo, multi-select on all selected characters with a keypress (after which, I can press my replacement character).

Replace all spaces with an underscore in the selected block

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?
When in visual mode type:
:s/\%V /_/g
see http://vim.wikia.com/wiki/VimTip438
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

Resources