Important Linux Vi/Vim Editors commands for Programmers – 2

Here is a collection of only Vi/Vim Editor related commands. It is only a collection of important commands that you can afford to remember and do all important and usual task a programmer need to do while writing scripts. If you got to work on vi/vim editor for long enough time then you surely can remember these commands and you will need for more advanced command that you can find one by one anywhere and can remember as and when they come.

$vi filename
Creates a filename and open the file for editing. Press i for insert mode ( allow editing).

:wp
For save and Quit.
While in vi editor, press : for coming out of insert (edit) mode and key wq. w is for save and q is for quit.

:q!
for force quit. Vim will force you to save a file before quitting. So, to quit without saving a file, use q! command.

h
cursor left by one character

j
cursor down by one line

k
cursor up by one line

l
cursor right by one character

These command wherever you are not seeing :, meant to enter in non edit mode. Otherwise they will just be typed in within content. Obvious!

u
undo the last change. U to undo all.

Ctrl+r
redo

x
delete single character under cursor.

dd
Delete a line. 4dd to delete four lines and similarly 3dd, etc. No need to select the whole line for using this command.

:.= (colon dot equal to sign without space)
returns line number of current line. It outputs at bottom.

:= (colon and equal to sign) - total number of lines in file

:w newFileName
Write the content to a new file. After this you may like to close the file without applying change the the current file or continue editing the original file and your old file data will be saved in newFileName.

:12,35 aSmallFile
Write lines 12 to 35 of the opened file to a new file (aSmallFile)

% (percent sign)
Place the cursor on {} / () and % will take you to matching bracket. Do it without ":".

Format the file for correct Indentation:
shift + v
shift + g
=

Use all three command one-by-one and see the effect. It will fix indentation problem. If it missed at one/two place then fix yourself and do this again to fix 100%. If you fix indentation problem yourself then you may miss 1-2 spaces.

For formatting few lines:
shift + v
j // for each line
=

/searchTerm
To search open file. Enter the / character in non insert mode and then your search term. It will highlight the found text. Use n and N to progress next and previous matched text. Continue pressing n and you will be allowed to continue after cursor goes to end of file.

:%s/findTerm/replaceTerm/
Find and Replace

:%s/findTerm/ReplaceTerm/g
Find and replace all (like match_all).

:%s/findTerm/replaceTerm/gc
c is for confirm.

ctrl-f
scroll forward one screen.

ctrl-b ↑ scroll back one screen.

:set number or :set nu To display line number in editor at left. Use :set number! or :set nu! to disable it. Add the line in .vimrc to have line number every time you open the editor.

v or V, y or d, p or P It is what is called Cut, Copy and Paste. v is for selected text, V is for whole line.
y is for copy and d is for cut (delete), p is for paste After cursor and P is for paste before.

Opening a DOS file
I got a file like this when I opened it. You see ^M at the start and end of the line. I have found these command work:
:1,$ s/^M//
Or,
:%s/<ctrl-v><enter>$//
These command will trim the control character. To see these control character (non printable) through cat command, type $cat -v fileName
To type ^M character after colon (:) on vim editor, press control+v+m.

Did you tried Marks?
It is very handy when you work on Vim editor.
'. = Jumps to the position where last change has occured
'' = Jumps back to line in current buffer where jumped from

There are more to know. Please Google it. Also check on Vim :marks.

:mx
x = marks
'x = to jump to mark = x.

My settings that can affect vi/vim:

File: .vimrc
set syntax=on
set tabstop=4
set shiftwidth=4
set expandtab

#color autumn

File:virc
set syntax=on

set tabstop=4
set shiftwidth=4
set expandtab

File: .bash_profile
alias vi="vim"

Related post:
Important Linux Commands for Developers

Comments are open for an year period. Please, write here on Facebook page.