Chose Language:
Author: Admin/Publisher |finished | checked

VIM -text editor

vim logo

Contenido:

Introduction

Vim is a text editor that we can use in any OS, its really configurable and lightweight, two of the reasons why a quantity of developers decides to use it.

Vim installation on Linux

If we try to run vim, and we have not installed, yet we will receive a message like in the following image.

There is a bast list of vim versions, but we will use the classic one in this post. INFO: the classic VIM and NEOVIM are the most chosen ones.

As the message said we run the following comand:

 sudo apt install vim

Finally, we can enter on vim with the vim command.

Let’s take a look at vim modes.

Ok this is a little different compared with another text editors, but this thing behavior it’s really and advantage that we will notice later on this entry.

Insert mode

When vim turn in this mode permit us write, we can enter on this mode with the keyboard insert, if we are already on normal mode it will turn to replace mode. (we can go out of this mode pressing ESC).

Normal

VIM always starts in this mode, in this mode we can navigate throw the archive. If whe are in another mode, we can return to this mode with ESC.

Cursor movements in vim.

  • h move cursor to left.
  • j move cursor one line down
  • k move cursor one line up
  • l move cursor to right

INFO: this keywords may seem strange that’s because vim was designed to not use mouse.

Incredible, in my installed version I can use arrow keys too (,, , ).

Visual Mode

We can enter on visual mode with the keyword v, visual mode allow us to select our text with the cursor movements and take an action before the selection that could be (copy, paste, cut and delete).

  • x – pressing x key you can cut text
  • y– creates a copy of the selected text ( y is for yank )
  • d– deletes the selected text
  • p– paste the selected text

Command-Line Mode

The command line mode probably is the most helpful mode in the entire text editor, we can enter writing : if we are in normal mode.

quit from vim

we can quit from vim with :quit or in short term :q

inside VIM text editor
:q

Writing our file

In order to write a file in vim you can use :write or the short term :w

inside VIM text editor
:w

At the case that we not specify the name of the file vim will take the name that we enter on the console Example:

vim example_name

To specify a name where to save the file add the name after :w command

inside VIM text editor
:w index.php

Also we can do a combination with quit instead of :w and then :quit

inside VIM text editor
:wq

Sometimes we can’t write the file, so we can force the file to quit with ! at the end

Using mutiple tabs on vim

Adding a tab in vim is simple as write :tabe in command mode

inside VIM text editor
 :tabe 

Vim configuration file .vimrc

You may specify vim source file if you wish with :source equaly we can refresh source

inside VIM text editor
:source .vimrc

Another method to refresh the vim source is with :so %

Explorer

Navigate in a project is one of the thing you need to know, you can open the vim explorer with :Explore or with the short term with :e

We can open the parent folder with the following command:

inside VIM text editor
:e ..

Navigating inside the file

Going to determinate line, could be helpful if you are debugging, you can do this with : followed with the number of line where you want to go.

inside VIM text editor
:115

Furthermore, you can select multiple lines, for example :1,10 will select lines 1 to 10.

inside VIM text editor
:1,10

Indenting with command line

You can indent multiple lines in vim with > or < after the selected lines for example:

inside VIM text editor
:1,10>

Makes 1 indentation from line 1 to line 10, equally you can unindent with < at the end.

¿How to search in vim?

Depending on the line you are the search command you will use ? or /

  • ? Will search from the start of file to the line we are
  • / will search from the line we are to the end

Once we search with this we can make a step to the next line pressing n

Help guides in VIM

You can check the help guide with :help or :h, in case you want a specific guide you can write :h guide_name

inside VIM text editor
:h

The .vimrc file.

The .vimrc is the default vim configuration thats includes, plugins, tabulations, line numbers, keymappings and others.

You may want to do the following changes in your .vimrc file.

The double quotes in the .vimrc file are comments, last double quote is not necessary but in this code is used cause the code plugin used in the blog do not recognize the vim comments.

.vimrc file
"---console"
"set line numbers"
set number
"quantity of white characters that is a tabulation"
set tabstop=4
"set the font type" 
"if you can set the font type ypu can change it in your console preferences"
set guifont=Fira\ Code:h12
set nocompatible
"add a line where is the cursor"
set cursorline
"numberline color and background"
highlight LineNr term=bold cterm=NONE ctermfg=DarkGrey ctermbg=NONE gui=NONE guifg=DarkGrey guibg=red

"-----theme ------"
"Im using colorscheme odyssey use what you have, colorschemes can overwrite other configurations"
colorscheme odyssey

Block and unblock VIM

Lot of time we can fall in this problem where you think that VIM gets frezzed or crushed and is not. If you are using console this is what happened you pressed Ctrl + S thats lock scroll on console.

You can unblock this using Ctrl + Q one time pressed you can continue with your job.

Remember that vim saves fie with :w while we are on command-mode

Plugins managers

The plugin mangers are plugins that manage the rest of plugins, its recommended use once instead a combination of them, I recommend to you use vundle or Plug cause are newer than pathogen. We will see a little about this 2 plugin managers.

Vundle Installation

Vundle installation requires git if you have not installed git and do not know what is you could read this article.

Once you have installed git you can run the following command on console.

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

Vundle needs this lines at the top of .vimrc file.

.vimrc file
set nocompatible              " be iMproved, required"
filetype off                  " required"

" set the runtime path to include Vundle and initialize"
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins"
"call vundle#begin('~/some/path/here')"

" let Vundle manage Vundle, required "

Plugin 'VundleVim/Vundle.vim'
" a continuacion puede ser cualquier otro plugin ejemplo"
Plugin 'preservim/nerdtree'
"y asi los plugins que necesitemos"

call vundle#end() "required"
filetype plugin indent on " required"

In case you are using Fish Sheel add this at the end.

.vimrc file
set shell=/bin/bash

You can install the plugins specified inside the vundle from vim using :PluginInstall or we can install those plugins from console with the following command.

vim +PluginInstall +qall

Plug Installation

You can install plug downloading the plug.vim file that’s is in the plugin github page https://github.com/junegunn/vim-plug and paste on the folder ~/.vim/autoload/plug.vim.

We can install it with curl with this command

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

This plugin manager can be installed for vim or neovim (if you want to install for neovim please follow the instructions in the plugin page)

One time the file is its place we can use the plugin.

We have to add a section for plug in .vimrc that start on call plug#begin and ends in call plug#end

.vimrc file
" Specify a directory for plugins
" - For Neovim: stdpath('data') . '/plugged'
" - Avoid using standard Vim directory names like 'plugin'
call plug#begin('~/.vim/plugged')

" On-demand loading
Plug 'scrooloose/nerdtree', { 'on':  'NERDTreeToggle' }

call plug#end()

this is the simples code I could write, as you can observe we define plugins to be installed with Plug ‘plugin-name’ and we have a second part defined by coma and { } these are plugin options.

to install these plugins write :PlugInstall on command-line mode

Plugins que muchos programadores usan en Vim

In this part we will touch what plugins are necessary to start programming with vim editor.

NERD-TREE

Permits us to have an explorer on our vim, lets see an image of this.

We saw how to install this plugin on the plugin manager, but to use some plugins it’s not only necessary install it, we need to configure it too.

You can read how to configure plugins in their respective pages.

Particularly, this is the form how I configure this plugin, because I use Visual Studio Code too. Visual Studio Code permit us to hide and show our explorer with ctrl+b so we will add that to our plug(toggle functionality).

.vimrc file
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif

map <C-b> :NERDTreeToggle <CR>

<C-b> refers to Control + b so when we press control + b do :NERDTreeToogle

Ok, now you may need more map keys to pass throw tabs or sections however you say to it.

.vimrc file
let mapleader= ","

First I’m going to explain this, the mapleader will be a key that will do something in combination of other when we are in normal mode. So lets do some convinations:

.vimrc file
nnoremap <leader>4 :wincmd h <CR>
nnoremap <leader>6 :wincmd l <CR>

noremap it for no recursive and the first n is for normal mode, <leader> in this case is “,” so when I press ,4 done this “:wincmd h” will be executed.

Being in nerdTree section, you can open the nerdTree help just pressing ? equally you can leave this help with ?, With C you can define the selected folder as root, Pressing t opens the selected archive in a new tab, equal as when we see :tabe you can navigate through tabs with g+t

Syntastic

This Plugin checks our syntaxis after we save a file, it’s done automatically. Let’s see a simple error only to see what happens.

sytastic example

Observe that the line with the error is marked with >>, and at the end after the normal mode give us and advice.

bc_modules/paypal/BC_SimplePaypal.php|6 error| unexpected 'use' (T_USE), expecting identifier (T_STRING) or function (T_FUNCTION) or const (T_CONST) or \ (T_NS_SEPARATOR)

Neoclide COC VIM

This plugin probably is one of the best plugins for vim, probably you enjoy more of this if you come from an IDE like Visual Studio Code. It Premit use to use autocompletion and had another options like autoformat with the extensions.

As I said COC can use a lot of extensions, so I publish here the link:

Category: en-others
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category