Archive

Archive for the ‘vim’ Category

Install black on VIM

April 26, 2020 Leave a comment

What is black?

Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters.

Blackened code looks the same regardless of the project you’re reading. Formatting becomes transparent after a while and you can focus on the content instead.

Black makes code review faster by producing the smallest diffs possible.

From https://github.com/psf/black

Installation steps
1. Deactivate any active virtual environments you may have running.

$ deactivate

Failure to do may lead to the following errors after completing all the installation steps on the github page.

When running :Black

Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'Black' is not defined

opening a *.py file

Error detected while processing ~/.vim/plugged/black/plugin/black.vim:
line  124:
Traceback (most recent call last):
  File "", line 56, in 
ModuleNotFoundError: No module named 'black'

2. Install a plugin manager. Eg Vundle

$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

3. Edit .vimrc to install plugin.
With Vundle

$ less .vimrc
set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'psf/black'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

" Specify the virtual environment path for black
let g:black_virtualenv='~/.vim/black'
" Run black on save
autocmd BufWritePre *.py execute ':Black'

4. Install the black plugin

$ vim test.py
:PluginInstall

You may get the following error

Done!
Please wait, one time setup for Black.
Creating a virtualenv in /home/vagrant/.vim/black...
(this path can be customized in .vimrc by setting g:black_virtualenv)
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
    apt-get install python3-venv
You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.
Failing command: ['/home/vagrant/.vim/black/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
Error detected while processing /home/vagrant/.vim/bundle/black/plugin/black.vim:
line  194:
E880: Can't handle SystemExit of python exception in vim
Press ENTER or type command to continue

5. Install python3-venv

$ sudo apt-get install python3-venv

6. Recreate the virtual environment

$ rm -rf .vim/black
$ vim test.py
vim test.py
Please wait, one time setup for Black.
Creating a virtualenv in /home/vagrant/.vim/black...
(this path can be customized in .vimrc by setting g:black_virtualenv)
Installing Black with pip...
DONE! You are all set, thanks for waiting ✨ 🍰 ✨
Pro-tip: to upgrade Black in the future, use the :BlackUpgrade command and restart Vim.
Press ENTER or type command to continue

7. Last step of installation

vagrant@vagrant:~$ cd .vim/bundle/black/
vagrant@vagrant:~/.vim/bundle/black$ git checkout origin/stable -b stable
Branch 'stable' set up to track remote branch 'stable' from 'origin'.
Switched to a new branch 'stable'
vagrant@vagrant:~/.vim/bundle/black$ cd ~

8. Test black
Add the following to test.py and write (:w)

j = [1,
2,
3
]

Black should reformat the file in place and give you

j = [1, 2, 3]

You may get the following error

Error detected while processing BufWrite Auto commands for "*.py":
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 92, in Black
  File "", line 128, in get_configs
"test.py" [New] 5L, 26C written
Press ENTER or type command to continue

Try running :Black inside test.py to see the actual error

Traceback (most recent call last):
  File "", line 1, in 
  File "", line 92, in Black
  File "", line 128, in get_configs
AttributeError: module 'black' has no attribute 'find_pyproject_toml'

The above error may occur if you skipped step 7. You could also run https://github.com/psf/black/pull/1273#issuecomment-594114067

System details
– Ubuntu 18.04.3 LTS
– VIM 8.0 with python3 support
vim test.py
:echo has(‘python3’)
1

Source:
https://github.com/psf/black#installation-and-usage
https://github.com/psf/black/issues/672
https://github.com/psf/black/pull/1273

Categories: vim Tags: , ,

5 useful Vim plugins for developers

April 28, 2019 Leave a comment

I have used Vim as a text editor for over 20 years, but about two years ago I decided to make it my primary text editor. I use Vim to write code, configuration files, blog articles, and pretty much everything I can do in plaintext. Vim has many great features and, once you get used to it, you become very productive.

I tend to use Vim’s robust native capabilities for most of what I do, but there are a number of plugins developed by the open source community that extend Vim’s capabilities, improve your workflow, and make you even more productive.

Following are five plugins that are useful when using Vim to write code in any programming language.

1. Auto Pairs
2. NERD Commenter
3. Vim Surround
4. Vim Gitgutter
5. Vim Fugitive

https://opensource.com/article/19/1/vim-plugins-developers

Categories: vim Tags:

Vim as a Python IDE

February 1, 2019 Leave a comment

VIM as a Python IDE

This project aims to use Vim as a powerful and complete Python IDE. In order to do that, we curated a list of awesome plugins available in the community and provided an automatic installation procedure for this set.

https://rapphil.github.io/vim-python-ide/

Source:
https://www.pythonweekly.com/

Categories: Interesting, python, vim Tags:

ALE (Asynchronous Lint Engine)

December 4, 2018 Leave a comment

ALE (Asynchronous Lint Engine) is a plugin for providing linting in NeoVim 0.2.0+ and Vim 8 while you edit your text files, and acts as a Vim Language Server Protocol client.

ALE makes use of NeoVim and Vim 8 job control functions and timers to run linters on the contents of text buffers and return errors as text is changed in Vim. This allows for displaying warnings and errors in files being edited in Vim before files have been saved back to a filesystem.

In other words, this plugin allows you to lint while you type.

https://github.com/w0rp/ale

Categories: vim Tags: ,

Trying out code completion in vim

November 3, 2018 Leave a comment

Trying out code completion in vim. Installed `Youcompleteme`.

Steps:
1. The version of vim on macosx that I had did not have +python3 or +conceal that was needed with some of the completion engines that were installed.

# To check if vim has the needed versions
$ vim --version
+python3 # Supported
-python3 # Not supported

2. Install macvim

$ brew install macvim

Create an alias (Save this to .bashrc or .zshrc)

alias vim='mvim -v'

3. Install cmake

$ brew install cmake

4. Download YouCompleteMe and install using Pathogen

$ cd ~.vim/bundle
$ git clone https://github.com/Valloric/YouCompleteMe.git
$ git submodule update --init --recursive
$ cd YouCompleteMe
$ ./install.py --all

5. Edit some code

Source:
https://github.com/Valloric/YouCompleteMe
http://liuchengxu.org/posts/use-vim-as-a-python-ide/

Categories: vim Tags:

Different tabs for different files

September 14, 2018 Leave a comment

Problem:

Needed to have different tabs for different files when editing with vim. Eg in python tab == 4 spaces, in a MakeFile tab == tab.

Solution:

Added this to ~/.vimrc

" Use different indentation for yaml files
 autocmd FileType yaml,yml setlocal tabstop=2 expandtab shiftwidth=2 softtabstop=2

 " In makefiles, don't expand tabs to spaces, since actual tab characters are
 " needed, and have indentation at 8 chars to be sure that all indents are tabs
 autocmd FileType make set noexpandtab shiftwidth=8 softtabstop=0

Source:
https://superuser.com/questions/632657/how-to-setup-vim-to-edit-both-makefile-and-normal-code-files
https://github.com/spf13/spf13-vim/issues/789#issuecomment-118369177

Categories: vim Tags:

Clear highlighting on escape in normal mode

December 22, 2016 Leave a comment

Problem:
How to disable highlighted text after doing a search in vim?

Solution:
Added the following to my .vimrc

 " Clear highlighting on escape in normal mode
 nnoremap <esc> :nohlsearch<return><esc>
 nnoremap <esc>^[ <esc>^[

System details:
vagrant box running ubuntu/trusty64

Source:
http://stackoverflow.com/questions/657447/vim-clear-last-search-highlighting?rq=1

Categories: vim Tags: ,

Custom Colors

December 21, 2016 Leave a comment

Problem:
I had set up my vim to run flake8 when I save python files. The issue now was that the quickfix window would display the errors in white/white-grey text on a yellow background. That proved to be really hard to read.

white on yellow

Solution:
1. One option was to change my colorscheme to something that would display the text. (Many hours later I still had not found a colorscheme that I liked.)
2. Change the color for just that “aspect”.

So I went with option 2 and changed the color. Which resulted in black on yellow and is now much easier to read.
screen-shot-2016-12-21-at-22-13-58

Steps:
While in vim look at the current colors that are already set. (:highlight or :hi)

:highlight

highlight

The colors I wanted to change were for ‘Search’. So while still in vim

:highlight Search term=reverse ctermbg=11 ctermfg=0  guibg=Yellow

To make this permanent I added the following to .vimrc

highlight clear
highlight Search term=reverse ctermbg=11 ctermfg=0  guibg=Yellow

Source:
http://andrewradev.com/2011/08/06/making-vim-pretty-with-custom-colors/
http://superuser.com/questions/654919/how-to-turn-off-gvim-error-highlighting

Categories: vim Tags:

QuickFix

December 21, 2016 Leave a comment

quickfix-window

QuickFix is designed to display compiler errors — a list of errors is displayed and can be selected to jump to a specific file and line.

:cn — Go to the next location in the list
:cp — Go to the previous location
:ccl — Close the quickfix window

Depending on where your quickfix window is. You can use below keys to move in and out of the window. (If quickfix window is at the bottom part of your screen)
CTRL-w + j — Move into the quickfix window
CTRL-w + k — Move out of the quickfix window

The window commands will work here. See link in source section below for more details.

Source:
http://usevim.com/2012/08/24/vim101-quickfix/
http://vimdoc.sourceforge.net/htmldoc/quickfix.html#quickfix
http://vimdoc.sourceforge.net/htmldoc/windows.html#windows

Categories: python, vim Tags:

Tab navigation

December 20, 2016 Leave a comment

In normal mode, you can type:

gt go to next tab
gT go to previous tab
{i}gt go to tab in position i

Note that the gt command counts from one. That means 3gt will jump to the third tab. Also note is 0gt and 1gt mean the same thing: jumping to the first tab.

http://vim.wikia.com/wiki/Using_tab_pages

Categories: vim Tags: ,