I am trying to create a mapping that will allow me to select a line of text non linewise so I can paste at curosr(not before or after) without introducing spaces that may have preceeded the line where it was yanked from.
This is what I was trying to do
"copy non linewise
nmap <leader>yy 0y$
nnoremap <Leader>yy ^yg_
^ and g_ are similar to 0 and $, respectively, but they exclude blank characters.
My UnconditionalPaste plugin has a gcp / gcP mapping that not only flattens any number of yanked lines into a characterwise paste, but it also removes preceding and trailing whitespace.
The advantage of "casting the contents" only on paste is that you don't need to think about the future use while yanking, and as the original contents are preserved, you can paste the same register contents in various ways (linewise, characterwise, and any of the other flavors that my plugin supports).
Alternatively, you could remap Y to yank until the end of string (similar to C):
noremap Y y$
Now ^Y would do the job.
Related
I want to create a command that replaces the word under the cursor by another one, say I have the word have under my cursor and it replaces it with the word had and vice versa. How to accomplish that?
This could easily be accomplished by ciw and then entering the word that you would like to replace it with. Assuming that you want to replace words with different values.
Another solution is you could use a plugin like switch.vim. You would have to define the words/regular expressions you would want to replace.
If you actually want to do this with longer words, then this method may help. First, position the cursor on the word "had" and use yiw to yank (copy) it into the #0 register (and also the unnamed register, but we are about to overwrite that). Then move the cursor to "have" and use ciw<C-R>0<Esc> to replace it with the yanked word.
Do not type <C-R> as five characters: I mean hold down the CTRL key and type r. Similarly, <Esc> means the escape key. Do type each as five characters if you want to make a map out of it, for example
:nmap <F2> ciw<C-R>0<Esc>
If you want to replace all occurrences of the word under the cursor, you can add this into your _vimrc:
" search and replace all occurrences of word under cursor
:nnoremap <C-h> :%s/\<<C-r><C-w>\>/
:inoremap <C-h> <ESC>:%s/\<<C-r><C-w>\>/
Usage of this:
1) Press Ctrl+h (under the cursor is the word "have"), and Vim will enter this in the command line:
:%s/\<have\>/
2) Now just complete the replacing statement:
:%s/\<have\>/had/g
3) And press ENTER...
The SwapIt - Extensible keyword swapper allows you to configure sets of words (e.g. have and had) and toggle them via <C-a> / <C-x> mappings.
I've got a file (LaTeX) which contains lines I wish to comment out.
The regex that I use after visually-selecting the relevant block is :s/^/%/g, which works fine. However, vim then highlights every matching occurrence of the first part of the regular expression used in the replace, (highlights the first character on the beginning of every line).
The selection changes if I do another search, or another search-and-replace, but I can't work out how to turn it off without doing a 'useless' search.
It's particularly annoying if I search for whitespace (because having every '' highlighted in a text file is visually annoying).
How do I de-select the matching strings after the search-and-replace has been completed?
:nohlsearch will stop highlighting it but keep it as the active search pattern. (It will start being highlighted on n etc.)
:let #/="" will clear the search pattern register (so that n etc. won't work).
A common thing I've seen in Vim is map <Leader><Space> :noh<CR>; this has the result that (assuming the default leader, backslash) \Space will stop highlighting the current match.
Just search for a string that is not on the page:
/poop
:nohlsearch will remove highlighting from the current search. Highlighting will return on your next search.
:set nohlsearch will disable highlighting for your current vim session.
If you want to disable highlighting completely, add :set nohlsearch to your .vimrc
Add that to your vimrc, and once done - press in my case <,> + enter to stop highlighting
map <silent> <leader><cr> :noh<cr>
How would I move the current line behind the line above it? Say I have:
function foo()
{
^ Cursor is here
And want to turn that into:
function foo() {
I am still new to vim, so what I do now is i[backspace][backspace]...etc. :)
Several ways:
In normal mode, kJ or kgJ or VkJ or VkgJ (the last two commands do the same in visual mode).
k will go to previous line, and J or gJ will merge with next line (J inserts a space inbetween, gJ just removes the EOL characters)
In command mode, :-,j or :-,j!
-, is a range that is abbreviation for .-1,. which means “from previous line to current line”
j is the ex command for concatenating lines in a range. The banged (with exclamation mark) version acts like gJ.
With a substitution: :-s/\s*\n\s*//
- means previous line
:s is probably known to you, else you should run vimtutor.
/\s*\n\s*/ is pattern for as many spaces as possible plus line terminator (matches different byte sequences according to the file format: LF, CR or CRLF) plus as many spaces as possible.
Here, replacement pattern is empty.
in insert mode, hit CTRL-W twice (each time it deletes a word, or leading whitespace on a line, or newline) (as ib. suggests, this depends on the backspace setting).
References:
:help J
:help gJ
:help k
:help range
:help :j
:help pattern
:help i_CTRL-W
Say, I have the following lines:
thing();
getStuff();
I want to take getStuff() using the yy command, go forward to thing(), placing the cursor on (, and paste via the p command, but since I yanked the whole line, p will paste getStuff() right back where it was.
I know you can first move the cursor to the beginning of that getStuff() line and cut the characters from there until its end via the ^D commands—then p will do what I want. However, I find typing ^D to be much more tedious than yy.
Is there a way to yy, but paste the line inline instead?
The problem is that yy is copying the entire line, including the newline. An alternative would be to copy from the beginning to the end of the line, and then paste.
^y$
^ Go to the first character of the line.
y Yank till
$ End of line.
// Credit to: tester and Idan Arye for the Vim golf improvements.
Use yiw ("yank inner word") instead of yy to yank just what you want:
yy is line-wise yank and will grab the whole line including the carriage return, which you can see if you look at the unnamed register ("") in :registers which is used as the source for pastes. See :help "":
Vim uses the contents of the unnamed register for any put command (p or P)
which does not specify a register. Additionally you can access it with the
name ". This means you have to type two double quotes. Writing to the ""
register writes to register "0.
An additional benefit to yiw is that you don't have to be at the front of the "word" you are yanking!
One way to simplify the routine of operating on the same text patterns
is to define mappings that mimic text-object selection commands.
The two pairs of mappings below—one for Visual mode and another for
Operator-pending mode—provide a way to select everything on the current
line except for the new line character (al), and everything from the
first non-blank character of the current line through the last non-blank
character, inclusively (il).
:vnoremap <silent> al :<c-u>norm!0v$h<cr>
:vnoremap <silent> il :<c-u>norm!^vg_<cr>
:onoremap <silent> al :norm val<cr>
:onoremap <silent> il :norm vil<cr>
Thus, instead of using yy to copy the contents of a line that
is to be pasted character-wise (and not line-wise), one can then
use the yal or yil commands to yank, followed by the p command
to paste, as usual.
A less efficient, but simple method:
v to highlight the word(s),
y to yank the highlighted word(s),
p (at the end of the line) you want to paste
I often have to paste some stuff on a new line in vim. What I usually do is:
o<Esc>p
Which inserts a new line and puts me in insertion mode, than quits insertion mode, and finally pastes.
Three keystrokes. Not very efficient. Any better ideas?
Shortly after :help p it says:
:[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.
:[line]pu[t]! [x] Put the text [from register x] before [line]
(default current line).
Unfortunately it’s not shorter than your current solution unless you combined it with some keyboard map as suggested in a different answer. For instance, you can map it to any key (even p):
:nmap p :pu<CR>
Options:
1) Use yy to yank the whole line (including the end of line character). p will then paste the line on a new line after the current one and P (Shift-P) will paste above the current line.
2) Make a mapping: then it's only one or two keys:
:nmap ,p o<ESC>p
:nmap <F4> o<ESC>p
3) The function version of the mapping (unnecessary really, but just for completeness):
:nmap <F4> :call append(line('.'), #")<CR>
" This one may be a little better (strip the ending new-line before pasting)
:nmap <F4> :call append(line('.'), substitute(#", '\n$', '', ''))<CR>
:help let-register
:help :call
:help append()
:help line()
:help nmap
You can paste a buffer in insert mode using <C-R> followed by the name of the buffer to paste. The default buffer is ", so you would do
o<C-R>"
I found that I use <C-R>" very often and bound that to <C-F> in my vimrc:
inoremap <C-F> <C-R>"
This still uses three keystrokes, but I find it easier than Esc:
o<Alt-p>
Since you're in insert mode after hitting o, the Alt modifier will allow you to use a command as if you weren't.
Using this plugin: https://github.com/tpope/vim-unimpaired
]p pastes on the line below
[p pastes on the line above
advantages:
works on all yanked text (word, line, character, etc)
indents the pasted text to match the indentation of the text
around it
2 keystrokes instead of 3 and much "easier" strokes
fast
Personally I've nmapped Enter (CR) like this:
nmap <CR> o<Esc>k
...based on this Vim Wikia article.
This way I can make newlines directly from normal mode, and combining this with wanting to paste to a newline below I'd do:
<CR>jp
You could also skip k in the nmap above, depending on what functionality you prefer from Enter, so it would just be <CR>p.
I've also imapped jj to Esc, which would also assist in this case. Esc is way too far away from the home row for how significant it is in vim.
Not shorter than the other solutions, but I do think it feels less clunky than some of them, and it has other uses too.
If you wanted to stay in the insert mode, you can do o ctrl+o p
o – insert mode and go to the new line
ctrl+o – run a single command
like in normal mode
p – paste
It's three keystrokes but you stay in insert mode and also o ctrl+o is quite fast so I personally treat it as 2.5 keystrokes.
If you're copying a whole line then pasting a whole line, use Y to yank the line or lines, including line break, in the first place, and p to paste. You can also use V, which is visual line mode, in contrast with plain v for visual mode.
I have mapping inoremap jj <ESC>. So it is easy to insert new line with ojj and Ojj and then p.
so ojjp paste new a newline. it have one more stroke then o<esc>p but ojjp is easy for me.
I found an elegant solution to this. If you are putting the yank register in your OS's clipboard (which is great anyway), with
set clipboard+=unnamed
than you can do o<Ctl-v>.
Besides being fewer strokes, this improves on both o<Esc>p and :pu because it preserves indenting: both of the other options start you at character zero on the new line.
Caveat is that this may or may not be OS dependent. All I know is that it works on recent version of OS X, but clipboard is just one of many ways to get yank in the OS clipboard.
If you want to paste in a new line and still keep indentation, create this mapping:
nnoremap <leader>p oq<BS><Esc>p
Prerequisite: you have leader mapped and you have set autoindent in your .vimrc.
Explanation: a new line is created with 'o', 'q' is typed and then back-spaced on (to keep indentation), and 'esc' brings you back to normal mode where you finally paste.
If you also want to end in insert mode, it is possible to paste while in insert mode using CTRL-R ". https://stackoverflow.com/a/2861909/461834
Still three keystrokes, but no escape, and you save a keystroke if you want to end in insert anyway.
I use the following mapping in my Neovim config:
nnoremap <leader>p m`o<ESC>p``
nnoremap <leader>P m`O<ESC>p``
A little explanation:
m`: set a mark in the current cursor position.
o<Esc>p: create a new line below and paste the text in this line
O<Esc>P: create a new line above and paste the text in this line
``: put the cursor in the original position
See :h mark for more information about marks in Vim.
This solution only seems to apply when the block of copied text starts on a new line (as opposed to grabbing a snippet of text somewhere within a line), but you can always start your copy on the last character you want to grab, then navigate to the last character at the end of line prior to the start of your desired copy block. Then when you want to paste it, place the cursor at the end of the line under which you want your text to be pasted and hit p. If I haven't screwed up the explanation, this should provide the effect you're looking for.