Substitute for vim replace in Sublime - vim

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).

Related

How do I substitute in vim editor?

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.

Vim - Select text in between parentheses, multiline

value(val_1)
value(val_100)
value(val_10)
I want to select text between parentheses and do it for multiline, for one line I can use f(va( but I don't know how to select for 2 remaining lines.
EDIT (SOLUTIONS)
What I want to is to change text inside parentheses with unique text every line, firstly, I was thinking to select the text, delete it then change the text manually, #rosipov tell there is a plugin to do the selection part and it's great, but #romainl gave me another direction that works too.
f(ci(foo<Esc>jci(bar<Esc>jci(baz<Esc>
Do you want to select this:
value([val_1])
value([val_100])
value([val_10])
or to select that:
value([val_1)]
[value(val_100)]
[value(val_10])
The first is unfortunately not doable. But depending on what you want to do with the selected text, change it for example, a reasonable approximation would be:
f(l<C-v>jj$cnew value)<Esc>
However I'm sure a lot of Vimmers would probably approach the problem with a substitution:
:,+2s/(.*/(new value)
The second is done simply with:
f(lv3/)h
or
f(ljjt)
You will probably be interested in EasyMotion plugin in this case: https://github.com/Lokaltog/vim-easymotion
With plugin it will be: f(vLeaderLeaderf)c
Or: LeaderLeaderf(avLeaderLeaderf)c
Where c is letter representing 3rd closing parentheses, a represents first opening p.
EDIT: Without plugin it is possible to do it by line number.
Assuming that you work with lines 1-3: f(v3Gf)
Where 3G stands for "go to line number 3", works in both visual and normal modes.

Remove/Add Line Breaks after Specific String using Sublime Text

Using Sublime Text 2 - Is it possible to insert a line break/text return after a specific String in a text file e.g. by using the Find ‣ Replace tool?
(Bonus question: Is it possible to remove all line breaks after a specific String)
Here's how you'd do it on a Mac:
Command+F > type string > Control+Command+G > ESC > Right Arrow > line break
and Windows/Linux (untested):
Control+F > type string > Alt+F3 > ESC > Right Arrow > line break
The important part being Control+Command+G to select all matches.
Once you've selected the text you're looking for, you can use the provided multiple cursors to do whatever text manipulation you want.
Protip: you can manually instantiate multiple cursors by using Command+click (or Control+click) to achieve similar results.
Using the Find - Replace tool, this can be accomplished in two different ways:
Click in the Replace field and press Ctrl + Enter to insert a newline (the field should resize but it doesn't, so it is hard to see the newline inserted).
Inside the Find - Replace tool, activate the S&R regex mode (first icon on the left .*, keyboard shortcut is Alt + Ctrl/Cmd + R to activate/deactivate it).
Type \n in the Replace field wherever you want to insert a newline.
Both solutions also work if you want to find newlines, just do it in the Find field.
Edit->Lines->Join Line (Ctrl+J)
You should probably use multiple cursors. See the unofficial documentation, or this nice tutorial. Here's some brief instructions to set you on your way:
Put the cursor on the string of interest.
Type Command+D (Mac) or Control+D (Windows/Linux) to select the current instance of the string.
Type Command+D (Mac) or Control+D (Windows/Linux) to select successive instances of the string.
Alternately, type Control+Command+G (Mac) or Control+Command+G to select all instances of your string.
Now you have multiple cursors, so insert or remove your newline as you please.
(type esc to exit multiple cursor mode.)
Have fun!

How do I get fine-grained undo in Vim

I find Vim's undo to be a bit too coarse. E.g. if I type something like this:
a // to go into edit mode
to be or not to ve
<esc> // to exit insert mode
Oops! I made a typo. I want to start undoing so I press u, but then it clears the whole line. Is there a way to undo word-by-word or character-by-character?
You can break undos via :help i_ctrl-g_u. You can map this if you want for every character, but that might a little bit complicated. Mapping this to space button is a way.
:inoremap <Space> <Space><C-g>u
After that every word can be undo via u
So as you see from the others what you are asking for doesn't exist in Vi (AFAIK).
Undo undoes what your last action was. If your last action was to enter insert mode and then add a line and then exit insert mode. That will be undone, however if from the default mode you hit the "x" key then you will delete 1 character or if in visual mode with text selected the text will be deleted. If you hit undo then you will restore that one character or the text that was selected.
...You should think of this as an action, and actions can be atomically undone or restored
As mentioned previously if you wish to delete the previous word then you should be able to hit Ctrl + w and delete the previous word while remaining in insert mode.
If you exit insert mode you can navigate (motion) back a word with "b" forward a word with "w" to the end of a word with "e", and can cut (which leaves you in insert mode) with "c" or delete with "d". Both actions cut and delete can accept a motion following them so you can delete the current word / up to the next word with "dw" or cut the previous word with "cb"
This concept becomes more useful when you remember to use the "." command (in normal mode). This command is to repeat the last action. I have used this many times to find and replace a small group of words in a file (It is especially useful if you are paranoid about changing too much). The scenario would be the following:
File:
This is my post
really it is a test
this is the middle
This is the end
if I wanted to replace "is" with "was" I could write:
%s/\<is\>/was/g
however if I wanted to change the first line and the third line "is" to "was" (and I didn't know their line numbers, and I wanted to see if there were any other places I wanted to change is to was I could type
"/is"
hit "n" until I reach the place I want substituted, and then hit "cw" and type "was"
I can now hit "n" until I reach another place I want substituted and hit ".", and that will replace "is" with "was" (Note: my search string didn't limit to the word "is", just the two characters "is" so "This" & "this" will match in this case)
No, it is not possible and is actually not necessary either. Vim has a million ways of dealing with that. Try cb for example. Or bC. Or brb. Or Tspace to jump back instead of b. Or ciw.
You can, of course use most of these solutions in insert mode (by pressing CTRLo first), or bind one to your favorite key combination (:help map and :help imap).
On Linux, using control-w while in input mode deletes the last 'word'.

In Vim, what is the best way to select, delete, or comment out large portions of multi-screen text?

Selecting a large amount of text that extends over many screens in an IDE like Eclipse is fairly easy since you can use the mouse, but what is the best way to e.g. select and delete multiscreen blocks of text or write e.g. three large methods out to another file and then delete them for testing purposes in Vim when using it via putty/ssh where you cannot use the mouse?
I can easily yank-to-the-end-of-line or yank-to-the-end-of-code-block but if the text extends over many screens, or has lots of blank lines in it, I feel like my hands are tied in Vim. Any solutions?
And a related question: is there a way to somehow select 40 lines, and then comment them all out (with "#" or "//"), as is common in most IDEs?
Well, first of all, you can set vim to work with the mouse, which would allow you to select text just like you would in Eclipse.
You can also use the Visual selection - v, by default. Once selected, you can yank, cut, etc.
As far as commenting out the block, I usually select it with VISUAL, then do
:'<,'>s/^/# /
Replacing the beginning of each line with a #. (The '< and '> markers are the beginning and and of the visual selection.
Use markers.
Go to the top of the text block you want to delete and enter
ma
anywhere on that line. No need for the colon.
Then go to the end of the block and enter the following:
:'a,.d
Entering ma has set marker a for the character under the cursor.
The command you have entered after moving to the bottom of the text block says "from the line containing the character described by marker a ('a) to the current line (.) delete."
This sort of thing can be used for other things as well.
:'a,.ya b - yank from 'a to current line and put in buffer 'b'
:'a,.ya B - yank from 'a to current line and append to buffer 'b'
:'a,.s/^/#/ - from 'a to current line, substitute '#' for line begin
(i.e. comment out in Perl)
:'s,.s#^#//# - from 'a to current line, substitute '//' for line begin
(i.e. comment out in C++)
N.B. 'a (apostrophe-a) refers to the line containing the character marked by a. ``a(backtick-a) refers to the character marked bya`.
To insert comments select the beginning characters of the lines using CTRL-v (blockwise-visual, not 'v' character wise-visual or 'V' linewise-visual). Then go to insert-mode using 'I', enter your comment-character(s) on the first line (for example '#') and finally escape to normal mode using 'Esc'. Voila!
To remove the comments use blockwise-visual to select the comments and just delete them using 'x'.
Use the visual block command v (or V for whole lines and C-V for rectangular blocks). While in visual block mode, you can use any motion commands including search; I use } frequently to skip to the next blank line. Once the block is marked, you can :w it to a file, delete, yank, or whatever. If you execute a command and the visual block goes away, re-select the same block with gv. See :help visual-change for more.
I think there are language-specific scripts that come with vim that do things like comment out blocks of code in a way that fits your language of choice.
Press V (uppercase V) and then press 40j to select 40 lines and then press d to delete them. Or as #zigdon replied, you can comment them out.
The visual mode is the solution for your main problem. As to commenting out sections of code, there are many plugins for that on vim.org, I am using tComment.vim at the moment.
There is also a neat way to comment out a block without a plugin. Lets say you work in python and # is the comment character. Make a visual block selection of the column you want the hash sign to be in, and type I#ESCAPE. To enter a visual block mode press C-q on windows or C-v on linux.
My block comment technique:
Ctrl+V to start blockwise visual mode.
Make your selection.
With the selection still active, Shift+I. This put you into column insert mode.
Type you comment characters '#' or '//' or whatever.
ESC.
Or you may want to give this script a try...
http://www.vim.org/scripts/script.php?script_id=23
For commenting out lines, I would suggest one of these plugins:
EnhancedCommentify
NERD Commenter
I find myself using NERD more these days, but I've used EnhancedCommentify for years.
If you want to perform an action on a range of lines, and you know the line numbers, you can put the range on the command line. For instance, to delete lines 20 through 200 you can do:
:20,200d
To move lines 20 through 200 to where line 300 is you can use:
:20,200m300
And so on.
Use Shift+V to go in visual mode, then you can select lines and delete / change them.
My usual method for commenting out 40 lines would be to put the cursor on the first line and enter the command:
:.,+40s/^/# /
(For here thru 40 lines forward, substitute start-of-line with hash, space)
Seems a bit longer than some other methods suggested, but I like to do things with the keyboard instead of the mouse.
First answer is currently not quite right?
To comment out selection press ':' and type command
:'<,'>s/^/# /g
('<, '> - will be there automatically)
You should be aware of the normal mode command [count]CTRL-D.
It optionally changes the 'scroll' option from 10 to [count], and then scrolls down that many lines. Pressing CTRL-D again will scroll down that same lines again.
So try entering
V "visual line selection mode
30 "optionally set scroll value to 30
CTRL-D "jump down a screen, repeated as necessary
y " yank your selection
CTRL-U works the same way but scrolls up.
v enters visual block mode, where you can select as if with shift in most common editors, later you can do anything you can normally do with normal commands (substitution :'<,'>s/^/#/ to prepend with a comment, for instance) where '<,'> means the selected visual block instead of all the text.
marks would be the simplest mb where u want to begin and me where u want to end once this is done you can do pretty much anything you want
:'b,'ed
deletes from marker b to marker e
commenting out 40 lines you can do in the visual mode
V40j:s/^/#/
will comment out 40 lines from where u start the sequence

Resources