Using regular paste (p) in normal mode breaks indentation - vim - indentation

I saw that there is an set paste option that allows correct indentation when pasting from the system clipboard in terminal vim, but I miss how to paste from vim's buffer "inner clipboard" with the p or P command and apply = to the pasted text. Until now I manually do that.
Doing set paste or its inverse doesn't fix the indentation for the pasted text.
Could you help making a .vimrc mapping for it ? Or is there an built-in option for that ?
Thank you.

The ]p and ]P commands will paste and place the contents at the indentation of the current line instead of using the original indentation of the copied lines. For example:
line 1
line 2
line 3
line 4
Copying lines 2 & 3, moving to line 3 and pressing ]p would result in
line 1
line 2
line 3
line 2
line 3
line 4

Try ]p and ]P
From :help p:
["x]]p or ]p ]<MiddleMouse>
["x]]<MiddleMouse> Like "p", but adjust the indent to the current line.
Using the mouse only works when 'mouse' contains 'n'
or 'a'. {not in Vi}
["x][P or [P
["x]]P or ]P
["x][p or [p [<MiddleMouse>
["x][<MiddleMouse> Like "P", but adjust the indent to the current line.
Using the mouse only works when 'mouse' contains 'n'
or 'a'. {not in Vi}

Related

Format a text file by regex match and replace

I have a text file that looks like the following:
Chanelle
Jettie
Winnie
Jen
Shella
Krysta
Tish
Monika
Lynwood
Danae
2649
2466
2890
2224
2829
2427
2816
2648
2833
2453
I need to make it look like this
Chanelle 2649
Jettie 2466
... ...
I tried a lot on sublime editor but couldn't figure out the regex to do that. Can somebody demonstrate if it can be done.
I tested the following in Notepad++ but it should work universally.
Use this as the search string:
(?:(\s+[A-Za-z]+)(\r?\n))((?:\s*[A-Za-z]*\r?\n)+)\s+(\d+)
and this as the replacement:
$1 $4$2$3
Running a replace with it once will do one line at a time, if you run it multiple times it'll continue to replace lines until there are no matching lines left.
Alternatively, you can use this as the replacement if you want to have the values aligned by tabs, but it's not going to match in all cases:
$1\t\t$4$2$3
While the regex answer by SeinopSys will work, you don't need a regex to do this - instead, you can take advantage of Sublime's multiple cursors.
Place your cursor at the beginning of line 1, then hold down Shift↓ to select all the names.
Hit CtrlShiftL (Selection -> Split into Lines) to split the selection into lines.
CtrlC to copy.
Place your cursor on line 11 (the first number line) and press CtrlShift↓ (Windows/OS X) or AltShift↓ (Linux) to place a cursor at the beginning of each number line.
Hit CtrlV to paste the names before the numbers.
You can now delete the names at the top and you're all set. Alternatively, you could use CtrlX to cut the names in step 3.

Enumerate existing text in Vim (make numbered list out of existing text)

I have a source document with the following text
Here is a bunch of text
...
Collect underpants
???
Profit!
...
More text
I would like to visually select the middle three lines and insert numbers in front of them:
Here is a bunch of text
...
1. Collect underpants
2. ???
3. Profit!
...
More text
All the solutions I found either put the numbers on their own new lines or prepended the actual line of the file.
How can I prepend a range of numbers to existing lines, starting with 1?
It makes for a good macro.
Add the first number to your line, and put your cursor back at the beginning.
Start a macro with qq (or q<any letter>)
Copy the number with yf<space> (yank find )
Move down a line with j
Paste your yank with P
Move back to the beginning of the line with 0
Increment the number with Ctrl-a
Back to the beginning again with 0 (incrementing positions you at the end of the number)
End the macro by typing q again
Play the macro with #q (or #<the letter you picked>)
Replay the macro as many times as you want with <number>## (## replays the last macro)
Profit!
To summarize the fun way, this GIF image is i1. <Esc>0qqyf jP0^a0q10#q.
To apply enumeration for all lines:
:let i=1 | g/^/s//\=i.'. '/ | let i=i+1
To enumerate only selected lines:
:let i=1 | '<,'>g/^/s//\=i.'. '/ | let i=i+1
Set non recursive mapping with following command and type ,enum in command mode when cursor is inside the lines you are going to enumerate.
:nn ,enum {j<C-v>}kI0. <Esc>vipg<C-a>
TL;DR
You can type :help CTRL-A to see an answer on your question.
{Visual}g CTRL-A Add [count] to the number or alphabetic character in
the highlighted text. If several lines are
highlighted, each one will be incremented by an
additional [count] (so effectively creating a
[count] incrementing sequence).
For Example, if you have this list of numbers:
1.
1.
1.
1.
Move to the second "1." and Visually select three
lines, pressing g CTRL-A results in:
1.
2.
3.
4.
If you have a paragraph (:help paragraph) you can select it (look at :help object-select). Suppose each new line in the paragraph needs to be enumerated.
{ jump to the beginning of current paragraph
j skip blank line, move one line down
<C-v> emulates Ctrl-v, turns on Visual mode
} jump to the end of current paragraph
k skip blank line, move one line up
required region selected, we can make multi row edit:
I go into Insert mode and place cursor in the beginning of each line
0. is added in the beginning of each line
<Esc> to change mode back to Normal
You should get list prepended with zeros. If you already have such, you can omit this part.
vip select inner paragraph (list prepended with "0. ")
g<C-a> does the magic
I have found it easier to enumerate with zeroes instead of omitting first line of the list to enumerate as said in documentation.
Note: personally I have no mappings. It is easier to remember what g <C-a> does and use it directly. Answer above describes usage of pure <C-a> which requires you to manually count whatever, on the other hand g <C-a> can increment numbers with given value (aka step) and have it's "internal counter".
Create a map for #DmitrySandalov solution:
vnoremap <silent> <Leader>n :<C-U>let i=1 \| '<,'>g/^/s//\=i.'. '/ \| let i=i+1 \| nohl<CR>

How to replace spaces with A[ in emacs

I have an input file like this
line 1
line 2
line 3
There are four spaces before all the lines I want to replace.
I want the end result to be:
A[line 1
A[line 2
A[line 3
(Its funny, so editor doesn't want to display line by line)
I tried M-x replace-regex ^[]+ -> A\[, but get error invalid regex "Unmatched [ or [^"
I tried M-x replace-regex ^[]+ -> A[, but get same error.
Replacing the [ is a problem. How to fix this?
Try
M-x query-replace-regex RET ^ RET A[ RET
^^^
notice the 4 spaces here
In your examples you are missing a space inside the character class: ^[ ]+
Prettier would probably be: ^[[:space:]]+ for every kind of white-space.
Rectangles give you another way to deal with these issues. Cua-mode provides an even nicer system. To use it, first add the following to your .emacs:
(setq cua-enable-cua-keys nil)
(cua-mode)
Then, with point at the * in the following:
* line 1
line 2
line 3
Hit C-enter, and use the arrow keys (or C-n and C-f) to move point to:
line 1
line 2
*line 3
The blanks will all be highlighted. Type A[ to insert the characters. Then hit C-w to delete the spaces.
M-x query-replace-regex RET ^ + RET A[ RET
as it's a single kind of characters to replace, brackets or class are not needed

How can I do this search&replace with vim?

I have a text file containing these lines:
options[15]=new Option("text1","25");
options[16]=new Option("text2","23");
options[17]=new Option("text3","12");
(and more...)
How can I replace each line with text# ?.for example first line should be replaced by text1, second line with text2,etc...
As an alternative to :s and using a macro, I sometimes find :normal to be very pleasant.
%norm df"f"d$
We can short this up but using ; motion which will repeat the f" motion and use D which is the same as d$
%norm df";D
Read :h /\(
%s/\Voptions[\d\+]=new Option("\(text\d\+\)","\d\+");/\1/g
It looks like you already have them in the source. Assuming that you have the lines like this:
options[15]=new Option("text#","25");
...
And you want to change to this:
options[15]=new Option("text15","25");
...
Here is what you do:
change the first line to text1
yank "text1"
create a macro (qq)
/text
dw
p
ctrl+a
b
vwly
q
Then if you have 25 lines: do 23#q
23 invocations of the macro since you have done the first two manually.

Getting text that is on a different line, with ex in Vim

Let's say I have the following text in Vim:
file1.txt
file2.txt
file3.txt
renamed1.txt
renamed2.txt
renamed3.txt
I want a transformation as follows:
file1.txt renamed1.txt
file2.txt renamed2.txt
file3.txt renamed3.txt
What I have in mind is something like the following:
:1,3 s/$/ <the text that is 4 lines below this line>
I'm stuck with how to specify the <the text that is 4 lines below this line> part.
I have tried something like .+4 (4 lines below the current line) but to no avail.
You can do it with blockwise cut & paste.
1) insert space at the start of each "renamed" line, e.g. :5,7s/^/ /
2) Use blockwise visual selection (ctrl-v) to select all the "file" lines, and press d to delete them
3) use blockwise visual selection again to select the space character at the start of all the renamed lines, and press p. This will paste the corresponding line from the block you deleted to the start of each line.
:1,3:s/\ze\n\%(.*\n\)\{3}\(.*\)/ \1
explained:
\ze - end of replaced part of match - the string matched by the rest of the pattern will not be consumed
\n - end of current line
\%(.*\n\)\{3} - next 3 lines
\(.*\) - content of 4th line from here
This will leave the later lines where they are.
I would make a macro for this really. Delete the lower line, move up, paste, Join lines, then run the macro on the others. The other method I think would be appropriate is a separate script to act as a filter.