Sunday, December 25, 2016

Essentials of Vi editor - part 3

In my previous two posts (1 and 2 below) we looked at vi basic and editing.

1. Essentials of Vi editor - part 1
2. Essentials of Vi editor - part 2


In this post let's look at few advanced commands and techniques we can use in vi editor

Indentation and work wrapping
-----------------------------------------------------

>> indent the current line. e.g 3>> indents 3 lines, >} indents a paragraph
<< outdents the current line
:se ai // Enables auto indent
:se noai // Disables auto indentation
:se wm=8 // Sets the wrap margin to 8. As soon as you enter 8 columns it wraps the cursor
:se wm=0 // Auto wrap set off


Filtering
-----------------------------------------------------

!! - applies the filter to the current line
n!! - applies the filter to n number of lines from current line
!} - filters the next paragraph
!{ - filter the previous paragraph
!% - applies the filter from current location to next parranthesis, brace or bracket


These filters can be can be applied to shell commands like tr (transformation), fmt (formatting), grep (search), sed (advanced editing) or awk (a complete programming language). This would mean like sending the filtered text through these commands and getting the output of it and searching or placing back in file as applicable.

e.g In command mode uf you type
!!tr a-z A-z // And enter. Turns the current line into uppercase. Note however your lower command shows :.!tr a-z A-Z. It converts into a format that vi understands it.
5

Advanced examples with : command
-----------------------------------------------------

: 1,. d        // Delete all the lines from the first line (indicated by 1) to current line (indicated by .)
: .,$ s/test/text/g   // From current line (indicated by .) to end of line (indicated by $) search and replace all 'test' occurrences to 'text'
: /first/,/last/ ! tr a-z A-Z // Find first line that matches 'first' regexp to the first match following 'last' regex and filter it (indicated by !) using the unix command tr from a-z to A-Z (means convert to upper case)

ma // marks a line by character 'a'
mb // marks a line by character 'b'
!a // jump to the line marked by a
: 'a,'b d // Delete all the lines marked by a through b 


------- Update - Youtube tutorial of the above (04/01/2023) ---------
Youtube Video

No comments:

Post a Comment