Vim the ultimate text editor
My preferred text editor is vim
because it is portable and highly customizable. Text editors can be used to edit configuration files on a system and write source files for scripts and programs. While you can run vim in a separate window in your operating system, it can also be run within a terminal emulator. You can even find the original version vi
on most *nix systems that do not have any graphical interface. Learning vim will help make debugging systems that only have a terminal easier.
Installation
You need to install vim
on your system first. You can install vim on any operating system.
For example on Debian Linux based distributions including Ubuntu:
|
|
On RedHat based Linux distributions including RHEL, CentOS, and Fedora:
|
|
And on macOS I recommend using homebrew to install software.
|
|
On windows systems I recommend using Chocolatey to install software including vim.
|
|
When using Chocolately, your system will also have gvim installed which allows you to run vim like any other app. On Linux and macOS you may need to install gvim
separately.
Configurations
After installing vim, I install and open source vimrc files that includes a bunch of plugins. Vim is highly configurable and this is just scratching the surface. By default you can configure vim by customizing a .vimrc
file in your home directory.
Plugins for vim
Prerequisites
In order to install the vim plugins that I use you will need the git
utility on your system.
https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
Note this is entirely optional and if you are just getting started, I recommend NOT installing plugins until you are familiar with the basics.
|
|
Vim plugins go into ~/.vim_runtime/my_plugins/
|
|
Customizing vim further
If you install the awesome_vimrc, custom configs no longer go into the file ~/.vimrc
To add your own configurations to vim when using awesome vimrc, create a new file ~/.vim_runtime/my_configs.vim
My customizations include showing line numbers, applying the downloaded color theme, and enabling transparency if the terminal supports it.
|
|
Upgrading plugins
Here is an example script to update the plugins:
|
|
Next steps
Vim basics
The vim editor can be opened with the command vim
in the terminal. You can specify an existing file as the first argument or vim will create that file if you write the changes.
vim
will open with no current open file.
vim file.foo
will either open file file.foo
or create it if you write the changes.
When you first open vim it will be in Normal
mode. In this mode there are a lot of keyboard shortcuts to navigate the file, search for text, and manipulate text. This is NOT the mode where typing will enter text into the file.
In order to start typing into the open file you want to enter Insert
mode by pressing the
Another mode is Visual
mode by pressing Normal
mode where you can select blocks of text to edit or delete.
In order to exit vim
, press :
to enter a command. The command to quit is :wq
to write changes and quit.
Commands
Here is an overview of the basic commands to remember. For commands, make sure to start with a :
and then press Enter/Return to execute your command.
:w
write changes to file (add a filename to save a new file:w newfile.txt
):q
quit:q!
If you have edited the file you need to enter this to quit without saving changes:help
will bring up the help menu
When in Normal
mode, here are some useful shortcuts:
Basic navigation with arrow keys or jk
for down/up and hl
for left/right
0
will move the cursor to the beginning of the line$
will move the cursor to the end of the linea
will switch to insert mode and move the cursor one character to the rightb
will move the cursor to the beginning of the previous wordw
will move the cursor to the beginning of the next wordo
will create a new line after the current line and move the cursor thered
can be used to delete things. (dw
to delete next word,d$
will delete to the end of the current line, or useVisual
mode to select text and thend
to delete the selected text)dd
will delete an entire line where the cursor isgg
will move the cursor to the beginning of the fileG
will move the cursor to the end of the fileyy
will copy the current line which you can paste elsewhere withp
will paste the vim clipboard (not to be confused with the system clipboard)r
can be used to replace a single character (move the cursor under the character. Pressr
followed by the character you want to replace)x
will delete the character under the cursor
For r
, let’s say the file has the word vin
that is supposed to be vim
. You can fix this in Normal
mode by moving the cursor to n
and then press rm
to replace the n
with a m
.