96
votes
Accepted
How can I yank (copy) the single character on which the cursor rests?
Composing the yank operation with the so often used "one character to the right" motion should work: yl.
BTW (to comment on a OP comment), for me » can be yanked in this manner, in gVim or terminal ...
32
votes
How can I easily move a line?
If I want to swap one line with the line above I usually do the following
ddkP
Explanation
dd will delete the line and add it to the default register.
k will move up a line (j would move down a ...
31
votes
Accepted
How to replace inner text with yanked text
You can use vi"p when inside "". This can replace text in yank register, so it matters when you want to use original yanked text more than once.
EDIT:
Additional info from Octaviour comment regarding ...
30
votes
Accepted
Delete from cursor till nth occurence of character x
You can do :
wd4/x<Enter>
If you start on the top left of your text
Explanation
w : move to beginning of next word
d : delete
4/x<Enter> : until the 4th occurence of x
If you don't ...
28
votes
Accepted
Does "dap" delete everything?
"dap" does not actually delete everything. For example, try putting this into your buffer:
1
1
2
2
3
3
Then put your cursor on either '2' and type "dap". It'll leave the '1's and '3's.
This is ...
27
votes
Accepted
How do I copy a file in netrw?
You cannot use y, p etc., to copy files in netrw - it comes with its own commands. See :h netrw-mc:
MARKED FILES: COPYING netrw-mc {{{2
(See ...
25
votes
Accepted
Delete from cursor till first character x (spanning multiple lines)
Use / for forward search:
d/x<CR>
and ? for backward search:
d?x<CR>
23
votes
Accepted
Why did p paste a copied line after the current line, instead of after the current word?
When you deleted the line using dd, you performed a linewise delete.
The p command pastes after the cursor position. Since the default register was populated with linewise content, that means it ...
22
votes
How can I yank (copy) the single character on which the cursor rests?
Usually I press vy (visual yank), sometimes xu (delete undo) too. Also , if you want to yank the char before your cursor, instead of lvy, you can press Xu, I think that's the reason why I press xu for ...
22
votes
Accepted
How to delete all white spaces until first character?
You can either visually select the lines and use
:'<,'>s/^\s*//
Which means 'substitute all of the whitespaces following the first column of the line by nothing'
Or go on the first line, use
...
22
votes
I Have a Problem with Premature Yanking
The + and * registers can be used to access the clipboard; and you can assign values to registers with let @<register-name>. For example if you've pressed y to yank text to the "0 register:
...
21
votes
Accepted
Why is Y a synonym for yy instead of y$?
I found a paper "An Introduction to Display Editing with Vi" by William Joy (vi creator) and Mark Horton (vi maintainer since 1979).
From the paper it is clear that the default Y behavior is not a ...
21
votes
Why is Y a synonym for yy instead of y$?
Y was the yank command of the first vi version (ex-1.1, January 1, 1978). This version did not have the yy cammand. ex-2.2 (May 6, 1979) did have both yy and Y. So actually yy is a synonym for Y (Y ...
20
votes
Accepted
Delete to EOL without entering insert mode
Yes. You want to use the "delete" operator instead of the "Change" operator. The delete operator is
d<motion>
and it deletes everything that <motion> moves over.
In this case, the ...
19
votes
Accepted
How to copy across terminals?
You can check if Vim is built with clipboard support with:
:echo has('clipboard')
If the output is 1, the following should be enough to achieve your goal:
"*y in source Vim
"*p in destination ...
19
votes
How to delete all white spaces until first character?
In addition to statox's methods, you can:
Position the cursor at the beginning of the leading whitespace and type dw
Position the cursor anywhere in the leading whitespace and type diw
Position the ...
18
votes
Accepted
How to copy first four characters of every line to the end of that same line?
:% s/\v^(\d{4})(.*)$/\1\2 \1/
is one way to do it
\v magic option, to avoid having to escape grouping ()
^ start of line
\d{4} match exactly four digits
.* rest of line
\1 \2 has the matched ...
18
votes
Accepted
Paste visual block selection on its own lines
Try this:
:put! "
:put: insert the contents of the specified register
!: insert before the current line (the default is after)
": the unnamed register (check :help registers for details)
You could ...
18
votes
How to delete all white spaces until first character?
This works for me.
esc
: to enter vim command mode
%s/^\s*//g
Meaning:
%s/<REGEX TO REPLACE>/<REPLACEMENT TEXT>/ for string substitution
g for global (all lines)
Regex matching ...
17
votes
Delete from cursor till nth occurence of character x
You can do this by typing
<n>df<x>
where:
<n> is the number of occurrence of particular character
df<x> means delete till you find the occurrence of character x
16
votes
Accepted
Copying multiple words (from different lines) and paste them at once
Use an uppercase register when yanking (copying):
"Ay
"A says to append to the a register, as opposed to "a which would replace the contents of the a register.
Once you've copied everything into ...
16
votes
Accepted
How can I delete in Vim all text from current cursor position to the end of file without using End key?
With your cursor in starting position to begin, you can do: vG$d
v ( enter visual mode )
G ( go to bottom of file )
$ ( go to end of line )
d ( delete selection )
15
votes
Accepted
I Have a Problem with Premature Yanking
I’m going to go ahead and infer from your use of the word “select”, that you yanked via visual mode.
If that’s the case, then you can use gv to reselect the previous selection. You can then yank to ...
14
votes
How can I copy text to the system clipboard from Vim?
If you can't figure out how to get +clipboard to work, which I couldn't for some reason, then you can do this.
Assuming you only want to copy a section of the file, I do shift+v to go into visual ...
14
votes
Accepted
Yanking and pasting while incrementing
Put your cursor on the (1, 1), line then type :
qaVypt)<C-a>q135@a
Explanation :
qa begins the recording of a macro inside the register a (you could choose another letter).
Vyp duplicates ...
14
votes
How to replace text inside () with previously yanked text?
You can use visual selection to select the text to replace, and then paste over it as follows:
vi(p
vi( selects Baz (because it is enclosed in parenthesis), p pastes Fnord over it.
However, this ...
14
votes
How can I easily list the content of the registers before pasting?
This peekaboo plugin is pretty good for for previewing registers. When you press " or @ in normal mode, or ctrl-r in insert mode, a split will open to show you the register contents.
Preview from ...
14
votes
Accepted
ci) - also delete the surrounding parens?
You can use the a variant: ca), ca].
The i is "inner", whereas the a is for "a" as in "a word", "a sentence", or "a [] block". I usually think of ...
13
votes
Accepted
How to paste visual block starting with another line
When you copy some text, it goes into a register. The text inside a register has a type: characterwise, linewise or blockwise.
This type determines in which way the text will be put.
In your example,...
13
votes
Vim on WSL: synchronize system clipboard? (set clipboard^=unnamed)?
I'm aware this is a very old question but I recently found a good solution. After much frustration, I came across these ways to access the windows system clipboard by copying and pasting.
By copying, ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
cut-copy-paste × 487register × 49
os-clipboard × 41
visual-mode × 33
key-bindings × 31
neovim × 28
vimrc × 25
visual-block × 22
cursor-motions × 20
vimscript × 18
normal-mode × 15
replace × 13
terminal × 12
indentation × 12
gvim × 11
cursor-movement × 11
search × 10
command-line × 10
insert-mode × 10
linux × 9
macos × 9
repeated-commands × 7
mouse × 7
substitute × 6
tmux × 6