Μετάβαση στο περιεχόμενο

Vim Usage

Description

Vim is a text editor for Unix that comes with Linux, BSD, and macOS. It is known to be fast and powerful, partly because it is a small program that can run in a terminal. It is mainly because it can be managed entirely without menus or a mouse with a keyboard. It is based on the text editor Vi, but with added features.

Installation

To install Vim, visit here, where there are installation instructions depending on the operating system you use.

Usage

In Vim, there are different Modes for editing a file. The most common Modes are Normal mode (You start in this mode when you open Vim), Insert mode, Visual mode and Command-Line mode.

Normal mode

This mode allows the user to navigate their file, to copy/paste text, to delete parts of text, to undo/redo changes and to execute editing commands in general. The navigation can either be done with the arrow keys, or the Vi/Vim/Neovim way (that is faster), which is by pressing the keys h for left, j for down, k for up and l for right.

Insert mode

This mode allows the user to write/insert text just as other text editors and the user can change into this mode from Normal mode by pressing the i key. To change back into Normal mode, the user can press Esc or Ctrl+C.

Visual mode

This mode allows the user to select and edit parts/blocks of text and the user can change into this mode from Normal mode by pressing v or V. If the user changes into this mode using v, then the character the cursor is selected and with the navigation keys, the user can select words and lines. If the user changes into this mode using V, the line the cursor is on is selected and it also can select lines by navigating, but Not individual words. To change back into Normal mode, the user can press Esc or Ctrl+C.

Command-line Mode

This mode allows the user to execute editing commands for the file that he is editing. To change into this mode from Normal mode, the user must press : key and then type the command that he wants to execute, followed by pressing Enter. For example, the command :%s/<Old text>/<New text>/g changes every occurrence of <Old text> with <New text>. Additionally, the user can run terminal commands by writing :! <command> (ex. :!ls or :!grep -r "search_term" /path/to/search). To change back into Normal mode, the user can press Esc or Ctrl+C.

Saving and exiting Vim

To exit Vim, the user must run the command :q (quit). The user can save files by running :w (write), while saving a file and exiting can be achieved by running the command :wq (write quit). If the user tries to exit without saving the changes, the command :q will fail. In case the user wants to exit without saving, he will have to use the command :q! (force quit) that force exits Vim.

Useful keybinds

Some of the most basic and useful keybinds that the user can use while in Normal mode or Visual mode are the following:

  • d+d : Deletes the line on which the cursor is located and stores it into the default Register.
  • y+y : Copies the line on which the cursor is located into the default Register.
  • /<keyword> : Searches for the pattern <keyword> in the file.
  • y : Copies the selected test into the default register.
  • p : Pastes the content of the default register into line below the cursor.
  • d : Deletes the selected text and saves it into the default register.
  • u : Reverts the latest change in the file (undo).
  • Ctrl+r : Restores the latest reverted change in the file (redo).
  • Shift+z+Shift+z : Saves and closes Vim.
  • g+g : Moves the cursor to the start of the file.
  • Shift+g : Moves the cursor to the end of the file.

For more information on the default keybinds, visit here

Configure Vim

To configure Vim, the user must go to where the configuration (.vimrc) file of Vim is located (usually ~/.vimrc in Linux. If the file doesn't exist. it will have to be created).

This file will store the user's configuration.

If the user does not want to create their own .vimrc from scratch, there are pre-built configurations like this basic vimrc (that is a good starting point for someone to start), this advanced vimrc (that contains a lot of plugins and configured keybinds), etc.

Install the Basic vimrc configuration

In order to install the recommended basic configuration on Linux, the user must follow these steps:

  1. Go into the home directory
    (cd ~)
  2. Clone the repository and put the content into a directory named vim_runtime
    (git clone https://github.com/amix/vimrc vim_runtime)
  3. Run the install script for vimrc basic
    (sh vim_runtime/install_basic_vimrc.sh)

Now that the configuration is copied, the user can can delete the directory named vim_runtime.

Install the Advanced vimrc configuration

In order to install the recommended advanced configuration on Linux, the user must follow these steps:

  1. Go into the home directory
    (cd ~)
  2. Clone the repository and put the content into a directory named vim_runtime
    (git clone https://github.com/captbaritone/dotfiles vim_runtime)
  3. Move the vim folder in vim_runtime into ~/.vim
    (mv ~/vim_runtime/vim ~/.vim)
  4. Move the vimrc file in vim_runtime into ~/.vimrc
    (mv ~/vim_runtime/vimrc ~/.vimrc)
  5. Run vim (vim) to install the plugins and press Enter when prompted.

Now that the configuration is copied, the user can can delete the directory named vim_runtime.

Setting up Options

In Vim, the user can configure an option by adding the following line into their .vimrc.
set {option} where {option} is the option that will be set.
Option example:
set number makes vim show line numbers.

Setting Up Keybinds

Keybinds allow users to bind a keyboard shortcut for quick execution of a command or function.
To add a new keybind, the user must go to the .vimrc file and add the following line:
MODE <KEYBIND> <COMMAND>
MODE is the mode that the keybind will be for (ex. nnoremap for Normal mode, inoremap for Insert mode, vnoremap for visual mode).
KEYBIND is the key combination that needs to be pressed to run the the COMMAND.
COMMAND is the action that Vim will run when the keybind activates.
Keybind example:
nnoremap <f5> :w <CR>:!clear <CR>:!python3 % <CR>
This keybind will save and run the current python file by pressing the F5 key in Normal mode.

Configuring Plugins

Installing a Plugin manager

In order to configure/install plugins, the user must first install a plugin manager, like vim-plug. If the user installed the Advanced vimrc configuration, they can skip this step.

To install vim-plug, the user must add the following lines into the .vimrc (before adding any plugins).

let data_dir = has('nvim') ? stdpath('data') . '/site' : '~/.vim'
if empty(glob(data_dir . '/autoload/plug.vim'))
  silent execute '!curl -fLo '.data_dir.'/autoload/plug.vim --create-dirs  https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
  autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif

Installing a Plugin

The user can install a plugin with vim-plug by adding the following lines into the .vimrc(after installing vim-plug).

call plug#begin()
Plug 'PluginCreator/Plugin'
call plug#end()
Where PluginCreator is the creator of the plugin and Plugin is the plugin that the user wants to install. For example, if the user wants to install the plugin sensible.vim, Plug 'PluginCreator/Plugin' must be replaced with Plug 'tpope/vim-sensible'.