Monday, Dec. 11, 2017

Vim Mapping Epiphanies That I Havn’t Seen Anywhere Else

Written By:

|

October 23, 2017

|

Posted In:

Vim Mapping Epiphanies That I Havn’t Seen Anywhere Else

As a Vim user, you will know the ecstatic sense of epiphany that you get when you think of a mapping or setting that can change your life. Each item in this list is a mapping that came to me through ‘epiphany’ that I havn’t seen elsewhere on the internet, even though they are quite simple. I use Vim mainly for writing, but also for code and these mappings have each revolutionised my productivity in both.

Reverse Enter

This is the mapping that has improved my life immeasurably that I can’t believe no one else uses. All it does is line-break up rather than down. It’s not as simple as you think to make this happen, because the linebreak character breaks down as you may know, and there is no up-linebreak character. This is why it needs to use the VIM-Schlepp plugin because it adds a normal linebreak, then switches the order of the original line and the new one below. There are definitely non-plugin ways to map this, but Schlepp just works so seamlessly.


" Reverse Enter
inoremap <S-CR> <CR><C-o><Plug>SchleppUp

Move And Duplicate Text In Any Direction With One Key

To have true power over your text, you want to be able to move and also duplicate it up, down, left or right with just one key. It’s extremely satisfying to be able to do this, and especially efficient when editing code.

VIM-Schlepp is a plugin that provides plugs for shifting and also duplicating text. I assign the ‘A-h,j,k,l<Up>,<Down>,<Left>,<Right>’ keys to these things.


" VIM-Schlepp
vmap <unique> <A-k> <Plug>SchleppUp
vmap <unique> <A-j> <Plug>SchleppDown
vmap <unique> <A-l> <Plug>SchleppLeft
vmap <unique> <A-h> <Plug>SchleppRight
vmap <unique> <A-S-k> <Plug>SchleppIndentUp
vmap <unique> <A-S-j> <Plug>SchleppIndentDown
vmap <unique> <A-Up> <Plug>SchleppDupUp
vmap <unique> <A-Down> <Plug>SchleppDupDown
vmap <unique> $ <Plug>SchleppDupDown
vmap <unique> <A-Left> <Plug>SchleppDupLeft
vmap <unique> <A-Right> <Plug>SchleppDupRightde

You may have noticed that I also mapped duplicate-line-downwards to ‘$’ because I want an easy-to-reach mapping because I use it so frequently.

Then there are some non-plugin mappings for simplifying indenting (moving text left and right) through using the same keys:


" Indent using arrow keys
nnoremap <A-h> <<
nnoremap <A-l> >>
vnoremap <A-h> <gv
vnoremap <A-l> >gv
nnoremap <A-left> <<
nnoremap <A-right> >>

Then I also simplified the default ‘<<’, ‘>>’ indent mappings by making them a single key. This isn’t always recommended because you may want to make mappings like ‘<o’, ‘<p’.


" Indent simplification
nnoremap > a<C-t><Esc>
nnoremap < a<C-d><Esc>
vnoremap > >gv
vnoremap < <gv

More Consistent, Less Key-chording Visual Select Line And To End-of-Line

By default, visual select line is ‘V’, and visual select to end-of-line is ‘v\$’. I remapped select to end-of-line to ‘V’ to be consistent with the (questionable) pattern that capital letter performs action to end-of-line (e.g. ‘Y’, ‘D’, ‘C’). Then, I remapped visual select line to ‘v’ in visual mode. Therefore, to select a line, you just press ‘vv’ (the first works in normal mode, then the second works when you’re in visual mode).


" Select to end of line visual mode
nnoremap V v$
vnoremap v V
vnoremap V $

Join Lines Upwards

‘J’ joins the current line to the one below, but when you want to join the current line to the one above, you need to do ‘kJ’. This is not seamless, especially when you want to join say the next three several above, and you end up mashing the keyboard with ‘kJkJkJkJ….’. That’s why I made the join-line-above mapping for ‘K’ because you can just hold it down to repeat easily.


" Join lines upwards
nnoremap <leader>gK K
nnoremap K k0J

The first mapping there remaps the default ‘K’ key (lookup keyword) to some other key so you can still use it.

Backspace And Delete In Normal Mode

‘Backspace’ and ‘Delete’ do basically nothing in normal mode by default. Why not map them to delete words quickly?

Furthermore, ‘Control-Backspace’ and ‘Control-Delete’ are mapped to operate on entire lines.


" Normal mode backspace, delete
nmap <BS> db
nmap <Del> dw
nnoremap <C-BS> ddk
nnoremap <C-Del> dd

Redo to most recent change

Remap the counter-intuitive ‘U’ key to something that will save you from pressing like 15 times in a row.


" Redo to most recent change
nnoremap U 999<C-r>

Kill Buffers With One Key

This is the simplest one in the list: ‘:bd’ using just the ‘-’ key which is not useful by default (it is just another upwards-movement key).


" Kill buffer not window
nnoremap – :Bdelete<CR>

One-key Cycle Through Buffers

Another epiphany that hit me like something that I should have thought of long ago. Cycle through buffers using just the ‘Tab’ key.


" Cycle through buffers
nnoremap <silent> <Tab> :silent bnext<CR>
nnoremap <silent> <S-Tab> :silent bprevious<CR>

Cycle Through Windows Using Important, Easy-to-reach Keys

Cycling through windows is something that you probably find yourself doing a lot, so you want to map some important, easy-to-reach keys to do it. This is what I decided to dedicate to it, because it is so easy to press it is almost instantaneous.


" Cycle through windows
nnoremap <S-Space> <C-w>w
nnoremap <C-Space> <C-w>W

I find this to be a faster alternative to mapping ‘<C-h,j,k,l>’ because you don’t have to even think about the direction, and it saves all your precious ‘<C-h,j,k,l>’ keys.

Share This Article

Related News

How Google AMP Is Changing The Web Even More Than You Think
Google’s New Technology (AMP) Can Make Your Mobile Site Load Instantaneously

About Author

Leave A Reply

Leave a Reply

Your email address will not be published. Required fields are marked *