VIM

VIM is a command line based text editor, Its Open Source and higly configurable. its a very popular editor among linux developers and people who use command line to edit and modify files.

Screenshot

Installation

 sudo apt-get install vim

Open a file using vim

vim <file>

VIM Modes

vim Support two modes

  • Normal Mode
  • Command Mode
  • Insert Mode
  • Visual Mode

Command Mode

To run any command in Vim user has to enter command mode. press esc to Switch to command mode. below are the list of commands.

  • Save File

    :w
    
  • Close current open file

    Make sure the file has no unsaved changes

    :q
    
  • Save and then Close current open file

    :wq
    
  • Close file without saving

    :q!
    
  • Enable line number

    :set nu
    
  • Disable line number

    :set nu!
    
  • Do not add EOF while saving new file

    By-default vim will add EOF (End Of File) to every file while closing, so if you wanted to avoid it then

    :set nofixendofline
    
  • Auto indentation

    The Vim editor has a autoindent option which indents each line the same the previous one. The vim editor does a much better job of indentation. The cindent option is set with the command:

    :set cindent
    

    This turns on language based style indentation. Each new line will be automatically indented the correct amount according to the language specific indentation standard.

  • Basic search and replace

    • Search a string foo partially

      /foo
      
    • Enable incremental searching

      :set incsearch
      

      With incremental searches enable, Vim will start searching when you type the first character of the search string. As you type in more characters, the search is refined.

    • Highlight the search string

      To Enable Highlight

      :set hlsearch
      

      To Disable disable Highlight

      :set nohlsearch
      
    • Search a exact string

      /\<string-to-search\>
      

      enclose your search string between \< and \>

    • Search a string having start after a pattern(it is partially to last) then use

      /pattern\<string-to-search\>
      

      for example: /a <char> will matches with a char, na char, char, visa char

      NOTE: this can be also done by short kut key. To use this, place the cursor at that string u want to search and press ctrl + #

    • Search special character

      /\special-character
      

      Note: there are no spece between / and \

    • Find each occurrence of 'foo' (in all lines), and replace it with 'bar'.

      :%s/foo/bar/g
      
    • Find each occurrence of 'foo' (in the current line only), and replace it with 'bar'.

      :s/foo/bar/g
      
    • Change each 'foo' to 'bar', but ask for confirmation first.

      :%s/foo/bar/gc
      
    • Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation.

      :%s/\<foo\>/bar/gc
      
    • Change each 'foo' (case insensitive due to the i flag) to 'bar'; ask for confirmation.

      :%s/foo/bar/gci
      

      or

      :%s/foo\c/bar/gc
      

      \c makes the search case insensitive.

    • This may be wanted after using :set noignorecase to make searches case sensitive (the default).

      :%s/foo/bar/gcI
      
    • Change each 'foo' (case sensitive due to the I flag) to 'bar'; ask for confirmation.

      :%s/foo\C/bar/gc
      

      is the same because \C makes the search case sensitive. This may be wanted after using :set ignorecase to make searches case insensitive.

Normal Mode

vim always starts with normal mode. we can move the cursor in this mode but we can not edit file. we can switch to normal mode by pressing ESC.

  • Moving in code block

    Move cursor end of current function

    ]]
    

    Move cursor starting of current function

    [[
    

    Move between matching (), {} and []

    %
    
  • Move to stating of file

    Press gg to move cursor at starting/first-line of a file

  • Move End of a file

    Press G or shift+g to move cursor at end/last-line of a file

  • copy and paste a block of code

    To copy a block of code, place cursor at starting of code block then enter number of lines you wanted to copy and then press yy to copy.

    To paste a block of code, place cursor where you wanted to paste the code and then press p.

    • Example 1: to copy 1 line, move cursor to that line and then press yy. to paste this, move cursor where you want to paste and then press p

    • Example 2: To copy 5 line, move cursor to that to the first line and press 5 then press yy. to paste this, move cursor where you want to paste and then press p

  • move a block of code

    To move a block of code, place cursor at starting of code block then enter number of lines you wanted to move and then press dd. move cursor where you wanted to move the code block and then press p.

  • browse man page

    place the cursor at the name of command or function to search in man page and use

    K or shift+k

    It will jump to man page, to exit from man page press q and then Enter.

  • autoindent whole file

    Press shift+g then press = and then press gg

  • Show Current File name

    Press ctrl+g, It will show the current filename in command area

Insert Mode

In this mode we can edit the file

Visual Mode

Set Shift width

 set shiftwidth=4

Set Tab Length

 set tabstop=4

Set Color for Colume boundry

 set colorcolumn=80

vimrc file

vim will use language related file for syntex higlighting, indentation ... etc.

global language syntax file is located in /usr/share/vim/vim<version>/syntax/

NOTE: if your vim version is 8.2 then location will be /usr/share/vim/vim82/syntax/

for example: c language file will be /usr/share/vim/vim82/syntax/c.vim

if you want to customized language related file then make your own ~/.vim/syntax/c.vim and add syntax for your own. (override)

For example: if you want to add new atoms with name RCULOCK then use below patch

	- syn keyword cTodo       contained TODO FIXME XXX 
	+ syn keyword cTodo       contained TODO FIXME XXX RCULOCK

set the size of indentation

for four space indentation

:set sw=4

Add indentation guide/line

:set listchars=tab:\|\ 
:set list

Enable syntax

:syntax on

Disable syntax

:syntax off

find a variable / function name and use them

ctrl+p -used to find variable/function after cursor ctrl+n -used to find variable/function above cursor

use:
	eg: if u want to search a variable name packet
		then type pa and press ctrl+p or ctrl+n
		according to requirment.

go to starting of a function or go to end of a function (i.e matching parentheses)

put the cursor at any parentheses and press %

split the editor into multiple windows(vsplit / hsplit) and open other file

or varticle split: :vsplit for horizontal split :split to switch window use ctrl+w direction-key
note:- direction key are h : for left l : right j : down k : up to open another file with splite use :split file-name for horizontal split :vsplit file-name for verticle split to open file after splite use :edit file-name to copy between multiple windows use yy for copy and p for paste. To close all windows excluding cursor windows use :only to close cursor windows use :hide to readonly splite windows use :sview file-name to edit another file use :e file-name to list all open windows( i.e buffers) use :ls to switch or open windows( i.e buffer ) use :b buffer-number to maximize current windows use ctrl-w_ to make all windows equal size use ctrl-w=

jump at a specific line

:line-number

enable and disable syntax hylighting

for enable syntax: :syntax on for disable syntax: :syntax off

Abbreviations

in insert mode
	:iab abbreviation string-to-fill-in-place-of-abbreviations
	example
		:iab #i #include
		if in insert mode, u type #i and then press a enter or space then 				automatically #i string change into #include. This methood is used 			for speed typing. Here some example.
	:iab #b /****************************************
	:iab #e ^V^H************************************/
to remove a single abbreviations use
	:una abbreviation
to remove all abbreviations use
	:abc

execute commands (without leaving editor)

:!command-to-execute

To delate all lines of file

use gg to move the cursor at the first line and then type dG

Refrences


©2023-2024 rculock.com