I've just made a little snipplet on my keybindings.vim to shift a hole block of code around. It works basically the same way I do it in the old fashioned manual way ( on normal mode, {V} to select the hole block on visual mode, then > and < to move through indentation, and :m+ and :m-2 to move down or up ).
The problem I'm getting is that it works only to move around through indentation (horizontally), but for vertical move, it does not work. I just can't find the reason, as doing exactly the same sequence manually (as I'm used to), it works like a charm.
First of all: I've tested the snipplet on Vim 7.2 (on Linux) and Vim 7.3 (on MacOS).
Second: I know If I put a:
vnoremap < <gv
...and a:
vnoremap > >gv
...in my keymaps, I'll be able to move visual-selected blocks without loosing the visual selection... in spite of this, I'd like to get this working so I don't have to take care of the visual selection with a manual ESC {jV}k
Can anybody tell me what am I doing wrong? I thank you all in advance!
Regards!
"============================================================================
"Ctrl + Shift + > [normal or insert mode] - move entire block around
"============================================================================
nnoremap <silent> <C-S-Right> :let savecur=getpos(".")<CR>{V}><CR>
\:call setpos('.', savecur)<CR>4l
inoremap <silent> <C-S-Right> <Esc>:let savecur=getpos(".")<CR>{V}><CR>
\:call setpos('.', savecur)<CR>5li
nnoremap <silent> <C-S-Left> :let savecur=getpos(".")<CR>{V}<<CR>
\:call setpos('.', savecur)<CR>4h
inoremap <silent> <C-S-Left> <Esc>:let savecur=getpos(".")<CR>{V}<<CR>
\:call setpos('.', savecur)<CR>3hi
nnoremap <silent> <C-S-Up> :let savecur=getpos(".")<CR>{V}:m-2<CR>
\:call setpos('.', savecur)<CR>k
inoremap <silent> <C-S-Up> <Esc>:let savecur=getpos(".")<CR>{V}:m-2<CR>
\:call setpos('.', savecur)<CR>ki
nnoremap <silent> <C-S-Down> :let savecur=getpos(".")<CR>{V}:m+<CR>
\:call setpos('.', savecur)<CR>j
inoremap <silent> <C-S-Down> <Esc>:let savecur=getpos(".")<CR>{V}:m+<CR>
\:call setpos('.', savecur)<CR>ji
"============================================================================
Change the :move commands used in those mappings for moving a paragraph down
from :m+ to :m'>+.
Related
Help! My vim ui is weird. It is hard to explain in word, please see the picture.
Try cleaning the search register:
:let #/=""
I have some mapping to temporarelly disable hlsearch, actually it toggles it, wich is more convinient, in my humble opinion.
nnoremap <silent> <c-l> <ESC>:set hls! hls?<cr> :call clearmatches()<cr><left><c-l>
inoremap <silent> <c-l> <C-o>:set hls! hls?<cr> <C-o>:call clearmatches()<cr>
vnoremap <silent> <c-l> <ESC>:set hls! hls?<cr> <bar> gv :call clearmatches()<cr>
I've recently mapped 10j to <A-j> and 10k to <A-k>, which is seemingly quite amazing, but there is one problem with it:
When I normally type 10j (not using the shortcut), it will just move 10 rows down vertically but not move horizontally at all (given the lines have the same length), but when I use <A-j> it will always (well, interestingly enough, not always, but most of the times) also move one letter to the right.
Funnily enough, this happens only for <A-j>, whereas <A-k> works as intended. How can I prevent that? And maybe most importantly: Why is that?
If it helps, these are my other keybindings:
nnoremap K K<C-w>L
nnoremap <A-h> :set hls!<cr>
nnoremap / :set hlsearch<cr>/
nnoremap <A-j> 10j
nnoremap <A-k> 10k
nnoremap <A-w> W
nnoremap <A-b> B
nnoremap <A-v> V
nnoremap <A-m> '
nnoremap <A-p> "+p
nnoremap <A-y> "+y
nnoremap <A-4> $
nnoremap <A-3> 0
nnoremap Y y$
vnoremap <A-h> :set hls!<cr>
vnoremap / :set hlsearch<cr>/
vnoremap <A-j> 10j
vnoremap <A-k> 10k
vnoremap <A-w> W
vnoremap <A-b> B
vnoremap <A-v> V
vnoremap <A-m> '
vnoremap <A-p> "+p
vnoremap <A-y> "+y
vnoremap <A-4> $
vnoremap <A-3> 0
Yeah, I like the alt-key a lot.
You have a trailing space character at the end of your mapping:
:nnoremap <A-j>
n <M-j> * 10j<Space>
<Space> is the same command as l; it moves a character to the right (where possible).
The right-hand side in a mapping is taken literally (up to the end of the line or a | command separator). Another common mistake is appending a " comment to a mapping definition.
Plugin recommendations
If you regularly stumble over trailing whitespace (it's generally frowned upon in many coding styles, and tools like Git also highlight it as problematic), my ShowTrailingWhitespace plugin can alert you to those, and the DeleteTrailingWhitespace plugin can remove them for you. (The plugin pages have links to alternative plugins.)
For example I'm in python code and want to jump between classes:
nnoremap <buffer> [c /^\s*class\ <CR>
How to prevent them from highlight in more elegant way than :nohl at the end of command each time?
You can avoid highlighting search matches by using the :help search() function or writing your own function.
With search()
nnoremap <buffer> <silent> [c :<C-u>call search('^\s*\zsclass\s')<CR>
With your own function
" with ':help :normal'
function! JumpToNextClass()
normal! /^\s*\zsclass\s
endfunction
" with ':help search()'
function! JumpToNextClass()
call search('^\s*\zsclass\s')
endfunction
nnoremap <buffer> <silent> [c :<C-u>call JumpToNextClass()<CR>
But none of that really matters since Vim already comes with ]] and [[.
I have in my .vimrc the following lines, which lets me switch windows with ctrl+hjkl:
nnoremap <C-h> <C-W>h
nnoremap <C-j> <C-W>j
nnoremap <C-k> <C-W>k
nnoremap <C-l> <C-W>l
These are fine for my desktop computer, but on my netbook, I want to have the active window completely fill the tiny screen. This means typing ctrl+w _ and ctrl+w | after each window change. The logical step would be to add those keystrokes to the mapping, yielding:
nnoremap <C-h> <C-W>h<C-W>_<C-W>|
nnoremap <C-j> <C-W>j<C-W>_<C-W>|
nnoremap <C-k> <C-W>k<C-W>_<C-W>|
nnoremap <C-l> <C-W>l<C-W>_<C-W>|
But that fails, consistently, when in a mapping, despite working when I simply type the required keys; and (as I have set 'showcmd') it seems to leave a trailing <C-W>.
I have also tried using :wincmd:
nnoremap <C-h> :wincmd h<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-j> :wincmd j<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-k> :wincmd k<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-l> :wincmd l<cr>:wincmd _<cr>:wincmd |<cr>
But that complains about trailing <cr> whenever my vimrc is sourced, so I'm not going to pursue that further without more research.
Any ideas?
Try using <Bar> instead of |. ie:
nnoremap <C-h> <C-W>h<C-W>_<C-W><Bar>
nnoremap <C-j> <C-W>j<C-W>_<C-W><Bar>
nnoremap <C-k> <C-W>k<C-W>_<C-W><Bar>
nnoremap <C-l> <C-W>l<C-W>_<C-W><Bar>
| are used to have multiply commands on one line and you will need to be escaped with a backslash when used literally:
nnoremap <C-h> <C-W>h<C-W>_<C-W>\|
nnoremap <C-j> <C-W>j<C-W>_<C-W>\|
nnoremap <C-k> <C-W>k<C-W>_<C-W>\|
nnoremap <C-l> <C-W>l<C-W>_<C-W>\|
On the other hand | can be useful:
nnoremap xxx :if 1 == 2 | echom "hello" | endif
I've just started using EasyGrep . Often my NERDTree window is in focus when I am doing a search and the first search result loads in this window.
Is there a way to force EasyGrep to open the first result in the main buffers window?
Thanks!
Here is what I added in my .vimrc:
function! SearchOutsideOfNerdtree()
if &filetype == 'nerdtree'
:wincmd l
endif
:execute input("", ":Grep\<Space>")
endfunction
function! ReplaceOutsideOfNerdtree()
if &filetype == 'nerdtree'
:wincmd l
endif
:execute input("", ":Replace\<Space>")
endfunction
nnoremap <Leader>f :call SearchOutsideOfNerdtree()<CR>
nnoremap <Leader>g :call ReplaceOutsideOfNerdtree()<CR>
Basically I created a function that checks if the current filetype is nerdtree and I move to the right buffer.
Here is what I added in my .vimrc and it works:
nnoremap <silent> ,g :<C-u>wincmd w<CR>:Unite grep:. -buffer-name=search-buffer<CR>