Normally, I am using <shift-v> and <up(or)down> to select some lines of code in vim. Then I use <y-y> to yank the lines. Then, I do <p> for pasting the code at the desired location.
I used this step for a long time and had no issues. Recently, I started noticing that when I yank more than 10 lines and paste them, only 2 of the lines are pasted.
What could be the reason for this? If anybody knows alternate ways of selecting multiple lines, yanking and pasting it, please let me know.
I have never seen where lines get lost when I paste them. However, when you use visual mode (shift-v), usually you only need the first "y" to yank lines. It's likely that when you press the second "y" and move down, you are actually yanking the top two lines (which is what is supposed to happen). You probably just need to stop pressing "y" twice. (Hint: press "y" once in visual mode, or twice when you want to yank the current line in normal mode).
You can yank multiple lines in a variety of other ways. For example, 14yy will yank 14 lines. If you use gvim, you can use the mouse cursor to select text. You can also do y14j to yank the next fourteen lines, since y, followed by a movement command, copies everything in that movement command.
Related
My vim seems to be acting weird today.When I try to copy paste it is carrying out the vim keyboard shortcuts related to the characters I have copied and pasted. e.g. If i copy and paste two letter ks they will not paste but the cursor will jump up two lines. I think it has something to do with SHIFT+CTRL+V taking me out of insert mode and into normal mode. Then paste is putting the kk as input into normal mode.
How do I get back to the correct copy paste functionality/ stop making shift+ctrl+v exit insert mode? Thanks!
My cluster terminal went into bracketed paste mode while in vim somehow.
Typing reset into the terminal outside of vim fixed it.
I usually visual select a block and copy/cut it somewhere else, then I found myself always formatting the pasted text, so is there a way to quickly visually select the text again.
Every command that modified the buffer (and yanks) will set the '[ and '] marks around the changed area. So you can reformat your pasted block via:
`[v`]=
Some people go so far as to use the following mapping to reselect last modified chunk of text:
nnoremap <expr> gV '`[' . getregtype()[0] . '`]'
With this mapping you can just do gV= and format your just pasted text.
However if you have the unimpaired.vim plugin and you are pasting linewise you can use the =p command it provides. This will paste and then reformat the text in one go. It also provides some other alternative paste commands >p for example will paste one indent level deeper.
For visual mode, gv is the standard way to reselect the previous area.
If you want to toggle between start and end positions of the area, just press o.
As other answers have mentionned it, you can apply standard = command on this reselected area.
It works well after a p or P paste.
The advantage is that you do not need any custom mapping.
The way I use is straightforward. The cursor is at the beginning of the pasted text after pasting. The press V to switch to visual selection, the press '] to go the end of the pasted.
They are 3 key presses. If it is too long then you can do mapping for p
map p pV'[
map P PV'[
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?
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.
Is there a way I can select a portion of multiple lines in vim and then paste them at the end of another block of lines. For instance if I want everything after the equal sign from:
qw=12345
er=23435
and pasted into:
ty=
ui=
What would I have to do?
This seems real simple, but for some reason, I am completely stuck.
You are looking for block selection mode.
Please note the different keybinding for Windows platforms.