VIM editor and configuration for python editing

VIM editor and configuration for python editing

2020, May 12    

After I was introduced to VIM at my first company, I started to discover how amazing it was for text editing, particularly code. I used to work on a huge C codebase that was built over 20 years, all in the comfort of VIM. Over the years, I’ve changed companies, worked with different languages and codebases, but the comfort of VIM made me stick with it.

Many beginners are reluctant to switch from their favorite IDEs to VIM, mostly because they are scared of a long list of commands to remember and that would hamper their productivity at the workplace. But, the ones who took a step to start small, realized that it will be a lot more productive in terms of text editing and most of the commands are a motor memory. The best part of using VIM is you don’t have to move out of the comfort of your keyboard. You could accomplish text editing tasks without reaching out for a mousepad or mouse just with a small set of key combinations.

VIM is a modal editor meaning everything you do with VIM, you do it in a specific mode. The most common ones are insert mode, normal(escape), command mode. There are other modes as well, which are for advanced text editing. A couple of them are visual and block modes. Explore these once you are comfortable with the basic modes.

You can download and install VIM directly or can use one of the package managers of your OS. Once installed you can check if it’s installed via the command in terminal.

vim --version

You can open a specific file in the VIM editor via a command like below.

vim distributedstack/index.html

This will put you in normal/escape mode, let’s get in detail about this mode later on. To start editing the text, just press the i key. Now you can add text, for now, use the arrow keys for navigation in the file. Once you are done editing, press the Esc key. This will put you in normal mode. Now press :, which will take you to command mode, and press w and then enter key. You just gave VIM command to save the buffer. Similarly, if you press wq in command mode, VIM will save the current buffer and quit.

Below is my VIM configuration file(.vimrc). Some of the commands are commented, not using them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
syntax on
set nocompatible
"set guifont=Monaco:h20
set path=$HOME/Repos/**
set backspace=indent,eol,start
set shiftwidth=2
set expandtab
set smartindent
set autoindent
set laststatus=2
set encoding=utf-8
set nu
set hlsearch
set incsearch
set cc=80
noremap <F9> :vertical botright copen 60<cr>
noremap <F10> :copen 40<cr>
au BufNewFile,BufFilePre,BufRead *.md set syntax=off


" Plugins
call plug#begin('~/.vim/plugged')
"Plug 'Valloric/YouCompleteMe', { 'do': './install.py --tern-completer' }
Plug 'davidhalter/jedi-vim'
Plug 'dracula/vim'
Plug 'cocopon/iceberg.vim'
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'
Plug 'tpope/vim-fugitive'
Plug 'airblade/vim-gitgutter'
Plug 'scrooloose/nerdtree'
Plug 'majutsushi/tagbar'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
call plug#end()

function! GitBranch()
  return system("git rev-parse --abbrev-ref HEAD 2>/dev/null | tr -d '\n'")
endfunction

function! StatuslineGit()
  let l:branchname = GitBranch()
  return strlen(l:branchname) > 0?'  '.l:branchname.' ':''
endfunction

set mouse=r

colorscheme iceberg
set statusline=
set statusline+=%#PmenuSel#
set statusline+=%{StatuslineGit()}
set statusline+=%#LineNr#
set statusline+=\ %f
set statusline+=%m\
set statusline+=%=
set statusline+=%#CursorColumn#
set statusline+=\ %y
set statusline+=\ %{&fileencoding?&fileencoding:&encoding}
set statusline+=\[%{&fileformat}\]
set statusline+=\ %p%%
set statusline+=\ %l:%c
set statusline+=\

" Airline configuration
if !exists('g:airline_symbols')
  let g:airline_symbols = {}
endif

let g:airline_powerline_fonts = 1
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#formatter = 'default'
let g:airline#extensions#tabline#left_sep = ''
let g:airline#extensions#tabline#right_alt_sep = ''
let g:airline_left_sep=''
let g:airline_right_sep=''

let mapleader = " "
map <leader>t <C-t>
map <C-o> :e **/
map gh :GitGutterLineHighlightsToggle<cr>
map nt :NERDTreeToggle<cr>
map B :CtrlPBuffer<cr>
map <C-h> :e ~/.vim-readme.md<cr>
map <C-j> :bn<cr>
map <C-k> :bp<cr>
map cf :bd %<cr>
map J :cn<cr>
map K :cp<cr>
map <C-p> :Files<CR>
map <S-p> :Rg<CR>

" Switching between buffers
" Set commands to switching between buffers
:nnoremap <Tab> :bnext!<CR>
:nnoremap <S-Tab> :bprevious!<CR>
:nnoremap <C-X> :bp<bar>sp<bar>bn<bar>bd<CR>
"copy relative file path
:nmap cp :let @+ = expand("%")<CR>

" Start autocompletion after 4 chars
"let g:ycm_min_num_of_chars_for_completion = 4
"let g:ycm_min_num_identifier_candidate_chars = 4
"let g:ycm_enable_diagnostic_highlighting = 0
" Don't show YCM's preview window [ I find it really annoying ]
"set completeopt-=preview
"let g:ycm_add_preview_to_completeopt = 0

You might also like

Brief tutorial on Django web framework | 31 Jul 2020 - 3 minute read
Overriding Django Model fields for good | 14 May 2020 - 2 minute read
Overriding Django Model fields for good | 14 May 2020 - 2 minute read
Backend developer roadmap, skills, resources | 26 Jul 2020 - 4 minute read
Overriding Django Model fields for good | 14 May 2020 - 2 minute read