The shortcut Y, for some reason, does not operate the way D and C
do. Instead of yanking from the current position to the end of the
line, Y yanks the whole line. Y does the same thing as yy.
Is it possible to override this functionality ?
I want Y to acts as y$
Use a map in Normal mode:
:nnoremap Y y$
Learn how to look up commands and navigate the built-in :help; it is comprehensive and offers many tips. Pertaining to your question, :help Y offers this:
If you like "Y" to work from the cursor to the end of line (which is more logical, but not Vi-compatible) use :map Y y$.
Related
I'm using:
set clipboard=unnamed
So that cut and copied selections go to the * register, which is conveniently linked to the x clipboard (I'm using Linux).
I would like to have it also in the + register to get both middle-mouse-pasting and Ctrl-V giving the same results in other applications. I guess this might related to the difference between xclip and xclip -selection c.
For now my workaround is:
noremap Y "+y
So that I could do Yy to copy the current line to + register and then use Ctlr-V in some applications. But then I have to think in advance if I will use the mouse or Ctrl-V.
Is there a way I could have the + and * registers have the same value when I yank or when I select some text with the mouse?
I think you want to
:set clipboard+=unnamed,unnamedplus
From the help:
unnamedplus A variant of "unnamed" flag which uses the clipboard
register '+' (|quoteplus|) instead of register '*' for
all operations except yank. Yank shall copy the text
into register '+' and also into '*' when "unnamed" is
included.
Note that you need a fairly new Vim 7.3 (patchlevel 151?) for this option value.
Thinking out of the box, you can install autocutsel which sinchronizes both clipboards.
As discussed in the comments, here's a solution:
vnoremap Y ygv"+y
However that does not work, only the other way around:
vnoremap Y "+ygvy
Here's what it does:
noremap Y: Map to the Y key, using default behavior of keybindings. :h noremap
"+y: Yank into + register.
gv: Re-select previous visual selection.
y: A normal yank into the * register (in Linux).
Glad I was able to help you solve it. :)
I am trying to find an easy way to copy the word that the caret is currently on top of.
I know that I can select to the front of the word: press v e y.
But this seems crazy, I can simply press * above a word to search for it, surely there is a better way to copy the word. Maybe even in a single key press?
You can use y i w (Yank In Word). Though it is just as many keystrokes. If you are at the beginning of the word you can drop the i and use either y w or y e.
Alternatively you can map the command to a key any way you like. For example, you could put this in your vimrc file:
nmap <F8> yiw
The F8 key is right near the * key so it would be easy to remember that it acts similar to the * word highlight. This would be a single key to yank the word.
UPDATE:
Satoru.Logic's comment is definitely a good way to go. If you are not sure what <leader> means, have a look at this post.
I sometimes find document traversal to be too slow in Vim when using h, j, k, l.
Is there a way to temporally increase the number of lines / characters that these keys move the cursor? (I.e. instead of moving 1j, pressing j will move 3j)
Edit:
Solution:
:map <F8> :noremap j 3j <CR>
:map <S-F8> :noremap j j <CR>
I wanted something like this so that I can easily browse longer bodies of code that am not necessarily familiar it. This approach allows me to easily toggle between "browsing" mode and "coding" mode.
While possible (use :noremap j 3j Enter and :noremap j j Enter to restore), it may not be useful for very long to change the behaviour of these keys.
There are many ways to navigate in Vim. Of course you can advance by full screens using CtrlF and CtrlB.
You can, as you alluded to, enter a number of moves before executing the navigation.
You can also go directly to a specific line using :9Enter, for example.
If you see the text to which you want to navigate, use / or ? followed by the text.
For h and l, you can navigate word boundaries more quickly with b, w, and e, and contiguous non-whitespace with B, W, and E.
Try Ctrl+D/Ctrl+U and Ctrl+F/B (Up/Down, Forward/Back respectively).
These will traverse the document much faster than h,j,k,l.
What #Bryan Ross have suggested is absolutely right. I want to add just a thing. Use relativenumber, it will help you to use j and k more efficiently.
Answered by Jay is sufficient, I would like to add the following !
There are many different kinds of navigation possible in vim, ( where as the h, j, k, l are just line navigation ). Some more are:
screen navigation
spl navigation
search navigation
word navigation
Refer this write up to find out the short cut keys to do it: Essential Vim editor navigation
Another thing that helps is to have line numbers turned on (:set number). If you see on the screen where you need to go and see the line number, it's just G.
If you deal with code in blocks, % will move you to a matching brace, parenthesis, etc.
If you deal with files with lots of wacky characters, t, T, f, and F are very helpful.
try CTRL + Y & CTRL + E to scroll instead of moving the line
use the CTRL + B to scroll to bottom of page and CTRL + F to scroll to top of page
use the H (Height), M (middle), L (low) to move the cursor to top, middle and bottom of screen
you can also increase the speed of CTRL + Y and other keys as of this answer explained https://stackoverflow.com/a/7990810/10539792
I know there's so many vim tricks to traversal quickly, but for me is the most comfortable is a mechanical keyboard, with built in repeat rate increase when u hold a button it increase the repeat reat etc for the j or k so the traversal will be quick and smooth too. (So nice!)
But its annoying when u have your vimrc but didnt have your keyboard so not a universal solution.
I have word-wrap enabled and tend to have quite long lines.
But moving around inside a line that's actually 4 lines high with "w" is cumbersome. I keep using / to jump to the word I'm looking for, but that seems overdoing it a bit.
Any hints on how to move more quickly inside of a line?
Thanks,
MrB
You can use $, 0, and ^ to move to line endpoints and then use w and b. Also, adding a numeric argument to w and b can accelerate the process, so using 6w instead of just w can put you about to where ou need to be.
Using f and t to move to individual characters will help also. (I use this typically with punctuation. If, for example, I have four sentences on one long line 2f. will go to the end of the second sentence)
Using the ( and ) keys are an alternative way to navigate entire sentences.
Splitting out long lines into multiple lines (manually, or with set tw=72 [or 80]) can make editing them simpler. You can always join them later with J.
Something I just discovered, you can move up and down one displayed line by using gj and gk. That way, you can treat your one wrapped line as multiple lines.
If you comment on the type of data you're editing, it might make it easier for us to make suggestions.
I think you can benefit from gk and gj instead of just k and j.
Also look at 'virtualedit' for some options that allow you to cursor through 'void' areas without flicking the cursor to the next best physical character.
You might want to (temporarily)
nnoremap <buffer> k gk
nnoremap <buffer> j gj
Leave out the <buffer> part to apply this globally.
You can use ( and ) to navigate by sentence; it just looks for ., but that can be immensely helpful, especially if you don't like the sentence and want to change it: (c) will jump to the beginning of the current sentence, then change the entire sentence.
You can also use w and e, with count modifiers, to move words. 3w will move three words at a time.
You can also use f and F to search forward and backwards for a specific character. This is much more useful if you're looking for the word quite or syzygy than the. :)
My preferred strategy while jumping around long lines is to use f F and t T to zero in on the character. What makes this family of motions supercharged is that you can utilize the ; and , motions, so you don't have to count the position of character relative to cursor, but just step through them (extremely useful with
' " . etc)
Let's say we have a line:
reallyLongObjectName.longMethod().prettyPrettyLongMethod().burp();
If we need to jump to, say, the third dot from the beginning of the line, we can use either 3f. or f.;; visiting two dots and landing on third.
While the ; , style can use more keystrokes, I found it more agile and fun overall.
If you choose to go the route of remapping these:
nnoremap k gk
nnoremap j gj
here are a couple others along the same lines:
nnoremap 0 g0
nnoremap $ g$
nnoremap ^ g^
I recently started using a plugin that I find really nice to move very quickly inside a line (or the whole file).
The plugin's name is PreciseJump and you can find it here.
When you use this plugin it defines to mappings _f and _F.
If you type _f followed by x it will highlight all x characters and will replace temporarily with other characters that you can press to jump to that location. Check the script page for an illustration.
You can also move around with W B that will skip to the next space :)
G moves to the end of the document
Please notice that using "g" followed by Up or Down arrows indeed works fine, but if you have long lines and move quickly you may enter "gg" by mistake and end-up at the top of the text...! (Undo will not bring you back, and AFAIK there is not one-key-pressed way to go back to where you were.)
It happened to me too many times.
What I did was, and I suggest you, to modify (or create) your "~/.vimrc" and add these two lines:
map <C-Up> g<Up>
map <C-Down> g<Down>
This will map you control-up and control-down to the movements commands. Will make mistyping "gg" impossible and is perfectly coherent with control-right and control-left to move around long lines.
If you add these other two lines, you can use the same command in insert mode (!)
imap <C-Up> <C-[> g<Up> i
imap <C-Down> <C-[> g<Down> i
(VIM is great !)
Greg Ruo
Emacs has a useful transpose-words command which lets one exchange the word before the cursor with the word after the cursor, preserving punctuation.
For example, ‘stack |overflow’ + M-t = ‘overflow stack|’ (‘|’ is the cursor position).
<a>|<p> becomes <p><a|>.
Is it possible to emulate it in Vim? I know I can use dwwP, but it doesn’t work well with punctuation.
Update: No, dwwP is really not a solution. Imagine:
SOME_BOOST_PP_BLACK_MAGIC( (a)(b)(c) )
// with cursor here ^
Emacs’ M-t would have exchanged b and c, resulting in (a)(c)(b).
What works is /\w
yiwNviwpnviwgp. But it spoils "" and "/. Is there a cleaner solution?
Update²:
Solved
:nmap gn :s,\v(\w+)(\W*%#\W*)(\w+),\3\2\1\r,<CR>kgJ:nohl<CR>
Imperfect, but works.
Thanks Camflan for bringing the %# item to my attention. Of course, it’s all on the wiki, but I didn’t realize it could solve the problem of exact (Emacs got it completely right) duplication of the transpose-words feature.
These are from my .vimrc and work well for me.
" swap two words
:vnoremap <C-X> <Esc>`.``gvP``P
" Swap word with next word
nmap <silent> gw "_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<cr><c-o><c-l> *N*
Depending on the situation, you can use the W or B commands, as in dWwP. The "capital" versions skip to the next/previous space, including punctuation. The f and t commands can help, as well, for specifying the end of the deleted range.
There's also a discussion on the Vim Tips Wiki about various swapping techniques.
In the middle of a line, go to the first letter of the first word, then do
dw wP
At the end of a line (ie the last two words of the line), go to the space between the words and do
2dw bhP
From the handy Equivalence of VIM & Emacs commands
You could add shortcut keys for those by adding something like the following to your vimrc file:
map L dwwP
map M 2dwbhP
In that case, SHIFT-L (in command-mode) would switch words in the middle of the line and SHIFT-M would do it at the end.
NB: This works best with space-separated words and doesn't handle the OP's specific case very well.
There's a tip on http://vim.wikia.com/wiki/VimTip10. But I choose to roll my own.
My snippet has two obvious advantages over the method mentioned in the tip: 1) it works when the cursor isn't in a word. 2) it won't high-light the entire screen.
It works almost like emacs 'transpose-words', except that when transposition is impossible, it does nothing. (emacs 'transpose-words' would blink and change cursor position to the beginning of current word.)
"transpose words (like emacs `transpose-words')
function! TransposeWords()
if search('\w\+\%#\w*\W\+\w\+')
elseif search('\w\+\W\+\%#\W*\w\+')
endif
let l:pos = getpos('.')
exec 'silent! :s/\(\%#\w\+\)\(\W\+\)\(\w\+\)/\3\2\1/'
call setpos('.', l:pos)
let l:_ = search('\(\%#\w\+\W\+\)\#<=\w\+')
normal el
endfunction
nmap <silent> <M-right> :call TransposeWords()<CR>
imap <silent> <M-right> <C-O>:call TransposeWords()<CR>
You can use dwwP or dWwP as Mark and CapnNefarious have said, but I have a few notes of my own:
If the cursor is on the first letter of the second word, as in the example you gave, you can use dwbP (or dWbP to handle punctuation);
If the cursor is in the middle of the word, you can use dawbP/daWbP.
There's a transpose-words script on vim.org that works beautifully.