Any new coder who has worked with Vim before can tell you that it is not very intuitive and has a sharp learning curve. That being said, I tend to do things the hard way, and dove right in to learning it. After going through
vimtutor, I decided to make Vim my one and only code editor.
Through today, I have been using Vim exclusively for writing code. I have gotten very quick at it, and I feel it has increased my productivity. I recently was able to boost my productivity further after reading about reassigning your leader key to the space bar, and then creating any space + key actions that you need. So, a few weeks ago, I went remapping crazy and added a bunch of quick shortcuts to my
.vimrc. This has really sped up my work, and I highly recommend Vim users try some or all of these.
In all of the examples below,
<space> is the leader key. All of these work in normal mode, and a few in visual mode.
Working with Files
<space> wto save
<space> qto save and quit
<space> Qto quit without saving
<space> eto reload the file
<space> Eto discard all changes to the file
lto move left, down, up, or right a window
Lto move a window to the extreme left, down, up, or right of the screen
<space> oto rotate windows clockwise
<space> Oto rotate windows counter-clockwise
<space> ]to make the window 10 columns wider
<space> [to make the window 10 columns narrower
<space> +to make the window 10 rows taller
<space> -to make the window 10 rows shorter
<space> =to make the windows sized eqally
<space> sto split the window horizontally
<space> vto split the window vertically
<space> nto open the NERDTree window
<space> tand enter a shell command to run it.
<space> rto repeat the last-run shell command
<space> bto switch to the shell
<space> pto paste the last yanked item in front of the cursor
<space> Pto paste the last yanked item behind the cursor
(also works in visual mode)
;to move a page down
'to move a page up
<space> 4to jump to the end of the line
<space> 6to jump to the beginning of th eline
(also works with multiple lines in visual mode)
.to indent to the right
,to indent to the left
I’m sure I will think of others, but these have really helped. Now, in it’s full glory, the key mapping section of my
.vimrc. Feel free to copy/paste/edit. If you’ve got a great Vim key mapping, leave it in the comments!
let mapleader = "\<space>" nnoremap <leader>w :w<CR> nnoremap <leader>n :NERDTreeToggle<CR> nnoremap <leader>q :wq<CR> nnoremap <leader>Q :q!<CR> nnoremap <leader>b :sh<CR> nnoremap <leader>r :w<CR>:!!<CR> nnoremap <leader>e :e<space>%<CR> nnoremap <leader>E :e!<space>%<CR> nnoremap <leader>h <C-W>h nnoremap <leader>j <C-W>j nnoremap <leader>k <C-W>k nnoremap <leader>l <C-W>l nnoremap <leader>H <C-W>H nnoremap <leader>J <C-W>J nnoremap <leader>K <C-W>K nnoremap <leader>L <C-W>L nnoremap <leader>o <C-W>r nnoremap <leader>O <C-W>R nnoremap <leader>p "0p nnoremap <leader>P "0P nnoremap <leader>g :%s/ nnoremap <leader>t :!<space> nnoremap <leader>[ <C-w>10< nnoremap <leader>] <C-w>10> nnoremap <leader>- <C-w>10- nnoremap <leader>+ <C-w>10+ nnoremap <leader>= <C-w>= nnoremap <leader>s :sp<CR> nnoremap <leader>v :vsp<CR> nnoremap . >> nnoremap , << nnoremap <leader>4 $ nnoremap <leader>6 ^ vnoremap <leader>4 $ vnoremap <leader>6 ^ vnoremap , <gv vnoremap . >gv nnoremap ; <C-d> nnoremap ' <C-u> vnoremap ; <C-d> vnoremap ' <C-u> nnoremap <leader>; $a;<esc>