Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts

Monday, August 24, 2015

Add newline in vim

I am often analyzing SQL queries from Informatica session log and they are wrapped around and difficult to read. That's when I needed to look for a way to insert a newline after every comma.
Great documentation and examples here.

http://vim.wikia.com/wiki/Add_a_newline_after_given_patterns


select wall_of_text,block_of_text,cube_of_text from textbook;
Add a newline after comma: s/,/\r&/g
select wall_of_text
,block_of_text
,cube_of_text from textbook

 Though brackets let you specify multiple values that you can replace (the vim wiki has an example), I was not able to figure out how to get a newline after every comma and "from". Drop in a comment if you know how to.

Wednesday, May 11, 2011

vi or sed - Delete till end of line from pattern

Here's the regex that will match everything from the pattern until a newline is found.

Say you want to remove everything after "here" from this sample file.

code here    where?
code here   right here!
code here         look every where.
Used regex
s/here.*//
Explanation:
.*  
dot matches any character including spaces, numbers, alphabets but not new line.
asterisk matches any number of occurrences of a character

s/here.*/meow/ will result in

code meow
code meow
code meow

Friday, January 21, 2011

vi Search backwards

For some reason I couldn't ever remember how to do a backward search in vi. So I would use forward search command, "/pattern" and then do "N" which is fine except the clumsy usage of uppercase.

Backward search can be done with "?pattern" and then do "n" to iterate over all the occurrences. Committed to memory now.

Thursday, August 12, 2010

Paste in vi without annoying auto indent

vi annoys the hell out of me with its auto-indenting during clipboard paste. So thanks to this post I've a solution now.

In command mode "set paste" and after pasting the text, turn it back off by "set nopaste".

what a breath of fresh air ;)


Thursday, July 29, 2010

REPLACE between marks

While using vi, add marks to work with a chunk of lines.

To replace beginning of all lines, between the (inclusive) marks a and b, with ZZZ; we could write

:'a,'bs/^/ZZZ/

Where a has lower line number than b.

Wednesday, April 28, 2010

Function in shell script

Declare the function before calling it.# Function to get password
functionGetpwd()
{
echo "Please provide $USR database password"
read PWORD
echo "Connecting to Database"
}

Call the function
 functionGetpwd

Tuesday, February 2, 2010

vi multiple lines ; repeat last commands

To join multiple lines in vi, use 5,9j! in cmd mode.
We know that J (note uppercase) on a line joins it with the next line. "!" preserves whitespace between the lines but it didn't in ksh shell.

To apply last used substitute command through out the file:
e.g. %s/this/that/g

& to repeat the last used substitution for the first occurrence on the current line
&g to change all on the current line
%&g for entire file

Use . to repeat the last insert command. It inserts the last few inserted words/lines after the cursor.


Wednesday, November 25, 2009

changing case within vi

1)
If you have to change one or two letters, use ~ over the letters while in command mode. vi toggles the cases with ~

2) Between a range, say marks a and b

Change to lower case
'b,'a,s/[aA-zZ]/\L&/g

Change to upper case
'b,'a,s/[aA-zZ]/\U&/g

3) Change to upper case thru out the file
%s/[aA-zZ]/\U&/g

Running shell commands from within vi

Without leaving vi, we can run shell commands from within vi.

e.g. we need to insert contents of a file within our current file opened in vi.

1) Insert will begin immediately after the cursor. So we place cursor where we need the insert.
2) enter vi command mode
3) :r! cat filename

:r! can be used to insert output of a unix command also.
e.g. :r! ls
the above command inserts list of the files in the current directory (pwd)

:sh can be used to enter shell from within vi.
to return to vi, use exit.

Thursday, July 16, 2009

Error SP2-0734 - unknown command beginning

while running a query that ran fine on toad but failed on the server/sqlplus due to above error.

The solution is simple, just remove all blank lines from your script.

To delete blank lines in vi:
%g/^$/d
(i.e. global change, nothing between beginning of line to end of line, delete)

Friday, May 22, 2009

DELETE

delete till end of file
dG

some other delete commands

added:
:g/^$/d
delete all (global) lines that have nothing from start (^) to end ($) i.e. blank or empty lines

:.,$d
delete lines from current line (.) to end of file ($)
dG will do the same. It's not an editor command so should be done directly on the line.

between marks
:'p,'qd
delete lines starting from mark p to mark q, including both p and q

delete current line and lines below
4dd
delete current line and 3 below it

delete current line and lines above it
3k4dd
3k moves the cursor 3 lines up and 4 dd deletes the required 4 lines

Thursday, May 14, 2009

Wednesday, May 6, 2009

Caret M or ^M creeps into files imported to unix from windows/dos. It is a bad line break or carriage return. It can be seen in vim.

Caret M is a single character that can be created by pressing control+v and control+m. Writing Shift+6M would not work.

To remove caret M from the files give "%s/^M//g" on vi in escape mode.

Friday, May 1, 2009

while editing a file in vim, to do any of the following, get into vim command mode.

1. invoke shell using :sh. Return to vim using exit in shell.
2. see output of a vim command using :!cmd
3. insert the output of a vim command using :!rcmd
this last command is highly useful if we have to insert data from a different file into the current. it saves copy/paste efforts.

Wednesday, April 29, 2009

few vi commands:

Deleting from cursor to end of line
D


Selecting text for further copy/paste/delete etc
  • enter visual mode per character using v or per line using V;
  • use motion commands like h, j, k, l to select text;
  • use the selected text to yank/delete etc;

Move to the beginning next "word delimited by space"
W (w is for any next word)


Move to the beginning of previous "word delimited by space"
B (b is for any previous word)