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

VIM Modes

VI Support two modes

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

Command Mode

Normal Mode

  • 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

  • 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.

Insert Mode

Visual Mode

vim not to add end-of-file

:set nofixendofline

Set Shift width

 set shiftwidth=4

Set Tab Length

 set tabstop=4

Enable line Numbering

 set nu

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

Auto indentation for c program

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 C style indentation. Each new line will be automatically indented the correct amount according to the C indentation standard.

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

Search a string partially

/string-to-search

incremental searching

incremental searches will be done. The Vim editor will start searching when you type the first character of the search string. As you type in more characters, the search is refined. To use this feature use

:set incsearch

higlite the search string

for hilighting

:set hlsearch

for disable hilight

:set nohlsearch

Search a exact string

/\<string-to-search\>

if you want to search a string having start after a pattern(it is partially to last) then use

/pattern\<string-to-search\>

for example:
emsp /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 #

search special character

/\special-character

Note:- there are no spece between / and \

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

alignment

G=gg

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

ctags with vim

1.Run Ctags recursively on your project-root-directry to generate the tags file. ctags -R

2.To search for a specific tag and open Vim to its definition, run the following 		command in your shell:
	vim -t <tag-name>
3.Or, open any Linux source file in Vim and use the following basic 				
commands:
Keyboard command

Action Ctrl-] Jump to the tag underneath the cursor :ts Search for a particular tag :tn Go to the next definition for the last tag :tp Go to the previous definition for the last tag :ts List all of the definitions of the last tag Ctrl-t Jump back up in the tag stack

Basic search and replace

:%s/foo/bar/g
    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/gc 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/gci Change each 'foo' (case insensitive due to the i flag) to 'bar'; ask for confirmation. :%s/foo\c/bar/gc is the same because \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.

https://stackoverflow.com/questions/4097259/in-vim-how-do-i-highlight-todo-and-fixme


©2023-2024 rculock.com