2.1 Vim

Vim

重点推荐实用PyCharm。介绍Vim的内容,可看可不看。

Vim, short for Vi IMproved, is a configurable text editor often used as a Python development environment. Vim proponents commonly cite the numerous plugins, Vimscript and logical command language as major Vim strengths.

Why is Vim a good Python development environment?

Vim’s philosophy is that developers are more productive when they avoid taking their hands off the keyboard. Code should flow naturally from the developer’s thoughts through the keyboard and onto the screen. Using a mouse or other peripheral is a detriment to the rate at which a developer’s thoughts become code.

Vim has a logical, structured command language. When a beginner is learning the editor she may feel like it is impossible to understand all the key commands. However, the commands stack together in a logical way so that over time the editor becomes predictable.

If you’re interested in Vim you’ll also want to read about development environments for coding and learn what makes Python a great programming language.

Configuring Vim with a Vimrc file

The Vimrc file is used to configure the Vim editor. A Vimrc file can range from nothing in it to very complicated with hundreds or thousands of lines of configuration commands.

Here’s a short, commented example .vimrc file I use for Python development to get a feel for some of the configuration statements:

" enable syntax highlighting
syntax enable

" show line numbers
set number

" set tabs to have 4 spaces
set ts=4

" indent when moving to the next line while writing code
set autoindent

" expand tabs into spaces
set expandtab

" when using the >> or << commands, shift lines by 4 spaces
set shiftwidth=4

" show a visual line under the cursor's current line
set cursorline

" show the matching part of the pair for [] {} and ()
set showmatch

" enable all Python syntax highlighting features
let python_highlight_all = 1

Here is how these configuration options look with a dark background on Mac OS X while editing the markdown for this webpage (how meta!).

Vim with basic configuration options on a dark background.

Take a look at another example using these configuration options, this time with a light background and editing Python code from my Choose Your Own Adventures Presentations project.

Vim with basic configuration options on a white background.

The Vimrc file lives under the home directory of the user account running Vim. For example, when my user account is ‘matt’, on Mac OS X my Vimrc file is found at /Users/matt/.vimrc. On Ubuntu Linux my .vimrc file can be found within the /home/matt/ directory.

If a Vimrc file does not already exist, just create it within the user’s home directory and it will be picked up by Vim the next time you open the editor.

Vim tutorials

Vim has a reputation for a difficult learning curve, but it’s much easier to get started with these tutorials.

  • Learn Vim Progressively is a wonderful tutorial that follows the path I took when learning Vim: learn just enough to survive with it as your day-to-day editor then begin adding more advanced commands on top.
  • A vim Tutorial and Primer is an incredibly deep study in how to go from beginner to knowledgeable in Vim.
  • Why Atom Can’t Replace Vim discusses one of Vim’s core principles: command composability. Vim has a language where simple commands are combined to execute more advanced operations. For example, in command mode,$ moves to the end of a line. When $ is preceded by d then everything to the end of the line is deleted. Over time the simple commands become intuitive and the combinations become more powerful than having distinct commands such as a drop-down menu with a specific option to delete all text until the end of the line.
  • Vim as a Language explains the language syntax and how you can build up over time to master the editor.
  • How to install and use Vim on a cloud server along with How to use Vim for advanced editing of code on a VPS are two detailed Digital Ocean guides for getting up and running with Vim, regardless of whether you’re using it locally or on a cloud server.
  • Vim Adventures is a cute, fun browser-based game that helps you learn Vim commands by playing through the adventure.
  • In Vim: revisited the author explains his on-again off-again relationship with using Vim. He then shows how he configures and uses the editor so it sticks as his primary code editing tool.
  • Why Atom Can’t Replace Vim explains how the composability of Vim and the extensibility of Emacs allow both editors to remain relevant and very useful in the 21st century.
  • Things About Vim I Wish I Knew Earlier explores the lessons one developer learned while exclusively using Vim for several years. The author includes using relative instead of absolute line numbering, setting numerous configuration options and fuzzy finding to quickly open files in other directories rather than expanding the whole path.

Vimrc resources

These are a few resources for learning how to structure a .vimrc file. I recommend adding configuration options one at a time to test them individually instead of going whole hog with a Vimrc you are unfamiliar with.

Vim installation guides

These installation guides will help you get Vim up and running on Mac OS X, Linux and Windows.

  • Upgrading Vim on OS X explains why to upgrade from Vim 7.2 to 7.3+ and how to do it using Homebrew.
  • The easiest way to install Vim on Windows 7+ is to download and run the gvim74.exe file.
  • On Linux make sure to install the vim package with sudo apt-get install vim.
  • If you’re using PyCharm as your IDE you won’t need to install Vim as a separate text editor – instead use the IdeaVim PyCharm plugin to get Vim keybindings, visual/insert mode, configuration with ~/.ideavimrc and other Vim emulation features.

Using Vim as a Python IDE

Once you get comfortable with Vim as an editor, there are several configuration options and plugins you can use to enhance your Python productivity. These are the resources and tutorials to read when you’re ready to take that step.

  • VIM and Python – a Match Made in Heaven details how to set up a powerful VIM environment geared towards wrangling Python day in and day out.
  • The python-mode project is a Vim plugin with syntax highlighting, breakpoints, PEP8 linting, code completion and many other features you’d expect from an integrated development environment.
  • Vim as Your IDE discusses how to set up Vim for greater productivity once you learn the initial Vim language for using the editor.
  • Vim as a Python IDE goes through the steps necessary to make Vim into a more comfortable environment for Python development.
  • Setting up Vim for Python has a well written answer on Stack Overflow for getting started with Vim.
  • If you’re writing your documentation in Markdown using Vim, be sure to read this insightful post on a Vim setup for Markdown.

Vim Plugin Managers

  • Vundle comes highly recommended as a plugin manager for Vim.
  • Pathogen is a widely used plugin manager.
  • Vim-plug bills itself as a minimalistic Vim plugin manager.

Vim Plugin resources

发表评论

电子邮件地址不会被公开。 必填项已用*标注