Switch to split window on the same line - vim

I have two buffers open in vim using a vertical split which are linked using :set scrollbind.
Is there a way to switch between the windows so that the cursor remains on the same (relative) row when I do switch between them using the ctrl+w commands?

A mapping will do what you need
Do this in the left window:
:nmap <right> :let linenum=getpos('.')[1]\|:wincmd l\|:call cursor(linenum,0)<cr>
and do this in the right window:
:nmap <left> :let linenum=getpos('.')[1]\|:wincmd h\|:call cursor(linenum,0)<cr>
Then you can use the left and right arrows to switch between the windows and the cursor will go to the same line in the other window.
Edit:
I didn't read your question carefully (shame on me!:).
Here's how to get exactly what you wanted:
On the left window:
:nnoremap <right> :let offset=winline()\|wincmd l\|exe 'normal ' . offset . 'H'<cr>
and on the right:
:nnoremap <left> :let offset=winline()\|wincmd h\|exe 'normal ' . offset . 'H'<cr>
Have fun!

Vim version 7.3 added a cursorbind option:
When this option is set, as the cursor in the current
window moves other cursorbound windows (windows that also have
this option set) move their cursors to the corresponding line and
column. This option is useful for viewing the
differences between two versions of a file (see 'diff'); in diff mode,
inserted and deleted lines (though not characters within a line) are
taken into account.

^W+r (switch buffers) followed by ^W^W (return cursor back)

Related

Is there a way to copy a selected area up or down in vim?

In VSCode you can copy and paste selected lines of text up or down using Alt+Shift + Up or Down.
I switched to vim a few months ago, and I really miss this feature.
I found out you can move selected lines in visual mode up or down using these visual mode bindings,
vnoremap J :m '>+1<CR>gv=gv
vnoremap K :m '<-2<CR>gv=gv
but it'd be great if you could copy and paste up and down as well without using yy and p because when you're done yanking, the cursor is placed at the initial position, exiting visual mode and reducing productivity.
Well, that, here, is the problem with copying and pasting random snippets from the internet without understanding what they do: they are little black boxes that one can't modify or extend at will by lack of knowledge.
Let's deconstruct the first mapping:
vnoremap J :m '>+1<CR>gv=gv
:[range]m {address} moves the lines covered by [range] to below line {address}. Here, the range is automatically injected by Vim: '<,'> which means "from the first line of the latest visual selection to its last line", and the address is '>+1, meaning "the last line of the latest visual selection + 1 line". So :'<,'>m '>+1<CR> effectively moves the selected lines below themselves,
gv reselects the latest visual selection,
= indents it,
gv reselects it again for further Js or Ks.
Now, we want a similar mapping but for copying the given lines. Maybe we can start with :help :m and scroll around?
Sure enough we find :help :copy right above :help :move, which we can try right away:
xnoremap <key> :co '>+1<CR>gv=gv
Hmm, it doesn't really work the way we want but that was rather predictable:
the address is one line below the last line of the selection,
the selection is reselected and indented, which is pointless.
We can fix the first issue by removing the +1:
xnoremap <key> :co '><CR>gv=gv
and fix the second one by selecting the copied lines instead of reselecting the latest selection:
xnoremap <key> :co '><CR>V'[=gv
See :help :copy and :help '[.

Cursor moving after remap of <C-e> and <C-y> in vim

I have encountered some strange behavior in vim. I have done this mapping:
execute "set <A-j>=\ej"
execute "set <A-k>=\ek"
noremap <A-j> <C-e>
noremap <A-k> <C-y>
But now when I use <A-j> or <A-k>, the cursor keeps moving to the right! It doesn't do that when I'm just using <C-e> or <C-y>.
Is this a bug or some strange feature?
Here is the Vim code you posted:
execute "set <A-j>=\ej"
execute "set <A-k>=\ek"
noremap <A-j> <C-e>
noremap <A-k> <C-y>
If you copy/paste it, or if you edit this post (or yours) and go to the end of the <A-j> line, you'll find there is a trailing space there. That is significant, because in Vim's normal mode, pressing Space will move the cursor one position to the right.
Using this code as you pasted it, I can reproduce the problem, although my cursor is moving to the right, not to the left as you describe. The map is executing as CtrleSpace, which would scroll the viewport downward by one line, and then move the cursor forward by one character.
Your question describes the cursor moving to the left, not to to the right. It's possible that you simply confused left and right when you wrote the question, but it's also possible that in your .vimrc you have an embedded backspace (Ctrl-H, or Delete) character at the end of the line. That would have the effect of moving backward (left) by one character, much as Space advances forward (right) by one character.

vim move a block of selected code up or down

There is a way to move highlighted code up or down with arrow keys. Even the indentation would be correct on dropping the block.
For the life of me I do not remember how to do it.
Can someone please tell me how to do it.( suitably embarrassed asking this).
Here's the link that'll help you
link
You can move a line, or a block of lines, with the :m command. Examples:
:m 12 move current line to after line 12
To move a block of lines, use the same command but visually select the lines before entering the move command. You can also use arbitrary ranges with the move command. Examples:
:5,7m 21 move lines 5, 6 and 7 to after line 21
why not use d+p if you had chosen the block code
If you want to do it with arrow keys, you can try Tim Pope's vim-unimpaired plugin, which you can find on Github.
Then you can use mappings similar to these (except map them to whatever keys you like).
" ^U moves lines up, ^D moves lines down.
" Works in normal or visual select modes.
nmap <C-u> [e
nmap <C-d> ]e
vmap <C-u> [egv
vmap <C-d> ]egv
Cutting a block of text in visual mode and then pasting:
Go to command mode by pressing Esc if you're not already in it
Move to the start or end of where you want to go
Press V to switch to visual mode
Move with arrows or H,J,K,L to the other end of the block you want to move
Press D to cut it, it will switch back into command mode
Move to where you want to put it and press P to paste what you cut
It seems like a lot of steps but it's just a verbose explanation.
Here is my configurations, There is a nice configuration that I am using to map Ctrl+J to move a block of code up and Ctrl+K to move a block down. I am using currently NVIM but I think the same configuration setting is applicable for VIM as well.
Setting on NVIM
The location that I am using on macOS Catalina for the NVIM is located at:
.config/nvim/init.vim
nnoremap K :m .-2<CR>==
nnoremap J :m .+1<CR>==
vnoremap K :m '<-2<CR>gv==gv
vnoremap J :m '>+1<CR>gv=gv
for vim I think you can change the setting on
.vimrc located under the home directory.

In Vim, is there a way to move an object/motion (word, character, visual selection, etc) horizontally in the line, shifting characters around it?

For instance, I have the following line:
object => "plaintext",
I want to move the word 'object' over to the right (so it's one space away from the '=>'), like this:
object => "plaintext",
The only way I know of doing it would be (starting at the beginning of the line): i <esc>ea4x, or the equivalent from another starting location. I'd love to do the same thing I do for moving lines, but horizontally:
" Move lines up/down
nnoremap <Down> :m+<CR>==
nnoremap <Up> :m-2<CR>==
vnoremap <Down> :m '>+1<CR>gv=gv
vnoremap <Up> :m '<-2<CR>gv=gv
You could cut(delete) and paste(put) your whitespace. It's not glorious but it would work. However I feel like a better option would be to align your text by using tabular (Aligning text with Tabular.vim) or vim-easy-align.
Using tabular you can do the following:
:Tabularize/=>/r1l1l1
There is also exchange.vim, but that is just fancy delete and putting. Swapping two regions of text with exchange.vim
Another option to just do the horizontal movement is the following:
Provided you have already selected the text you want to move:
:vnoremap <A-S-l> xp`[v`]
:vnoremap <A-S-h> xhhp`[v`]
In normal mode, to just move the word your cursor is over (in any character of that word). These mappings will let you in visual mode:
:nnoremap <A-S-l> viwxp`[v`]
:nnoremap <A-S-h> viwxhhp`[v`]
In insert mode, to just move the word your cursor is over (in any character of that word). These two mappings will let you in visual mode:
:inoremap <A-S-l> <Esc>viwxp`[v`]
:inoremap <A-S-h> <Esc>viwxhhp`[v`]
With these mappings, you can:
Move selected text to the right:
Alt+Shift+l
Move selected text to the left:
Alt+Shift+h
Basically, you just cut what you have selected, paste it appropriately and select again what you have just pasted.
Obviously you can select any mapping you like.

How do I press "<C-L> <C-W>l<C-W>_" in Vim?

In one of the vim config files I have noticed this keyboard mapping
map <C-L> <C-W>l<C-W>_ supposedly for easier moving in tabs and windows. What does that translate to keyboard presses? What is that underscore at the end for?
The command map <C-L> <C-W>l<C-W>_ maps Ctrl-L to Ctrl-W, l, Ctrl-W, _.
You invoke this binding by just pressing Ctrl-L. To invoke what it binds to you would type Ctrl-W, then l, followed by Ctrl-W again, and finally _ (which on a US keyboard is shift-hyphen). This is two separate bindings, <C-W>l moves the cursor to the window to the right, and <C-W>_ resizes current window to the maximum possible vertical size.
The Ctrl+wlCtrl+w_ keys sequence is somewhat too long so someone has created a shortcut ("mapping" in Vim-speak): Ctrl+L for it.
<C-w>l<C-w>_ moves the the cursor to the window on the right (<C-w>l) and maximizes it vertically (<C-w>_).
Mappings always follow the same structure:
map (or imap for insert mode mapping, nmap for normal mode mapping, etc.)
some whitespace
the shortcut you want, here <C-L>
some whitespace
the sequence of commands triggered by the shortcut
See :help windows for more info on window management and :help mapping for more info on mappings.

Resources