vim: left align consecutive lines with the current line - vim

Often times when I paste into vim I get cascading indents that are quite frustrating to fix. The result will look something like this
This is line one
This is line two
This is line three
This is line four
I'd like to know if there is a way that I could tell vim to align lines two through four with line one. If line one text is starting at cursor position 6 is there a way to say "make the next ten lines also start at position 6?"

To correct this cascading indentation, you can re-indent a block using =. Select a visual block and type = or supply a motion: =4j to re-indent the next 4 lines.
You might avoid the cascading indentations by setting paste before pasting: :set paste. After the paste :set nopaste.

You can use :set paste to avoid this when pasting in text. And you can set the indent level for a range with left.
:<range>left3
E.g.
.,+4left3
Will set the indent of the next 4 lines to 3.
Note: Range can be defined in visual mode, just select some lines with S-v and then press :left4

Before pasting, do :set paste, after pasting, do :set nopaste.
Or use Vim's built-in paste commands with the clipboard register:
"+p (paste after the cursor or below the line)
"+P (paste before the cursor or above the line)
See :help 'paste' and :help registers.

Related

Why does VIM paste 1 line below where expected?

A simple recipe to demonstrate the behavior of pasting in Vim/Vi...
Run vim
Enter insert mode
Add 3 different lines of garbage to your file
Exit insert mode (press escape)
Move the caret to the second line
Enter the command dd (delete line)
Enter the command p (paste)
Notice the pasted line is inserted below where you might expect it to be placed.
Why is Vim/Vi programmed with this behavior, and is there any way to change it? (ie: Have the line pasted one line above the default position.)
Use P. It will paste above the cursor instead of below.
p will paste below the current line, P will paste above the line. If you want you can swap the behaviors of P and p add the following to your vimrc.
nnoremap p P
nnoremap P p

How do you select text in vim?

In most text editors, I can select text by clicking and dragging with my mouse, and then using Ctrl-C to copy that text, or Backspace to delete it.
However, since vim runs in the console, if I highlight some text with the mouse, my vim commands don't affect what I have selected.
What is the equivalent way to select text in vim?
In vim, text is selected by entering Visual mode. This can be done in multiple ways.
v (lower case v) begins regular Visual mode, and works similar to selecting text with a mouse. Use h and l to expand the selection left and right to include more words, and use j and k to expand the selection to the lines below and above.
V (upper case v) begins linewise visual mode. This selects entire lines of text at a time. Use j and k to expand the selection up and down.
Ctrl+v(lower case v) enters block visual mode. This selects text in a block format, allowing you to select parts of multiple lines without including the entire line. Use hjkl as usual.
As #FDinoff suggested, if your terminal emulator supports it, you can even specify visual selections with the mouse by enabling mouse input with :set mouse=a.
Once you have selected the text you want, you can use all sorts of commands on them. Some of the more useful ones are:
Escape visual mode
delete the text
yank (copy) the text
paste your clipboard onto the text, replacing it
change the text, which deletes it and sets your cursor for typing
replace the text with the next character you type
yq/p search for the text elsewhere in your document
You can learn more about Visual mode by typing :help v while inside vim.
Hightlight yanked text
First of all I would like to recommend highlight yanked text:
https://github.com/machakann/vim-highlightedyank (vim and neovim)
This is useful because it will give you a visual hint of what you have just copied.
For neovim:
augroup highlight_yank
autocmd!
au TextYankPost * silent! lua vim.highlight.on_yank({higroup="IncSearch", timeout=700})
augroup END
The vim philosophy goes way beond selecting, copying etc.
Start spending more time reading about vim/neovim and you will not going back to any other editor.
Nice to meet you dear "text-objects"
Read more about them here
Copy a whole paragraph to the clipboard:
"+yip
"+ .................... clipboard register
y ..................... copy
ip .................... inner paragraph
Copy the whole file to the clipboard
:%y+
Test some vim commands from the clipboard
:#+
The above command allows you to run functions and vim commands even if did not pasted them into your vimrc, there are some exceptions but in general it will work.
You can define your own text-objects
" vim line text-objects
xnoremap al :<C-u>norm! 0v$<cr>
xnoremap il :<C-u>norm! _vg_<cr>
onoremap al :norm! val<cr>
onoremap il :norm! vil<cr>
So you can use vil or dil
Sometimes you don't need to select to copy
If you wan to copy the second line to the end of the file you can do:
:2t$
If you want to move lines 4-7 to the beggining of the file you can do:
:4,7m0
copy from mark a to mark b:
ma .................. mark current line as mark a
Jump to a second place in your file and then
mb .................. mark current line as mark b
finally:
:'a,'by+
from mark a to mark b copy to the clipboard
Diving into advanced vim:
What is your most productive shortcut with Vim?

how can I do the paste in vim without troubled by indent issue

aaa
bbb
ccc
ddd
When I using copy the above lines from a file and pasted by "right click the mouse and select paste option and left click the mouse" into a file under edit by Vim in the insert mode, I get
the following:
aa
bbb
ccc
ddd
I think it is due to some indent related settings in Vim.
Before pasting, do :set paste. Afterward, do :set nopaste. See :help paste for more details.
This is because what you're doing is essentially like just typing the text into Vim character-by-character, and so it does everything it would normally do.
The * register represents the system clipboard, so you can paste from it like so:
"*p
This assumes your Vim is compiled with support for the system clipboard. You can test if it is by running vim --version | grep '+clipboard'.
:set paste before pasting, then :set nopaste afterward to restore normal behavior.
You can use :set paste and :set nopaste to toggle paste mode.
In addition, you can use key combinations to make it easier. Update your .vimrc config file:
let mapleader = "," "map leader to do extra combination.
map <leader>pp :setlocal paste!<cr>
Now when you can input ,pp to toggle paste mode on and off.
I have this on my .vimrc
set pastetoggle=<f5> "for better pasting from clipboard
You can use F5 to active the paste toggle and F5 to disable it.
Or just put vim into insert mode (press a or i) before you "right click the mouse and select past option and left click the mouse".
The first "a" in your paste is doing that and so not being included in the paste.

Vim: copy/delete line from the first none blank character to the last none blank character

Most time when I copy or delete some code in Vim use yy or dd, I also got the indent spaces. Is there a quick command that I can yank a line without leading or trailing spaces?
I'm not a wizard, but:
^v$gey
works for me. You can always make an alias.
EDIT: here's a better one that doesn't rely on visual mode.
^yg_
There's another way to solve your implied problem. After you yank line(s) into the buffer, you can then paste them using the appropriate indent using ]p or ]P. These paste commands automatically adjust the indent of the pasted line(s) to match the indent of the line where the cursor is.

How to duplicate a selection and place it above or below the selection

If I have something selected in Vim in visual mode, how can I duplicate that selection and place it below or above the selection?
Press y to yank what you've got selected visually, then p to paste below the cursor or P to paste above it.
And since you asked about pasting below the selection block, I'll copy what michael said below: After you y to yank, use '> to move it to after the selection block, and then p to paste.
Since I do this a lot (select a block, yank, go to end of last visual selection, paste) I set up a visual block shortcut under CTRL+P (prior to this, CTRL+p seems to be the same as j in a visual block).
vmap <C-p> y'>p
Now it's just making a visual selection and pressing CTRL+p.
In addition to the V...yp combo you might want to know about some jumps '< and '> to get to the last character of the previous visual mode text. Specifically, if you want to paste below you'd go V...y'>p. If it's a long multiline it may be handy.
It's one of those jumps you may find handy if you're doing this a lot.
Use y to yank (copy) the selection into a buffer.
Use p to paste the selection where you want it to be.
You have two options:
yy which copies the current line, then p to paste.
Make a selection, with v for example, then copy with y and paste with p.
Do you want to copy/paste the whole line? If so, get out of visual mode, then use yy to yank the whole line, then p to paste.

Resources