How to replace multiple selection with incremental number in sublime text 3? - sublimetext3

For example :
xx
test1
xx
xx
test1
xx
test1
test1
i use the regex search : test\d to grab those text1 and want to replace it with ok# . Sot make it to be like this :
xx
ok1
xx
xx
ok2
xx
ok3
ok4
How to generate that increment number ?

The built in Arithmetic command can do this sort of thing for you. The full specifics of how that command works can be found in "how to add different number at end of multi line edit?" if you'd like to know the various things you can do with the command and how it works.
For your case, the steps would be:
Do a regex Find and use the Find All button to select all of the items that you would like to replace; you end up with multiple cursors in the buffer
Type the part you want before the prefix (in this case ok) which will replace all of the matches with that text
Open the command palette and select the Arithmetic command and run it.
The default expression for the command is i + 1, which will do you want, so you can just hit enter to select it and run the command. The preview under the command name shows you what is going to be inserted.

Related

How to search and replace in current line only?

I see how to search and replace in specific lines, specifying by line number, and how to search and replace using the current line as reference to a number of lines down.
How do I search and replace in the current line only? I'm looking for a simple solution that does not involve specifying line numbers as the linked solutions do.
Replace all occurrences of str1 with str2 in certain line:
:s/str1/str2/g
remove the g option if you want to replace only the first occurrence.
You can use . for the current line, like:
:.s/old/new/
This will change old by new in the current line only.
If you want to search and replace all of the matched word in the current line, you could easily use simple substitute (s) with g modifier in command mode.
:s/search/replace/g
If you just want to search and replace the first matched word in the current line, just move away the g modifier from your command.
:s/search/replace/
Ref: :help substitute
Confirm (c) and Replace :
:s/foo/bar/gc
Find the occurrence of 'foo', and before replacing it with 'bar' ask for confirmation.
Vim gives you these options :
replace with bar (y/n/a/q/l/^E/^Y)?
y - yes
n - no
a - replace all occurrences
q - quit
l - replace the current and stop (last)
^E (CTRL + e) - scroll down
^Y (CTRL + y) - scroll up
Now, in your case you can use the combination of y, n and l to achieve your purpose.

How to complete the whole list with Vim?

I have a list of products to place on a rails seed and I would like to instead of put brackets one by one on the list with a command place the brackets on the whole list?
for example:
1. Dakine
2. Dale of Norway
3. Dan Post
1. ["Dakine"],
2. ["Dale of Norway"],
3. ["Dan Post"],
I searched on the help but did not find any about. Thanks.
You can record a macro in Vim and repeat that.
If you are on number 1, you can do following:
qqf a["Esc$a"],Esc0jq
Explanation:
qq: Start recording macro in register q
f: Go to first space character
a: : Insert after (the space character from above)
\[": Insert those characters
Esc: Back to normal mode
$: Go to end of line
a: Insert after (end of line)
"],: Insert the characters
Esc: Back to normal mode
0: Jump to start of line
j: Go down one line
If you have 100 such lines, you can do 100#q to achieve your result.
With vim substitute command:
:%s/.*/["&"]/
If you don't want to operate on all lines, then select the ones you want to transform or note the related line numbers, and then type :s/..... without the %. You'll see actually :'<,'>s this range represent the visually selected lines, and vim adds it automatically in visual mode.
On Atom you can enable the find to use Regex in the search(there is a button next to the search field)
Then you can search for something like (^.*$) to get every line separated by groups and in the Replace field you use ["$1"],. The $1 represents the value matched by the Regex.
Then just do a Replace All and remove the last comma in your list if needed.

Bash script : select lines starting with number in specific range

I have a file containing lines like:
121<some letters> random text ...
1234<some letters> random numbers etc...
Each line starts with a number followed by some letters.I'm looking for a way to select only the lines which start with a number in a specific interval, for example : [0-9999] . I'm having difficulty in selecting these lines if the number of digits can vary.
Tried using grep but can't seem to find the correct way to write the regex.
awk '($1+0)>10 && ($1+0)<50' file
would print lines that start with a number from 11 to 49 inclusive.
Through grep,
grep -E '^([1-9][0-9]?[0-9]?[0-9]|[0-9])\b' file

Allign the words to the specified column in vim using commands

How I can move or shift the words in the entire file to the specified column?
For example like below:
Before :
123 ABC
112 XYZS
15925 asdf
1111 25asd
1 qwer
After :
123 ABC
112 XYZS
15925 asdf
1111 25asd
1 qwer
How it can be done using command mode?
Here the thing is we need to shift the 2nd word to the specified column
Here the specified column is 8
except for vim-plugins mentioned by others, if you were working on a linux box with column command available, you could just :
%!column -t
% could be vim ranges, e.g. visual selections etc..
Approach with built-in commands
First :substitute the whitespace with a Tab character, and then :retab to a tab stop to column 8, expanding to spaces (for your given example):
:.,.+4substitute/\s\+/\t/ | set tabstop=7 expandtab | '[,']retab
(I'm omitting the resetting of the modified options, should that matter to you.)
Approach with plugin
My AlignFromCursor plugin has commands that align text to the right of the cursor to a certain column. Combine that with a :global command that invokes this for all lines in the range, and a W motion to go to the second word in each, and you'll get:
.,.+4global/^/exe 'normal! W' | LeftAlignFromCursor 8
I use the Tabular plugin. After installing it, you run the following command:
:%Tab/\s
where \s means whitespace character
I have made two functions for this problem.
I have posted it here : https://github.com/imbichie/vim-vimrc-/blob/master/MCCB_MCCE.vim
We need to call this function in vim editor and give the Number of Occurrence of the Character or Space that you wants to move and the character inside the '' and the column number.
The number of occurrence can be from the starting of each line (MCCB function) or can be at the end of each line (MCCE function).
for the above example mentioned in the question we can use the MCCB function and the character we can use space, so the usage will be like this in the vim editor.
:1,5call MCCB(1,' ',8)
So this will move the first space (' ') to the 8th column from line number 1 to 5.

How do I repeat an edit on multiple lines in Vim?

I'm aware that in Vim I can often repeat a command by simply adding a number in front of it. For example, one can delete 5 lines by:
5dd
It's also often possible to specify a range of lines to apply a command to, for example
:10,20s:hello:goodbye:gc
How can I perform a 'vertical edit'? I'd like to, for example, insert a particular symbol, say a comma, at the beggining (skipping whitespace, i.e. what you'd get if you type a comma after Shift-I in command mode) of every line in a given range. How can this be achieved (without resorting to down-period-down-period-down-period)?
Ctrl-v enters visual mode blockwise. You can then move (hjkl-wise, as normal), and if you want to insert something on multiple lines, use Shift-i.
So for the text:
abc123abc
def456def
ghi789ghi
if you hit Ctrl-v with your cursor over the 1, hit j twice to go down two columns, then Shift-i,ESC , your text would look like this:
abc,123abc
def,456def
ghi,789ghi
(the multi-line insert has a little lag, and won't render until AFTER you hit ESC).
:10,20s/^/,/
Or use a macro, record with:
q a i , ESC j h q
use with:
# a
Explanation: q a starts recording a macro to register a, q ends recording. There are registers a to z available for this.
That's what the :norm(al) command is for:
:10,20 normal I,
If you are already using the '.' to repeat your last command a lot, then I found this to be the most convenient solution so far. It allows you to repeat your last command on each line of a visual block by using
" allow the . to execute once for each line of a visual selection
vnoremap . :normal .<CR>
I believe the easiest way to do this is
1) record a macro for one line, call it 'a'; in this case one types
q a I , ESC j q
2) select the block of lines that you want to apply the macro to
3) use the 'norm' function to execute macro 'a' over this block of lines, i.e.,
:'<,'>norm#a
I think the easiest is to record a macro, and then repeat the macro as many times as you want. For example to add a comma at the start of every line, you type:
q a I , ESC j q
to repeat that 5 times, you enter
5 # a
With your edit already saved in the . operator, do the following:
Select text you want to apply the operator to using visual mode
Then run the command :norm .
Apart from the macros, as already answered, for the specific case of inserting a comma in a range of lines (say from line 10 to 20), you might do something like:
:10,20s/\(.*\)/,\1
That is, you can create a numbered group match with \( and \), and use \1 in the replacement string to say "replace with the contents of the match".
I use block visual mode. This allows you to perform inserts/edits across multiple lines (aka 'vertical edits').

Resources