[ JagoPG Site ]
This publication is more than a year old. The information can be out-dated.

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 stack
  • CNT + i: go to the next movement in the VI history stack

  • dw: delete the next word

  • db: delete previous word
  • u: undo
  • CNT + r: redo
  • d$: delete the content of the whole line from the current cursor position

  • p: paste in the next line

  • P: paste in the previous line

  • ciw: change inner world

  • gg: go to the begining of the file

  • G: go to the end of the file

  • 2k: 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 position
  • n: next result
  • N: previous result

Search and replace

  • %: move among parenthesis
  • s/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 line

  • O: create a new line in the previous line

  • R: replaces several characters at the same time

Visual mode

  • v: visual mode, highlights the selection
  • y: copy
  • p: 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 with p.

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