In the first place that I decided to use Vim as my primary code editor, I had some reasons:
- One of them is that I spend much time working on multiple servers, and I need something to work with it everywhere.
- The second one is Vim navigation; When I'm working with Vim, I don't need to switch between mouse or touchpad and keyboard (and this combination with my i3wm is great).
- It's very lightweight and customizable.
But overall everyone has their reasons to choose their code editor or IDE, and there are a lot of blog posts or Youtube videos that compares them. I'm not going to write this post about "Why Vim?" Actually, I want to describe some of my problems with Vim when I decided to switch to Vim and how I fixed them.
Use hjkl
to navigate instead of arrow keys
One of the things that might be helping you to get used to Vim is disabling keyboard arrow keys and use hjkl
when using Vim.
To do this, open your Vim config(.vimrc
) and put these lines into it:
"" disable arrow keys
:nnoremap <up> <nop>
:nnoremap <down> <nop>
:inoremap <up> <nop>
:inoremap <down> <nop>
:inoremap <left> <nop>
:inoremap <right> <nop>
Copy and paste text between Vim and other applications
It is common to copy some code from StackOverflow (!) or some other project when you are coding, and it is very convenient in other editors but you can't do this with your default installation of Vim.
To do this, you must pass these two steps:
- Check your vim installation with
vim --version
and check if you see+
or-
beforeclipboard
. If you see+
, It means your installation of Vim supportclipboard
, Otherwise, you must install a Vim version, which supports that. I'm using Debian and inapt-get
repository I couldn't find a version that supports this, So I installedgvim
and add this alias to my.zshrc
file to preventgvim
open in another window and run in Vi mode.
# use vim-gtk terminal mode instead of vim
alias vim="gvim -v"
- Put these configs in your
.vimrc
file. More info
:set clipboard=unnamed
:set clipboard=unnamedplus
Install a plugin
Vim has many plugin managers to install and manage plugins, and between these plugin managers, I chose vim-plug because it's straightforward and easy to install.
- To install vim-plug go to the project page in GitHub and follow the instructions.
- Open
.vimrc
file and put your plugin name betweenplug#begin
andplug#end
blocks, like bellow. - Save
.vimrc
file and run:PlugInstall
.
call plug#begin('~/.vim/plugged')
Plug 'airblade/vim-gitgutter' " <github-username>/<repo-name>
" More info: https://github.com/junegunn/vim-plug
call plug#end()
Fuzzy finder
In editors like Atom and VSCode, you can use ctrl + p
to open a fuzzy finder and easily navigate between files.
In Vim, you can do this with these simple steps:
- Install FZF on your system. More info on FZF GitHub page.
- Install FZF Vim plugin by installing it with your Vim plugin manager.
- Add a keyboard mapping to open FZF with
ctrl + p
.
" Use VimPlug
call plug#begin('~/.vim/plugged')
Plug 'junegunn/fzf'
" More info: https://github.com/junegunn/fzf/blob/master/README-VIM.md
call plug#end()
map <C-p> :FZF<CR>
If you don't like FZF you can use ctrlp.vim.
File explorer
In Vim, you can install NERDTree to have a tree explorer, But in version 8 of Vim you can use default NETRW file explorer.
To Open NETRW run :Explore
to open it horizontally and :Vexplore
to open it vertically.
NETRW config:
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 = 20
Use ctrl + n
to open NETRW window like NERDTree(But I don't use this because I'm more comfortable with :Vexplore
command):
map <C-n> :Vex<CR>
Open NETRW when Vim starts:
augroup ProjectDrawer
autocmd!
autocmd VimEnter * :Vexplore
augroup END
Use Vim blockwise visual selection
By using the blockwise visual selection, you can select a block or a rectangle area and edit or delete that. To use this feature, press ctrl + v
and select an area by using hjkl
keys.
For example, to edit the bellow lines and remove numbers from the start of all lines, do this step by step:
- Press
ctrl + v
. - Press
l
four times to go forward. - Press
j
four times to select four bellow lines. - Press
d
to delete.
Before:
0002 D1H060 1 3 11-46 1
0004 D1H520 5 3 27-36 4
0001 D1G554 1 1 21-56 3
0003 D1G049 5 1 23-56 4
0005 D1G004 15 1 29-56 3
After:
D1H060 1 3 11-46 1
D1H520 5 3 27-36 4
D1G554 1 1 21-56 3
D1G049 5 1 23-56 4
D1G004 15 1 29-56 3
EditorConfig
EditorConfig is a plugin for all code editors and IDEs that help you and your teammates to have a contract on how to use tab and space for indentation on different file formats. For example, this EditorConfig configuration tells your Vim to use two space for JavaScript files and four space for Python files in your project.
# https://editorconfig.org/
root = true
[*]
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
end_of_line = lf
charset = utf-8
[*.py]
indent_size = 4
[*.js]
indent_size = 2
To install EditorConfig on Vim, add these lines to your .vimrc
file and run :PlugInstall
.
" Use VimPlug
call plug#begin('~/.vim/plugged')
Plug 'editorconfig/editorconfig-vim'
" More info: https://github.com/editorconfig/editorconfig-vim
call plug#end()
Syntax checking
Syntax checking helps you to correct your mistakes when you are coding. There are many plugins in Vim that give you syntax checking, but between those, I prefer ALE because it's very simple to install and doesn't have many weird dependencies and configuration.
To install ALE, add these lines to your .vimrc
file and run :PlugInstall
.
" Use VimPlug
call plug#begin('~/.vim/plugged')
Plug 'dense-analysis/ale'
" More info: https://github.com/dense-analysis/ale
call plug#end()
Some useful commands:
Simple auto-complete
In Vim, you can have simple auto-complete with pressing ctrl + n
in insert mode to show you a list of matching words and press that again to navigate in popup list.
Indentation
By pressing >
two times, you can indent forward and <
for indent backward, Or select multiple lines with visual mode and press these keys to apply indentation.
Use window splitting
To use window splitting feature in Vim run :vsplit
to split window vertically and run :split
to split the window horizontally.
And to navigate between windows press ctrl + w
then press one of the h
, j
, k
and l
to navigate to other windows.
Use tabs in Vim
To open a new tab in Vim run :tabnew
to open a new tab and use :tabprevious
and :tabnext
to navigate between your tabs or using gt
to go to next tab and using gT
to go to previous tab.
Vim configuration
The best way to have a nice Vim config is to see others .vimrc
file and make your Vim config step by step and don't copy a big config file that you don't need 90% of that and make yourself confusing.
If you need some help you can use my Vim configuration
(Photo by Oskar Yildiz on Unsplash)
Top comments (0)