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.
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>
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 --versionand check if you see
clipboard. If you see
+, It means your installation of Vim support
clipboard, Otherwise, you must install a Vim version, which supports that. I'm using Debian and in
apt-getrepository I couldn't find a version that supports this, So I installed
gvimand add this alias to my
.zshrcfile to prevent
gvimopen 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
.vimrcfile. More info
:set clipboard=unnamed :set clipboard=unnamedplus
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.
.vimrcfile and put your plugin name between
plug#endblocks, like bellow.
.vimrcfile and run
call plug#begin('~/.vim/plugged') Plug 'airblade/vim-gitgutter' " <github-username>/<repo-name> " More info: https://github.com/junegunn/vim-plug call plug#end()
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.
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.
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
ctrl + n to open NETRW window like NERDTree(But I don't use this because I'm more comfortable with
map <C-n> :Vex<CR>
Open NETRW when Vim starts:
augroup ProjectDrawer autocmd! autocmd VimEnter * :Vexplore augroup END
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
For example, to edit the bellow lines and remove numbers from the start of all lines, do this step by step:
ctrl + v.
lfour times to go forward.
jfour times to select four bellow lines.
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
# 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
" Use VimPlug call plug#begin('~/.vim/plugged') Plug 'editorconfig/editorconfig-vim' " More info: https://github.com/editorconfig/editorconfig-vim call plug#end()
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
" Use VimPlug call plug#begin('~/.vim/plugged') Plug 'dense-analysis/ale' " More info: https://github.com/dense-analysis/ale call plug#end()
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.
> two times, you can indent forward and
< for indent backward, Or select multiple lines with visual mode and press these keys to apply indentation.
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
l to navigate to other windows.
To open a new tab in Vim run
:tabnew to open a new tab and use
:tabnext to navigate between your tabs.
But if you want, you can add these configs to your
.vimrc file to navigate between tabs with
ctrl + l and
crtl + h.
map <C-h> :tabprevious<CR> map <C-l> :tabnext<CR>
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)