DEV Community

Cover image for How I Vim
Vince
Vince

Posted on

How I Vim

Introduction

Vim jokes are as old as the editor itself. However I have been using Vim daily for years now, mostly for quick edits and writing commit messages. I figured I would share my setup and maybe someone else could find it helpful.

Ease of Use

The hardest thing for people to grasp when they first use Vim is the command system. This system can be extremely powerful, but for most use cases it just confuses new users on how to save the buffer they just wrote. Our first goal will be to abstract some of the more common commands to a hotkey combination.

First we need to create a .vimrc in our home directory touch .vimrc, since you may not be a vim user yet make the following changes with whatever editor you prefer.

We are going to be setting a "leader" which will be the start of all our hotkeys.

let mapleader = "`" 

I prefer to use the

`

key since it is a bit out of the way and I am less likely to accidentally hit it, however you can set it to whatever you want.

Now we are going to create a command to write the current buffer:

noremap <leader>w :w<cr>

noremap is a mapping command that is not recursive, meaning we will not be chaining our new commands together. This can get a bit unwieldy for beginners and make it hard to debug. If you are comfortable with vim then feel free to use the recursive method map.

Once we tell vim that we are mapping something we use the <leader> tag to use that variable followed by w. So the keystrokes

` w

will write the current buffer via the command :w, executed by the <cr> carriage return. This one line is the equivalent of pressing esc, typing :w, and then hitting enter.

Now we can write some more common commands via noremap:

nnoremap <leader>a :wqall<cr>
nnoremap <leader>q :qall!<cr>

These two mappings will add

` a

to write quit all buffers that are open, and

` q

to quit all open buffers.

Now we can easily get out of vim!

Going Further

Now that we have some commands mapped out to easily write and quit vim we can start to make it feel more like home. Here is an excerpt from my own .vimrc:

" Turn on syntax highlightin
syntax on
" Turn on line numbers
set number
" Set the colorscheme to elflord
" See more colorschemes by entering command mode via esc then typing
" :colorscheme then pressing space and changing it via tab
colorscheme elflord

" Turn tabs into spaces and set them to be two spaces long
set expandtab
set tabstop=2
set softtabstop=2
set shiftwidth=2
set splitright

" highlight the current line of the buffer
set cursorline
" allow the mouse to be used to change cursor position
set mouse=a

" This block here executes the Vim Explorer on startup, opening the
" current directory tree in a small pane to the left
let g:netrw_banner = 0
let g:netrw_liststyle = 3
let g:netrw_browse_split = 4
let g:netrw_altv = 1
let g:netrw_winsize = 10
augroup ProjectDrawer
  autocmd!
  autocmd VimEnter * :Vexplore
augroup END

" `` should rotate split focus
noremap <leader><leader> <C-W><C-w>


Conclusion

I hope this has possibly helped you better work around vim, or piqued your interest to start customizing your own vim to your liking!

If you have any cool vim tips feel free to drop them in the comments, I'd love to up my vim game!

Latest comments (3)

Collapse
 
alvarocaceresmu profile image
Álvaro Cáceres Muñoz

set mouse=a has just changed my life hahahhahaha thanks!

Collapse
 
aturingmachine profile image
Vince

That has to be one of my favorites!

Collapse
 
delbetu profile image
M Bellucci • Edited

I have three tips for you.

Combine vim with git by mapping your most common commands and of course use fugitive plugin

Use fuzzy search for finding files and exploring content—> search for vim fzf plug-in

Map a command to run the current tests you are working on.

These three things made my development flow super fast