mrexojo
11/9/2017 - 11:54 AM

Some tips and resources about vi editor

Some tips and resources about vi editor

#Help for command on vi - ej:delete
[Esc]:h x

#Edit multiple files. From shell:
vi file1 file2
  #Write to file1 and NEXT to file2
  [Esc]:wn
  #Next to follow file
  [Esc]:n
  #List files online
  [Esc]:ls

#Edit at line 6 file1
vi file1 +6
 
#Edit at first word "privdata" on file1
vi +/privdata file1

#Where we are
[Esc]:pwd

#Insert Mode
i
#Command Mode
[Esc]
#Insert mode next position
a

#Save file
[Esc]:w

#Quit/Exit
[Esc]q

#Save and exit
[Esc]:x
[Esc]:wq
# and force with !
[Esc]:x!
[Esc]:wq!

#Save file
[Esc]:w
# View Number lines
[Esc]:set nu
[Esc]:set number

# Un-number lines
[Esc]:set nu!
[Esc]:set nonu

#Go to the first line
[Esc]:0
#Go to the last line
[Esc]:$
#Go to the line 4
[Esc]:4
#Go to the next word
[Esc]:w
#Go to the init current line
[Esc]0

#Undo
[Esc]u

#Copy content from file
[Esc]:r /etc/passwd

#Copy output of commands
[Esc]:r!hostname
[Esc]:r!timedatectl
[Esc]:r!ip a|awk '/global/{print $2}'
[Esc]:r!df |grep -w \/ |awk '{print $4}'

#Execute command on console and return to vi
[Esc]![command]
[Esc]!:ls

#Load shell and returns to vi on fly
[Esc]:shell
....
exit

#Backup/copy of current vi on other file
[Esc]:w! file.bck

#Backup/copy of current line in other file
[Esc]:4w! L4
#or lines range
[Esc]:1,3w! L1-3

#Search string "privdata" into file1 and search the followings
[Esc]/privdata
n
n
#Or return previous match
N
N

# display changes performed since last save
:w !diff % –
  
#Replace on all file "hello" to "bye"
[Esc]:% s/hello/bye/g

[Esc]:1,$s/hello/bye/g

#1,$ = %  = all lines

#Replace mac address, symbols...etc
:%s/00\:16\:3e\:57/00\:16\:3e\:58/g

#Multiline Comment from 66 to 70:
:66,70s/^/#

#and for uncomment:
:66,70s/^#/

#Add BUF=14000 at end of each line
:%s/$/ BUF=14000/g

#Delete from current line to end file
[Esc]dG
[Esc]:.,$d

#Delete word at initial
[Esc]dw

#Delete blank lines
[Esc]:g/^$/d

#Copy line
[Esc]yy
#Paste line
[Esc]p

#Copy current line to next line
[Esc]:t.
#Copy current line to end of file
[Esc]:t$
#or current line on concrete line
[Esc]:t5

#Copy line n after line m
[Esc]:n copy m
[Esc]:2copy5

#Copy line 2 to current line
:2t.

#Copy line 1 to 5 after m line
[Esc]:1,5 copy m

#Move line 2 to current line
[Esc]:2m.

#Move lines 1 to 5 from current line
[Esc]1,5m.

#Move previous line to current
[Esc]:j
#Move previous 4 lines to current
[Esc]:j4

#Execute current [language] script
[Esc]!:python %
[Esc]!:ruby %

#History browse command mode
[Esc]:[up][down]

#Keyboard MAP - English
http://www.nathael.org/Data/vi-vim-cheat-sheet.svg
#Keyboard MAP - Spanish
https://mrexojo.files.wordpress.com/2013/12/vi.gif

#PDF & Gifs tutorial Vi from viemu.com
http://www.glump.net/files/2012/08/vi-vim-cheat-sheet-and-tutorial.pdf
http://www.viemu.com/vi-vim-tutorial-gif.zip