Using VI for code development
Lately, I have been using VIm emulator in IntelliJ to ease switching apps for writing code. For some languages I use the Ultimate Edition of JetBrains, but when working on my personal projects, I use Visual Studio Code. Trying to remember all the shortcuts in different apps and OS have me made to try to unify all of them using the famous VI emulator. But I wanted to go one step further, and check if I stay developing in Python with the terminal. In this article, I show the plugins I have installed and some problems that I found when setting up vi.
Tools
- I have installed neoVIm stable version as text editor using HomeBrew.
- For having jedi-vim plugin to add code suggestions it is required to installed these tools in pip:
pip3 install neovim pip3 install jedi
Plugin Configuration
" Vim Plug-ins
call plug#begin('~/.vim/plugged')
Plug 'easymotion/vim-easymotion'
Plug 'morhetz/gruvbox'
Plug 'itchyny/lightline.vim'
Plug 'davidhalter/jedi-vim'
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'
Plug 'preservim/nerdtree'
Plug 'christoomey/vim-tmux-navigator'
call plug#end()
" Shortcut to use fzf
map ; :Files<CR>
" Lightline theme
set laststatus=2
set noshowmode
let g:lightline = {
\ 'active': {
\ 'left': [['mode', 'paste' ], ['readonly', 'filename', 'modified']],
\ 'right': [['lineinfo'], ['percent'], ['fileformat', 'fileencoding']]
\ }
\ }
" Gruvbox config
colorscheme gruvbox
let g:gruvbox_contrast_dark = "hard"
" Easymotion config
let mapleader = " "
nmap <Leader>s <Plug>(easymotion-s2)
" Nerdtree config
nmap <Leader>nt :NERDTreeFind<CR>
Easymotion is one of the most useful plugins I found. It allows to move from any point of the code to other, just typing the two first words of the point you want to move.
jedi-vim offers code suggestion for Python, code completition, doc support highlightning, renaming...
Errors I found
When opening a Python file jedi could not initialise
This happened because I had jedi installed in the Python 3 stack, but Mac OS uses by default Python 2. Changing the python executable to point to python3 is not recommended, because python is assumed to be python2 by many applications, so I added the following lines to .vimrc:
" Jedi
let g:jedi#environment_path = "/usr/local/bin/python3"
let g:jedi#force_py_version = 3
Python magic number error when initialising nvim
The jedi-vim directory had some temporal files (.pyc) from executing nvim with Python 2 stack. When trying to execute with Python 3 afterwards, the interpreter could not read the temporal files. The solution to this problem is to delete the .vim/plugged/jedi-vim directory, and reinstall the plugin:
rm ~/.vim/plugged/jedi-vim
nvim
:PlugInstall
Helpful shortcuts
CNT + o
: go to previous movement in the VI history stackCNT + i
: go to the next movement in the VI history stackdw
: delete the next worddb
: delete previous wordu
: undoCNT + r
: redod$
: delete the content of the whole line from the current cursor positionp
: paste in the next lineP
: paste in the previous lineciw
: change inner worldgg
: go to the begining of the fileG
: go to the end of the file2k
: move to lines up. You can combine this method with any amount of lines followed by the movement key (l, k, j, h).gd
: when you put the cursor on a function name, you can move to the function definition.
Search commands
?
: search from the begining of the file/
: search from the cursor positionn
: next resultN
: previous result
Search and replace
%
: move among parenthesiss/the/de/g
: replace 'the' by 'de' in the whole file%s/the/de/g
: repalce 'the' by 'de' in the whole file, and prompt if the replacement want to be applied in the current selection.o
: create a new line in the next lineO
: create a new line in the previous lineR
: replaces several characters at the same time
Visual mode
v
: visual mode, highlights the selectiony
: copyp
: paste
Some notes
- When you delete something, it is saved in the clipboard. So, when remvoing a line with
dd
, you can paste in other point withp
.
References
[course] "Vim, aumenta tu velocidad de desarrollo", https://github.com/stubhub/checkout-purchase/
[jedi-vim] "Jedi-vim", https://github.com/davidhalter/jedi-vim
[jedi] "Jedi", https://github.com/davidhalter/jedi
[neovim] "NeoVim", https://neovim.io/
[easymotion] "Easymotion", https://github.com/easymotion/vim-easymotion