How to access/view Python help when using vim

I am a new Vim text editor user. I am writing Python code. Is there is a way to see Python documentation within vim and without visiting the Internet? Say my cursor is under the print Python keyword, and I press F1. I want to look at the help for the print keyword. How do I show python help() inside vim? How do I call pydoc3/pydoc to seek help without leaving vim?

The pydoc or pydoc3 command show text documentation on the name of a Python keyword, topic, function, module, or package, or a dotted reference to a class or function within a module or module in a package. You can call pydoc from vim itself. Let us see how to access Python documentation using pydoc within vim text editor.

Access python help using pydoc

The syntax is:
pydoc keyword
pydoc3 keyword
pydoc len
pydoc print

Edit your ~/.vimrc:
$ vim ~/.vimrc
Append the following configuration for pydoc3 (python v3.x docs). Create a mapping for H key that works in normal mode:

nnoremap <buffer> H :<C-u>execute "!pydoc3 " . expand("<cword>")<CR>

Save and close the file. Open vim text editor:
$ vim
Write some code:

print("Hello world")

Position cursor under the print Python keyword and press Shift followed by H. You will see output as follows:

Gif.01: Press H to view help for the print Python keyword

How to view python help when using vim

jedi-vim is a VIM binding to the autocompletion library Jed. It can do many things including display help for keyword when you press Shift followed by K i.e. press capital K.

How to install jedi-vim on Linux or Unix-like system

Use pathogen, vim-plug or Vundle to install jedi-vim. I am using Vim-Plug. Add the following line in ~/vimrc:
Plug 'davidhalter/jedi-vim'
Save and close the file. Start vim and type:
On Arch Linux, you can also install jedi-vim from official repositories as vim-jedi using pacman command:
$ sudo pacman -S vim-jedi
It is also available on Debian (?8) and Ubuntu (?14.04) as vim-python-jedi using apt command/apt-get command:
$ sudo apt install vim-python-jedi
On Fedora Linux, it is available as vim-jedi using dnf command:
$ sudo dnf install vim-jedi
Jedi is by default automatically initialized. So no further configuration needed on your part. To see Documentation/Pydoc press K. It shows a popup with assignments:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum