Showing posts with label vi. Show all posts
Showing posts with label vi. Show all posts

Monday, September 19, 2011

Numbered buffers in Vi

Been so long since the last post on Vi! Now time for another post! especially after my colleagues had trouble with named and numbered buffers!

If you haven't already taken a look at the earlier posts on Vi, here's what you need!

Named buffers are already discussed. It's now time for the numbered ones. I haven't  elucidated on the numbered ones.. so here goes!

Unlike named buffers, in numbered ones you can't decide, into which of the numbered (1-9) buffers your text goes. Because it's done automatically. When you delete or yank(copy) text without mentioning the named buffers, it automatically gets pushed into the 1st buffer referred by 1. So the recent buffer is always 1.

When another operation of yanking or deleting follows at a later time, whatever was already in 1, goes to 2 and the most recent one gets to 1. More like a stack!

Good! Now what if whatever was in 1 has gone down to 9... after 9 successive cuts/copies?! The 10th ie the recent most copy will move to 1 as said and whatever was in 9 gets lost, since 8 has now moved over to 9! Now that end is a FIFO!

Why do I need something over which I don't have control of! "What use?" you shall ask?! Now it's very much useful because if you happen to need your 2nd to recent cut/copied text... (previous to the recent cut)

Simple!

"2p

Done! third?!

"3p

and down to 9! Happy coding! or rather editing (who says vi is for coding only ;) )! 

Sunday, April 11, 2010

Some handy vi args!

Say you compiled a program called test.cpp and the compiler spitted the error
"test.cpp:97: error: ‘itoa’ was not declared in this scope
at you!

Mostly, you do one of the following!
Open the file and 
a) Scroll through it!  (See line numbers with :set number  or   :set nu )
b) Jump to line 97 which you can achieve by 
i) 97G   (vi command itself)
ii) :97 enter  (ex editor command inside vi)

You can achieve this easily with the command line args itself (those that are supplied when you open vi. The file name itself is one such arg)!

vi +97 test.cpp will open file with cursor at the 97th line!

Now what if you are like me and are bad with numbers? Can't remember 97?

vi +/itoa test.cpp will perform a pattern matching and will open the file at the first match. You can then use n or N to skim through the matches until you land on your line! 


Bonus tip! :) 

Ever wished vi has alt+tab (on windows/linux) or cmd+tab (on mac) to switch between two files? It has it too! :D

Just open two or more files with all the names as args to the command!

vi file1 file2 file3 file4 file5

You will be editing file1 initially!
Check which file you are editing with ex command :args or :ar 
Vi will print
[file1] file2 file3 file4 file5 
showing the current file in brackets and the entire list that you supplied!

Move around the list with :n and :N. To jump to the first file on list use :rewind or :rew and :last for the last file!

Ooops! Now to issue of concern alt+tab! 
Once you have at least once switched between the 2 files that you need to edit alternatively, use :e # to switch between files!
or simply ^^ (Ctrl + caret)
What if you have find that you need to edit file1 and file7 alternatively but forgot to open file7??? Wait! Don't quit vi!
When you are editing file1 just call in file7 with :e file7 (e for edit) and there you have it! Now use ^^!  

As mentioned in an earlier post, you can use the clipboard to move text around the files!

Though the y (yank) and p(put/paste) commands are handy you can't easily do that for multiple lines!
use :line1,line2ya  c for copying/yanking multiple lines (where line1 = starting line number; line2 = ending line number; cn = a-z one letter clipboard name)

Switch to your file you want now without exiting vi and

then :pu c will put that clipboard c's content into the new file!
(A word of caution! The switching between files can happen only if the file you are editing is saved)

Now that's why vi is a Very Interactive editor!

Saturday, April 3, 2010

vi has a clipboard too!


The very interactive(vi) editor has all that your GUI based editor has! Actually, it has more! Adding to all the basics, here are some key-taps that can help u tap the power of VI.

When you hit 'd' based delete commands like 'dd', 'nd' ... or 'y' based yank commands all that's is deleted or yanked is stored in a buffer. (Ssh! Deleting with d is not delete, it's actually 'cut'. Try 'p'-ing to paste after 'd' :) )

So where does it actually get stored? It's not one but 2 buffers!

Deleted lines are stored in a numbered buffer, named from 1 to 9. (stack like. recent entry is 1, oldest being 9)
You can yank/delete into alphabetically named buffers too (a-z).

"(double quote) is used to access this a-z buffer.

" is the simple syntax

Eg: "c5dd will delete/cut 5 lines in to buffer named 'c'

(simply 5dd will not cut into the buffer)

Interesting part is "C3dd will append your now-deleted 3 lines to your previously present contents of buffer 'c'. (Note the upper case.) You can't do this even in MS word!!!

Paste the buffer using "cp     ( "c for accessing buffer and p for paste/put )


Same works for yanking too!

To access the numbered, deletion buffer, use "np where n is number 0-9 similar to a-z.

Ain't that something!!!!

Friday, March 19, 2010

Very Interactive(VI) editor commands

This has been a request from couple of people, putting it down for them and for everyone who is looking for frequently used vi editor commands.

Edit commands:
-------------------
x - delete one character at a time
nx - delete n characters at a time, replace n with number you want
dd - delete/cut a line
ndd - delete/cut x lines (replace n with number you want Eg: 10dd would delete 10 lines)
yy - yank a line(copy to clipboard)
nyy - yank n lines(replace n with number you want Eg: 10yy would yank 10 lines)
p - paste from the clipboard(deleted lines are like copy too, they will be in clipboard, so p after dd or yy will paste whats there in the clipboard)

*Remember: All the above commands are relative to the current cursor position. i.e. If you are at line 20 and performing dd, line at that position will be deleted/cut. Similarly p operation will paste after 20th line.

More Edit commands:
--------------------------
dw - delete/cut a word
ndw - delete/cut n words (replace n with number you want Eg: 3dw will delete 2 words)
cw - change or replace word (move the cursor to the word you want to change and give cw, then type in the replacement word)
ncw - change n words at a time(Eg: 2cw will help you replace two words)
o - to insert a line below the line where the cursor is positioned
shift+o - to insert a line above the line where the cursor is positioned

Cursor positioning or movement:
---------------------------------------
<- or h - to move left -> or l - to move right
up arrow or k - to move up
down arrow or j - to move down
0(zero) - to move to the first character of the current line
$ - to move to last character of the line
w - to move cursor to the beginning of the next word
b - to move cursor to the first character of the preceding word
:0 - to move to the first line of the file(what you see here is zero)
:n - to move to the nth line of the file(replace n with number of your choice)
:$ - to move the cursor to the last line of the file

* CR- carriage return/Enter key in the keyboard

Scroll commands:
---------------------
^d (ctrl+d) - scroll half screen down
^u (ctrl+u) - scroll half screen up
^f (ctrl+f) - scroll full screen down
^b (ctrl+b) - scroll full screen up

(do it for yourself to see the difference, choose a bigger file to see the difference between these scrolls for yourself)

Search commands:
-----------------------
/string - to search for a string (replace string with your search string)
(n to find the next match, shift+n to find the previous match.

Search and Replace:
------------------------
:s/old_string/new_string - replaces the first occurrence of "old_string" with "new_string" in the current line (Eg:- :s/import/export will replace first occurrence of the word import with export in the current line)
:s/old_string/new_string/g - will replace all occurrences of "old_string" with "new_string" in the current line
:%s/old_string/new_string/g - will replace all occurrences of old_string with new_string in the file.

(**Remember everything is case sensitive here, to make your search & replace case insensitive see below)
:s/old_string/new_string/i
:s/old_string/new_string/gi
:%s/old_string/new_string/gi

see the 'i' at the end makes it case in-sensitive.

To perform search and replace between selected lines, you can use the commands below.
:l1,l2s/old_string/new_string/g - replace l1 and l2 with line numbers you want. (Eg:- :1,10s/import/export/g you know what this means... isn't it?)

Undo:
--------
u - to undo previous commands

Switching modes:
----------------------
i - insert mode
a - append mode
esc - to get out of any mode you already chosen

I will stop with these basic commands you would need to start.

In case if people need more useful commands at advance level, I will put a separate post later.

Happy VI'ing :))