I would like to wrap a paragraph. I have tried the following in my .vimrc:
nnoremap [w vipga
But there are two problems:
It does not work for me (even though the key sequence works for me when I type it manually).
I would like my cursor position to stay the same.
The command to reformat is gq, not ga (you may have remapped that, but then would need to use :nmap for the mappings to apply).
To maintain the cursor position (as much as possible, as the reformat may make the current position invalid), you can set a mark and then jump back to it. Ergo:
:nnoremap [w m`vipgqg``
Related
I would like to define a command, gv, which jumps to definition in a split window. I'm aware that gd is the command to jump to definition but in the existing window.
I tried this mapping:
nnoremap <silent> gv :vsplit<CR>gd
But it didn't work. It opened up the exactly same file in a split window, without actually jumping to definition. Seems like the gd command after <CR> was ignored. How do I fix this?
I saw some answers mentioning <C-]> but is there away to implement this without generating tags?
Hopefully this makes sense. Thanks in advance!
:help gd jumps to the local definition of the word under the cursor. That "local definition" is always in the same buffer no matter what so I hope you don't expect it to jump to some other buffer.
Your mapping:
nnoremap <silent> gv :vsplit<CR>gd
works as expected, here: the window is split and the cursor jumps to the local definition of the word under the cursor:
First jump is with gd, second jump is with your gv.
If you want a "jump to definition" that works across files, gd and gD are not what you want. See :help ctags, :help cscope and :help include-search. Note that those features are not particularly smart. If you want something that understands your code better than you do, look elsewhere.
You don't actually need to define a new mapping/command for this. If you press CTRL-W d (or type :dsp <word>), it will show you the definition in a new window. From the documentation:
CTRL-W CTRL-D *CTRL-W_CTRL-D* *CTRL-W_d*
CTRL-W d Open a new window, with the cursor on the first
macro definition line that contains the keyword
under the cursor. The search starts from the
beginning of the file. If a count is given, the
count'th matching line is jumped to.
However, if you really want to press gv and have the definition shown, an alternative to romainl's solution is the following:
nnoremap gv :vertical dsplit <C-R><C-w><cr>
I am using easymotion plugin (https://github.com/easymotion/vim-easymotion) with vim.
If you use f/F motions in VIM (with easymotion plugin), easy motion highlights all possible positions when there are multiple matches, that way you can easily jump to the position you want.
But it doesn't work with y/c/d commands, how can I achive that ?
I have provided an example below for clarification:
This is some line.
Say I am working on the above line in vim and the cursor is at the i in "This". If I do "yfs" in vim, I would like easy motion to mark the three "s"s present to the right of the cursor. That way, I can easily yank/change/delete upto the s I want.
Thanks in advance !
You can, use something like this in your .vimrc:
" Find next occurence of a char using easymotion
map <Leader>f <Plug>(easymotion-bd-f)
nmap <Leader>f <Plug>(easymotion-overwin-f)
Now you can do something like y<Leader>fs and it will highlight the three s-characters. Selecting one will then yank from your cursor's position to that character.
If this does not work
This means that there are other key bindings that are using the same combination. You can check this with :map and then look for the key combination you are trying to map to the easy-motion. Removing that keybinding from your .vimrc or removing the plugin that created the binding should solve the problem.
If it's the YankRing plugin that's hijacking the y/c/d keystrokes, you can add the following to your vimrc to prevent it from doing that (Check :h yankring for more info):
let g:yankring_zap_keys = ''
Is there a simple way (i.e. without writing a script or elaborate keymap sequence) to Yank a group of lines and leave the cursor wherever the Yank was performed, as opposed to at the start of the block?
According to VIM's help: "Note that after a characterwise yank command, Vim leaves the cursor on the first yanked character that is closest to the start of the buffer." Line-wise seems to behave similarly.
This is a bit annoying for me since I tend to select a large region from top to bottom, Yank, and then paste near or below the bottom of the selected region. Today I'm setting a mark (m-x) just before Yank and then jumping back, but I suspect there may be a different Yank sequence that will do what I need.
I've searched SO and the web for this numerous times. There is so much existing "VIM shortcuts" material to wade through yet I've not found a solution to this one yet.
Thanks in advance.
Not quite answering your question, but perhaps '] would solve your problem?
'] `] To the last character of the previously changed or
yanked text. {not in Vi}
If you're using visual blocks (v), then after yanking the block you can use gv to re-select the same block (which also moves your cursor position back to where it was before yanking). If you then press Esc, the block is un-selected without moving the cursor.
Also of interest might be the ctrl-o command in visual block mode, which jumps between the start and end of the selected block.
If yanking in visual mode, you could also use '> or `> to go to the last line/character of the just yanked visual selection. In this context this is essentially the same as '] and `] which is apparently not supported e.g. in VsVim.
:y3 will yank three whole lines from current current line, If you know exactly how many line to yank, this command is very handy. :help :yank for details.
:%y will select the whole buffer without moving the cursor,
like ggvG$y, without the flash of selection highlight and modifying the "* register.
I use this insert mode map:
function! SelectAll()
%y*
endfun
imap <expr> <F3> SelectAll()
ps: if you prefer <C-V> to paste(outside vim), use %y+
check https://stackoverflow.com/a/1620030/2247746
I'm not sure sure if YankRing has changed since the vmap ygv<Esc> solution was posted but that didn't persist for me after adding it to my .vimrc. YankRing actually overwrote it.
Here's my solution in .vimrc.
function! YRRunAfterMaps()
vmap y ygv<Esc>
endfunction
I don't know what happened in the meantime but in IdeaVim the accepted answers don't work as I'd like them to. (Don't move the cursor on yank)
For me what did the trick was just setting a mark before yank and go to that afterwards.
vmap y mxy`x
I like to use hlsearch but I hate to keep everything highlighted after searching, to solve this problem I could simply use :nohlsearch or an abbreviation of it but that is still to much effort so I decided to try to do that on pressing escape. What I came up with is:
nnoremap <ESC> :nohlsearch<CR>
This works exactly as I want it to in GVim which I usually use for development but it does not work in vim.
If I search something in vim, press escape to deactivate the highlighting and use one of the arrow keys to navigate vim goes directly into insert mode and inserts a character on a new line.
As I never really came around to using h, j, k and l for navigation this is really annoying and I would like to know how I can make vim behave like gvim.
If you need more information you can find my entire vim configuration here.
Your problem is that when you press <Up> terminal sends something like <Esc>OA (you will see it if you type <C-v><Up> in insert mode) which is remapped to :nohlsearch<CR>OA. I do not know any solution except not mapping a single <Esc>, try either mapping to double <Esc>.
I created this map to disable search when press double <Esc>
nnoremap <silent> <Esc><Esc> :let #/ = ""<CR>
is :noh still too much work?
EDIT: I don't know about you, but I personally think :noh is easier than pressing Esc key, since I can press all the buttons without stretching my pinky too far (which is why I think the default mapping of Esc for going back to Command Mode from Insert Mode is a bit unfortunate). If you really use the :nohlsearch that much, you probably should remap it to something you can reach from the Home Area (i.e. regular letters, or numbers, or perhaps Ctrl-letters).
Anyway, typing the exact command you give works in my vim (on gnome-terminal). Are you sure you put the rule in .vimrc file, instead of .gvimrc? No luck after restarting vim? Try :source /path/to/config/file and see if that makes it to work.
I like to insert blank lines without entering insert mode and I used this keymapping:
nomap go o <esc>
This does create the blank line but introduces some weird behaviour. I have smart indent and autoindent set. The new line follows the indents but doesn't remove them even though doing so manually automatically removes the redundant whitespace. It also adds a single whitespace where the cursor is each time.
Anyone have any insights as to explain this behaviour?
Vim is very literal with how you write your mapping commands - it's actually processing the space in your mapping before it does the <ESC>. In other words, your mapping does this:
nnoremap go o<SPACE><ESC>
You should change it to:
nnoremap go o<ESC>
And make sure you don't have any extra spaces in the mapping!
I agree with "too much php".
This is the relevant section from my .vimrc
nnoremap <A-o> o<ESC>k
nnoremap <A-O> O<ESC>j
I think it's faster since you get the cursor back at the original line (Although not on the original character).
As usual, the vim wiki has a useful tip: Quickly adding and deleting empty lines. The trick is to set paste before adding the new line and afterwards set nopaste. Additionally, this will set a mark to remember the cursor position and jump back to where you were.
nnoremap go :set paste<CR>m`o<Esc>``:set nopaste<CR>
nnoremap gO :set paste<CR>m`O<Esc>``:set nopaste<CR>