Thursday, July 11, 2019

World of Text Editing with Vi & Vim

'vi' written in 1976, become the part of BSD UNIX in 1978. According to the Linux Journal survey, even without GUI, it is the most popular editor, and GUI GNOME editor comes second. The reason behind its popularity is its permissiveness, convenience, performance, and speed.

Vim (vi Improved) released in 1991 by Bram Moolenaar, targeting Amiga systems. It is distributed as a vim-enhanced package with GUI frontend. The most notable improvement over 'vi' is its syntax highlighting feature for languages such as PHP, PERL, and PYTHON.

$ alias | grep vi alias vi='vim'

Vim needs 'vim-XII' package for the graphical interface. You can install it with the following command:

$yum install vim-XII

Edit file using vi or vim

$vi <file-name>

$gvim <file-name>

$vimx -g <file-name> (without -g option it starts normal vim program)

Control Functionalities & Customize Appearance

/etc/vimrc  for all users i.e., system-wide settings

~/.vimrc for each user

By Default Values in /etc/vimrc

if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
   set fileencodings=ucs-bom,utf-8,latin1
endif

set nocompatible        " Use Vim defaults (much better!)
set bs=indent,eol,start         " allow backspacing over everything in insert mode
"set ai                 " always set autoindenting on
"set backup             " keep a backup file
set viminfo='20,\"50    " read/write a .viminfo file, don't store more
                        " than 50 lines of registers
set history=50          " keep 50 lines of command line history
set ruler               " show the cursor position all the time

" Only do this part when compiled with support for autocommands
if has("autocmd")
  augroup redhat
  autocmd!
  " In text files, always limit the width of text to 78 characters
  " autocmd BufRead *.txt set tw=78
  " When editing a file, always jump to the last cursor position
  autocmd BufReadPost *
  \ if line("'\"") > 0 && line ("'\"") <= line("$") |
  \   exe "normal! g'\"" |
  \ endif
  " don't write swapfile on most commonly used directories for NFS mounts or USB sticks
  autocmd BufNewFile,BufReadPre /media/*,/run/media/*,/mnt/* set directory=~/tmp,/var/tmp,/tmp
  " start with spec file template
  autocmd BufNewFile *.spec 0r /usr/share/vim/vimfiles/template.spec
  augroup END
endif

if has("cscope") && filereadable("/usr/bin/cscope")
   set csprg=/usr/bin/cscope
   set csto=0
   set cst
   set nocsverb
   " add any database in current directory
   if filereadable("cscope.out")
      cs add $PWD/cscope.out
   " else add database pointed to by environment
   elseif $CSCOPE_DB != ""
      cs add $CSCOPE_DB
   endif
   set csverb
endif

" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
  syntax on
  set hlsearch
endif

filetype plugin on

if &term=="xterm"
     set t_Co=8
     set t_Sb=^[[4%dm
     set t_Sf=^[[3%dm
endif

" Don't wake up system with blinking cursor:
" http://www.linuxpowertop.org/known.php
let &guicursor = &guicursor . ",a:blinkon0"


Turn Line Numbering ON/OFF

To enable line numbering add the following command in a file:

vi ~/.vimrc

set number

and to set line numbering off

set nonumber

Turn Off Argument Highlighting

You can provide search arguments to vi, and it will take directly to the first appearance of the word in the file or on a particular line number.

vi +55 index.php

vi +/copyright index.php

The word searched is highlighted in color, if not desirable, can be turned off by adding:

set nohlsearch

Toggle Line Numbering ON/OFF

Navigate to particular line number by typing:

<line-number> G

or

<line-number> gg

Like 3G or 3gg to navigate to line number 3. If we don't wish to provide 'G' and with to reach directly to a line by providing line number, followed by 'Enter' key, then type the following line in ~/.vimrc file.

nmap <CR> G

We provide a command

vi +235 index.php

to reach that 235 line number. Press Shift g to reach the end of file and gg to the first line of the file.


No comments:

Post a Comment