Is there an equivalent to o-<esc>-p in Vim? - vim

When I yank a whole line and press p, Vim creates a new line to put the yanked text, because of the newline characters. But if I yank part of a line is there a way to put that yanked text in a new line? I currently do o ESC p. Is this the only way?

Here's your answer from Vim help:
:[line]pu[t] [x] Put the text [from register x] after [line] (default
current line). This always works |linewise|, thus
this command can be used to put a yanked block as new
lines.
The cursor is left on the first non-blank in the last
new line.
The register can also be '=' followed by an optional
expression. The expression continues until the end of
the command. You need to escape the '|' and '"'
characters to prevent them from terminating the
command. Example: >
:put ='path' . \",/test\"
< If there is no expression after '=', Vim uses the
previous expression. You can see it with ":dis =".
:[line]pu[t]! [x] Put the text [from register x] before [line] (default
current line).
But colon-p-u-enter is more keystrokes anyway =/

Theres o(c-r)0 as you can always paste from registers while your in insert mode but thats only if you want to remain in insert mode after the paste

Related

VIM: Why is the dot operator working differently?

I want to understand what gets stored in Vim's dot(.) register.
Consider the following text:
This is Line one
This is Line two
This is Line three
With the cursor on the first line, if I do A;<esc> I can repeat the same action for the next line by j.
However, if I do the action like removing the last character on the first line by $x and then try to repeat it for the next line by j., it is not removing the last character of the line, instead it just deletes the character under the cursor.
So why is dot command able to remember the position in the first example A;<esc> whereas not able to do the same for $x ?
From :help .:
. Repeat last change, with count replaced with [count].
Also repeat a yank command, when the 'y' flag is
included in 'cpoptions'. Does not repeat a
command-line command.
With A;, the change is to insert a ; at the end of the current line. A moves the cursor and switches to insert mode.
With $x, the $ first moves the cursor, then the x command deletes a character. They are not linked together, so the change is only the deletion of the character at the current cursor position.
(Put another way, the motion is only part of the change if the command takes a motion operator after the command, like d, or if the motion is implicit in the command, like with A.)

How do you insert a newline after every 80 characters in vim?

I have a long line of characters in vim. I'd like to insert a newline after 80 characters. How do I do that?
:%s/.\{80}/&\r/g
%: process the entire file
s: substitute
.: matches any character
{80}: matches every 80 occurrences of previous character (in this case, any character)
&: the match result
\r: newline character
g: perform the replacement globally
Using regular expression:
:%s/\(.\{80\}\)/\1\r/g
Using recursive Vim macro:
qqqqq79la<Enter><esc>#qq#q
qqq Clear contents in register q.
qq start marco in register q
79la<Enter> Carriage return after 80 characters.
<esc> go back to normal mode
#q run macro q which we are about to create now.
q complete recording macro
#q run macro
You can also modify this approved answer to only insert the newline at the first space occurring after the 80th character:
%s/\(.\{80\}.\{-}\s\)/\1\r/g

Select entire line in VIM, without the new line character

Which is the shortest way to select an entire line without the new line character in VIM?
I know that SHIFT + v selects the entire line, but with new line character.
To do this I go to the line and I press:
^ (puts the cursor at the start of the line)
v (starts the visual select)
$ (selects the entire line including new line character)
Left (unselects the new line character)
I also know that I can create a recording that does such a thing. But I am asking if is there any built-in shortcuts...
Yes, g_ is what you are looking for. g_ is like $, but without the newline character at the end.
Use 0vg_ or ^vg_, depending if you want to copy from the beginning of the line, or the first character on the line, respectively.
No, there is nothing built-in that does the job. That's why people have even created plugins to address the need.
Probably the most popular choice is textobj-line. With textobj-line you get two new text objects, al "a line" and il "inner line". Then,
vil selects the printable contents of the line (like ^vg_),
val selects the entire line contents (like 0v$h).
Both do not include the newline in the selection.
Pretty handy plugin if you ask me. And it works with operators, too.
By request, the installation:
With plain Vim:
Get the latest textobj-user and extract its directories into ~/.vim.
Get the latest textobj-line and extract its directories into ~/.vim.
Generate the help tags :helptags ~/.vim/doc.
With a plugin manager (recommended): just follow the usual installation procedure for your plugin manager, and don't forget to install the textobj-user dependency as well.
0v$
^v$
0vg_
^vg_
$v0
$v^
g_v0
g_v^
all do the job with different conceptions of what a line is (from first column or from first printable character, to last character or to last printable character). You can create a custom mapping if you like.
Note that selecting text is often unnecessary in vim.
Adding on to the answer by #glts, you can replicate the functionality of the textobj-line plugin using only vanilla vim mappings, no plugin installation required.
To do so, add the following to your .vimrc
vnoremap al :<C-U>normal 0v$h<CR>
omap al :normal val<CR>
vnoremap il :<C-U>normal ^vg_<CR>
omap il :normal vil<CR>
The al text object (short for 'a line') includes all characters in a line, but not the terminating newline. This includes all white space.
The il text object (short for 'inside line') goes from the first non-blank character to the last non-blank character.
Commands such as yil,val, and cil work as expected.
If you want to copy line into the buffer, you can use Du, which will delete from the cursor position to the end of line with D, and then revert changes with u. Text will be copied to the buffer without new line symbol.
Redefine $ for visual mode
The unwanted selection of the linefeed in visual mode can be permanently eliminated by adding the following visual mapping to .vimrc:
vnoremap $ g_
This replaces the standard behaviour of $ in visual mode for the move towards right before the linefeed g_.
You can still a mapping to what you want, e.g.:
nnoremap <leader>v 0v$
Another solution $ will be working as you want it to
:vnoremap $ $h
maps your original $ command to new one
Not exactly an answer to your question, but I wonder if you can skip selecting the line and do directly what you want next:
If you want to change the line, just cc
If you want to yank the line, 0y$ (note $ here does not capture the line break because it does not move over it in normal mode, unlike in visual mode)

Do vim or gVim commands exist to copy text between parentheses?

In the following code snippet, if I go to the first open parenthesis ( of the line beginning with (spit
(defn missing-accts
"Prints accounts found in one report but not the other."
[report-header mapped-data out-file]
(spit out-file (str "\n\n "
(count mapped-data)
" " report-header
"\n\n") :append true)
.
.
.
vim highlights the first ( and closing ) parentheses.
Is there and, if there is, what is the vim command that would yank the entire spit command?
Thanks.
The sequence
va(
will highlight from the opening to closing brackets inclusively, and a y will then yank that. Note unlike the % command, you don't have to be positioned on the bracket - you just need to be inside the clause.
Note that
vi(
would highlight everything inside the brackets, but not the brackets.
You can do this for braces too ({ instead of () and XML tags (t - presumably for tag)
Vim does have such a command, and fortunately it is very simple. Just type y%.
The reason this works is that % is what Vim calls a movement command. It moves from one delimiter to the matching delimiter -- in your case from the opening parenthesis to the closing one. The y command yanks a single line into Vim's buffer if invoked as yy, but the second y is not required. Instead, one can issue a movement like %, whereupon Vim will yank the text moved over. Thus, y%.
use % with y. press "y" once then "%" ,your cursor should be on "(" when you type the command.

vim substitute within block selection on one line

I have the following expression in a latex file
\begin{dfn} \tag{Diagram $𝗗$ over a trisp $\Delta$}
\label{dfn:Diagram D over a trisp Delta}
now i want to substitute all the spaces in the brackets on the second line with :.
my idea was to visual select the expression by v% while being on one of the brackets and then :s/\ /:/g but this replaces all whitespaces and results in
::::::::::::::\label{dfn:Diagram:𝗗:over:a:trisp:Delta}
which results in the task of removing the : before the \label{…}
as vim is a very powerful editor - there should be an easier way, that I don't know yet.
Go to the first {, press v, press %. This will visually select the text between { and }. Then press : and enter s/\%V\ /:/g in your minibar. See this for explanation:
http://vim.wikia.com/wiki/Search_and_replace_in_a_visual_selection
Note that otherwise g will replace all on the current line.

Resources