loading...
Cover image for Let's learn Vim - Basic Configuration

Let's learn Vim - Basic Configuration

thetinygoat profile image Sachin Saini ・3 min read

Let's Learn Vim (4 Part Series)

1) Let's learn Vim - The Basics 2) Let's learn Vim - Moving and The Configuration File 3) Let's learn Vim - Basic Configuration 4) Let's learn Vim - Plugins

In the previous articles, we learned about basic and some advanced movements, we also created our config file.
In this article, we are going to start customizing vim. These are basic customizations that make my life easier while using vim. If you prefer something else to my customizations feel free to update them.

When I was learning about vim this resource helped me a lot, my config file is heavily inspired by this.

With all that out of the way let's start customizing!!

Basic configuration

"enable syntax processing
syntax enable

This one is pretty self-explanatory.

set autoindent
"don't expand tabs to spaces
set noexpandtab  
"indentation will be 4 character blocks wide   
set tabstop=4
set shiftwidth=4
set softtabstop=-1

I like to use tabs instead of spaces, so these settings take care of that.

"enable line numbers
set number

By default vim doesn't show line numbers, this command enables that.

"highlight current line
set cursorline

Highlight the current line you are on.

"enable file specific indentation
filetype indent on
"enable wildmenu
set wildmenu

This is a pretty neat feature, in vim, when you type some command and hit <tab> it autocompletes that command, when we enable wildmenu, it displays a list of options we can choose from.

"tell vim to only redraw when necessary
set lazyredraw
"highlights matching [{()}]
set showmatch
"search as characters are being typed
set incsearch
"highlight matches
set hlsearch
"turn off search highlighting
nnoremap <leader><space> :nohlsearch<CR>
"move vertically by visual line
nnoremap j gj
nnoremap k gk

Sometimes when we have a very big line of text, it gets wrapped to a new line and when we use j, k to move vertically, they skip over the wrapped line in favor of the next real line, this setting fixes that.

"remap ^ and $ to B and E
nnoremap B ^
nnoremap E $
nnoremap $ <nop>
nnoremap ^ <nop>
"map leader to ,
let mapleader=","

Leader is a unique key in vim, it acts as a prefix key for many commands, here we have mapped leader to , (comma)

"map semicolon to colon
nmap ; :

I find it pretty annoying to press : every time i need to issue a command so I've mapped colon to the semicolon.

"map esc to jk
inoremap jk <esc>

Hitting <esc> to get into normal mode is kinda annoying as well, so I have mapped <esc> to jk.

"case insensitive tab completion
set ignorecase
"copy to the system clipboard
set clipboard+=unnamedplus

By default vim doesn't use system clipboard, this command fixes that.
Note: To get the system clipboard working on linux, install xclip or similar tool on other platforms.

"better split movements
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>

Move between splits in a better way.

"sane split defaults
set splitbelow
set splitright

Open new vertical splits to right and horizontal splits to the bottom of the current buffer.

"move between buffers
map gn :bn<cr>
map gp :bp<cr>
" move lines up or down
nnoremap <A-j> :m .+1<CR>==
nnoremap <A-k> :m .-2<CR>==
inoremap <A-j> <Esc>:m .+1<CR>==gi
inoremap <A-k> <Esc>:m .-2<CR>==gi
vnoremap <A-j> :m '>+1<CR>gv=gv
vnoremap <A-k> :m '<-2<CR>gv=gv

This one is nice, it allows you to move a line, up or down using <alt>-k and <alt>-j respectively and it even works in insert mode!

Conclusion

After this you will have a pretty sane vim configuration, I have left out some details on purpose so that you search and learn on your own. If you have any questions feel free to reach out.
In the next part, we will be working with plugins. We will set up a plugin manager and install our first plugin. See you soon :)
Follow me on Github and Twitter

Let's Learn Vim (4 Part Series)

1) Let's learn Vim - The Basics 2) Let's learn Vim - Moving and The Configuration File 3) Let's learn Vim - Basic Configuration 4) Let's learn Vim - Plugins

Posted on by:

thetinygoat profile

Sachin Saini

@thetinygoat

Currently a student, love to post about all things tech.

Discussion

markdown guide
 

Instead of set nu (number) I use
set rnu
Really handy