Search This Blog

Sunday, April 26, 2015

VI editor Tips and Tricks

vi editor in Linux is the best tool to create / edit a file. It has got more functions then any other Linux Editor (if you don't have access to GUI).
I am trying to keep this post as simple as I can. If you have any doubt or queries please ask.
In this post we will cover some exciting tricks and methods which I bet will be quite effective. 
I have categorized them in the following topics.
Now lets check them one by one.

---------------------------------------------------------------------------------------------------------------------
changing text
ccchange a line
Cchange from current cursor position to the end-of-line
cwchange from current cursor position to the end of the word
Jjoins the next line to the current line (inserts a space between the two)
gJjoins the next line to the current line (without a space)
:12,24jjoin lines 12 thru 24, leaving a space between each line.
~switch lower and upper cases
rreplace the current character with the next character typed
4rxreplace next four characters with "x"
Roverwrite until <esc>
ssubstitute the current character and go into insert mode. Same as "rxi <ESC>"
Ssubstitute the current line, erase it, and go into insert mode, starting at the indentation of the line. Same as "cc"
>>shift current line forward
3>>shift three lines forward
>%shift all lines until a matching brace or parenthesis
>}shift to the end of the paragraph
>'mshift to the marked line
<<shift current line backward
vEUchange from here to end of word to uppercase
vEuchange from here to end of word to lowercase

--------------------------------------------------------------------------------------------------------------------
copying and pasting (We call yanking for Copying anything in Linux)
ylyank (copy) current character
ynyank n characters
ywyank the current word
yyyank the current line
Yyank a line, same as "yy"
nyyyank n lines of text
y$yank to end of line
y)yank to the end of the sentence
y'xyank from current line to the mark x (use mx to mark as x)
y/wordyank from current position to "word"
"ay'myank from here to the mark. Place this into the named buffer "a". This will also allow one to switch files using the ":e fn" or ":n fn" command and then place the buffer back.
"ayyyank current line into buffer a.
"a3Yyank three lines, place into buffer "a"
"A3Yappend 3 lines, place into "a". This allows one to build up a buffer.
"appaste the contents of "a" back, below current line.
"zyy
@z
"zyy places the highlighted text into buffer z@z will run the contents of buffer z. This allows a search pattern to be written in the document and then reused.
ppaste to the right of the cursor
Ppaste to the left of the cursor
---------------------------------------------------------------------------------------------------------------------
deleting
xdelete the character under the cursor
dddelete the current line
d^delete from current cursor position to the beginning of the line
d$delete from current cursor position to the end of the line
dwdelete from current cursor position to the end of the word
d3wdelete three words
3dddelete three lines
d/worddelete until you find word
dfxdelete from here until the character "x"
Ddelete everything to the end of the current line (same as d$)
3dddelete everything to the end of the current line and the following 2 lines
uundo deletion
Uundo the line you just changed

---------------------------------------------------------------------------------------------------------------------
inserting
iinsert at the current character
Iinsert at the beginning of the current line
20i- <ESC>insert 20 dashes (-)
aappend to the right of the current character
Aappend at the end of the current line
oinsert a new line immediately following the current line
Oinsert a new line immediately before the current line

---------------------------------------------------------------------------------------------------------------------
moving
hmove the cursor one character to the left
jmove the cursor one character down
kmove the cursor one character up
lmove the cursor one character to the right
0move cursor to the beginning of the current line
$move cursor to the end of the current line
wmove cursor forward a word
3wmove cursor forward three words
Wmove cursor forward, ignoring punctuation
bmove cursor back a word
Bmove cursor back a word, ignoring punctuation
emove cursor to the end of the word
Emove cursor to the end of the word, ignoring punctuation
Gmove cursor to the last line of the file
nGmove cursor to the beginning of line n
1Gmove to the first line of a file
n|moves the cursor to the beginning of column n
ffind a character in the line, forward
fafind the character "a" in the line, forward
2fafind the second occurrence of "a" in the line, forward
Ffind a character in the line, backward
Fzfind the character "z" in the line, backward
tfind up to a character in the line, forward
)jump to the next sentence
}jump to the next paragraph
]jump to the next section
%find matching brace or parenthesis
^bscroll backwards one page. A count scrolls that many pages
^fscroll forwards one page. A count scrolls that many pages
^uscroll up half a screen
^dscroll down half a screen
z <enter>put current line at top of screen
z.put current line at middle of screen
z-put current line at bottom of screen
mstart a mark, the next character is the name, any character from a-z
mamark this spot with the character "a"
'areturn to the line marked "a"
`areturn to the character marked "a"
''return to previous line (auto-mark)
``return to previous character (auto-mark)
---------------------------------------------------------------------------------------------------------------------
search and replace
/wordsearch forward for word
?wordsearch backward for word
?word?z.
search forward for word, put line at the middle of screen
njump forward to next occurance of word
Njump backward to next occurance of word
*search forward for word under cursor (vim)
#search backward for word under cursor (vim)
g*search forward for word under cursor (partial match) (vim)
g#search backward for word under cursor (partial match) (vim)
:nohstop highlighting the word that was searched for
:s/old/new/replaces first occurence of old with new on current line
:s/old/new/gglobally replaces old with new on current line
:%s/old/new/gglobally replaces old with new on all lines
:s/old/new/gcconfirms replacements
:%s/^V^M//gglobally remove DOS carriage returns
:%s/,/^V^M/gglobally replace commas with newlines (yes, I know this looks like it would insert DOS carriage returns, but it only inserts the newlines on Unix)
:%s/\s*$//strip blanks from end of lines
:%s/^.\{30}//remove the first 30 characters from each line
:%s/[ ^I]*$/!dremove trailing whitespace (where ^I is produced by pressing the tab key)
:%s/.*/\L&/convert the entire file to lowercase.
:s/.*/\u&/convert the first character of line to uppercase.
:%s/\<./\u&/gconvert the first character of each word to uppercase
\< matches the start of a word;
. therefore matches the first character of a word;
\u tells Vi(m) to uppercase the next character in the substitution string;
& means substitute whatever was matched on the lhs;
:s/book{./\L&/convert the first character after the { to lowercase.
:'x,.s/\([a-z]\)=/\1 =/replace any lowercase character in the alphabet followed by an equal sign with the same character followed by a space and an equal sign, e.g., book= >> book =
::%s/\(That\) or \(this\)/\2 or \1/change "that or this" to "this or that"
::%s/\(^\\foilhead{\(.*\)}\)/
%\1^M\\section{\2}/
comment out each line beginning with '\foilhead' and replace with a line beginning with '\section'; note the nested backreferences for keeping the rest of the line the same
::%s/\(.*\)/<a href="\1">\1<\/a>/add HTML tags for references to files
:s/ \(\d\{1,2}\)\.\(\d\{1,2}\)\(.*\)/<a href="\1-\2.sh">\1.\2\3<\/a>change a line of the form
       1.1 Accessing Substring
to
       <a href="1-1.sh">1.1 Accessing Substrings</a>
:'x,.s:^\([^ ]\+\) \(.*\):
<\1>\1 \2<\/\1><br>:
change a line of the form
       KBD used for text to be typed by the user
to
       <KBD>KBD used for text to be typed by the user</KBD><br>
Colons are used here instead of the usual slashes for separating the 'find' and 'replace' parts. The part ^\([^ ]\+\) finds all of the characters up to the first space and saves them for use in the 'replace' part as \1.
:'x,.s/^/#/insert a pound sign (#) at the beginning of the line from the line marked with mx to the current line
:%s/word1\nword2//remove word1 from the end of a line and word2 from the beginning of the next line; \n represents the newline between the two
:&repeat previous substitution
:<up arrow>recall : history
:g/string/ddeletes every line that contains string
:g/\.c/+|s/^/\.cc/look for all ".c" commands, jump to next line, substitute the beginning of line with ".cc".
:g/^\..*/|s//\U&/convert all characters at beginning of line that start with a "." to upper case.
:v/string/ddeletes every line that does not contain string
:g/.*/m0This will reverse the order of the lines in the current file. m0 is the ex command to move the line to line 0.
:v/./d or :g/^$/dRemove all blank lines.
:g/^\s*$/dRemoves all lines that only have whitespace.
:v/./.,/./-1joinReplaces multiple blank lines with just one blank line.

---------------------------------------------------------------------------------------------------------------------
options
:se aiautoindent (unset with :se noai)
:se ff=unixset fileformat to unix
:se icignore case when searching.
:se listdisplay tabs and carriage returns
:se nudisplay line numbers in the file. They are not actually in the file. (unset with :se nonu)
:se smshow matching brace or parenthesis while inserting
:se smddisplay the mode
:se sw=2set shifting to 2 spaces
:se tabstop=4set tabs to 4 spaces
^^Dturn off autoindent for current line, resume same place for the next line (up carrot and control-D)
0^Dreset the autoindent, start at the left margin

---------------------------------------------------------------------------------------------------------------------
external commands
!cmdexecute an external program
!!cmdexecute an external program, replacing the output with the results
!$send from here to the end-of-line
!Lsend from here to the last line of screen
!23Gsend from here to line 23
!/wordsend from here until you find "word"
!)send from here until the next sentence
!}send from here until the next paragraph
!!dateadds the date
!!cut -c41-cut the first 40 characters from a file
3!!sortsend three lines to sort, and return the output
!Grevsend from here to the end of file to the "rev" command; the results will reverse the characters in each line
!}sortsends from the current line until the first blank line to sort
:'x,.!sortwill sort from the line marked with mx to the current line
:r !llread the output of an "ll" command, and put it after the current line.
:'t,'b !spellcheck spelling from mark t to mark b
:'t,. !awk '{print $3 " " $2 " " $1}'reverse the order of three columns
:%! sed Gdouble space the entire file.
:1,5! sed Gdouble space the lines from 1-5
:'x,.!sed '/^$/d'remove the blank lines from mark x to the current line

---------------------------------------------------------------------------------------------------------------------

miscellaneous

^ggive file name, status, current line number and relative position
^lrefresh the screen (sometimes `^P' or `^R')
^vvisual mode. in Windows, Ctrl-v is mapped to paste text. Use Ctrl-q instead (vim)
.repeat latest command
&repeat latest `ex' substitute command, e.g. :s/wrong/good/
:ab lg longercreate an abbreviation. Whenever lg is typed, it will be replaced with longer.
vi +/string filenamestart vi, jumping to the first occurrence of string
vi -r filenamerecovers an open file after a crash
vi `cat somefile`open a list of files that are in the file somefile
:r filenameinsert file filename, placing its contents after current line
:1,4w filenamewrite lines 1 through 4 to filename
:e filenameedit filename
:f filenamechange current file name to filename
:nedit next file
:shcall up the shell, run commands until a ^d (CONTROL-d), then return to editing
:viVI MODE. Used if "Q" is pressed, or called up ex, and now wish to use the visual mode
In Insert mode, press the Ctrl-p or Ctrl-n key to complete part of a word that has been typed. This is useful for entering function names.
:<up arrow>recall : history
----------------------------------------------------------------------------------------------------------------------
References:
  1. vi Fast Reference Guide — the motherload of vi commands
  2. Vim documentation: starting
  3. Vi IMproved — VIM
  4. Best of VIM Tips
----------------------------------------------------------------------------------------------------------------------


No comments:

Post a Comment

Thank you for your Feedback.