I like to use at least two editors: A full featured editor such as Atom, for most of my everyday coding needs (when I’m not in a Jupyter Notebook), and a second lightweight editor that I can use to make fast edits. For my lightweight editor, I use Vim. Vim is a command line based code editor, depending on how deep you dive into Vim, it is either a lightweight editor for small edits or a full featured code editor with the ability to drastically increase your productivity. In this article I’m going to go through a few add-on’s and modifications that I use to improve my Vim experience.
Locating your vim configuration file.
Some of you may be familiar with a
.bashrc file. The
vimrc file works in a similar manner, you can use it to add functionality and change your vim preferences. You can navigate to the file by simply typing:
If the file is not found, that might mean that you do not have Vim installed on your computer (it usually comes pre-installed on many computers). If you need to download Vim, please follow these instructions based on your operating system.
Mapping F9 to run the current script.
The most useful modification i’ve made to my Vim config was to map the F9 key to run the current script I’m working on. To do this, just open your ~/.vimrc and paste the following code towards the top of the file.
autocmd FileType python map <buffer> <F9> :w<CR>:exec '!python3' shellescape(@%, 1)<CR>
Now, you can create a new
.py file, write some code, and hit F9. Your code will now run! If you want to get back to the editor you can hit Escape.
Fixing automatic spacing.
If you have never messed around with your
vimrc before, then you have a clean slate configuration. This can make Vim seem like a tedious option when it comes to editing code. One area where this is apparent is with spaces. By default, Vim spacing will not act like you will expect, and you will spend a lot of time hitting the space bar and tab key to keep your cursor where you want it to be. To make things a little more intuitive, i’ve set some spacing options:
set expandtab " enter spaces when tab is pressed
set textwidth=120 " break lines when line length increases
set tabstop=4 " use 4 spaces to represent tab
set shiftwidth=4 " number of spaces to use for auto indent
set autoindent " copy indent from current line when starting a new line
" make backspaces more powerful
set ruler " show line and column number
set showcmd " show (partial) command in status line
Copy and paste these options into your Vimrc to add intuitive spacing options, line numbers, and a command status line.
Turn on syntax highlighting.
I am a big fan of syntax highlighting. It just makes code easier to read and scan through. Turning on Syntax highlighting is easy in Vim, all you have to do is add the following command to your
What I’ve described above are 3 very simple modifications that you can make to your
vimrc in order to make Vim a more enjoyable “quick and dirty” code editor. There is a LOT to learn if you want to become an efficient user of Vim. You can find a list of Vim commands here, if you learn these you will drastically increase your efficiency using the program. Also there are many vim add on’s such as NERDTree and powerline, which I also suggest you download and add to your personal vim configuration!
Check out my website 💻!