LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 08-15-2007, 09:28 AM   #1
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 206Reputation: 206Reputation: 206
Vi command: howto insert '\n' at column 80?


Hello,

I have a very long file, and I would like to insert an '\n' character before every word that contains a character on column 80. The word containing the character at column 80 would then be placed after the '\n' (on the next line.)

I am sure there must be a vi command for this, but I am too inexperienced with it to know where to begin. Would appreciate help from an experienced UNIX VIteran. (hoho.)

Thanks -
Jameson

Last edited by jhwilliams; 08-15-2007 at 09:30 AM.
 
Old 08-15-2007, 09:58 AM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893

Rep: Reputation: 68
I failed to find any easier way than to insert the following function in .vimrc:

Code:
function! WordWrap(n)
	let v:errmsg=""
	while v:errmsg==""
		exe "normal /.\\{".a:n."}/e? a"
	endwhile
endfunction
command! -nargs=1 WordWrap call WordWrap(<f-args>)
So ':WordWrap 80 ' will do after that.
 
Old 08-15-2007, 12:19 PM   #3
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Original Poster
Rep: Reputation: 206Reputation: 206Reputation: 206
Thanks!

Unfortunately, this is beyond my VI knowledge and I'm having a problem:

On the line:
Code:
exe "normal /.\\{".a:n."}/e? a(stuff)"
I am seeing a box with "001B" written in it (where I have "stuff" written) after /e? a

This is an ASCII ESC character, '\e', right?

When I change the segment in question to a(backslash)e , source my rc file and go to use vi, the function enters an infinite loop.

Perhaps I also do not understand what the function's argument is supposed to be.

- Jameson

Last edited by jhwilliams; 08-15-2007 at 12:21 PM.
 
Old 08-15-2007, 01:03 PM   #4
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893

Rep: Reputation: 68
The part up to "/e" was rendered correctly.. It is direct slash, not backslash.
After it you should press (in Vim):
Code:
<Ctrl-V> <Enter> <?> <Space> <Ctrl-V> <Enter> <a> <Ctrl-V> 
<Enter> <Ctrl-V> <Esc> <DoubleQuote ">
Keypresses are space-separated and <>-enclosed.

Argument is column number. What it does is cyclically simulates keypresses.
/.\{80}/e<Enter>
-- this looks for a line 80 characters long and puts cursor on its end.
? <Enter>
-- this goes to last space before that position.
a<Enter><Esc>
--this adds newline character after this space.

You should see:
Code:
exe "normal /.\\{".a:n."}/e^M? ^Ma^M^["
 
Old 08-15-2007, 01:40 PM   #5
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Original Poster
Rep: Reputation: 206Reputation: 206Reputation: 206
Thanks! It is working now. However, I get this error everytime I use it when it finishes:

Code:
Error detected while processing function WW:
line    3:
E486: Pattern not found: .\{80}
My .vimrc looks like this in VIM:
Code:
function! WW(n)
   let v:errmsg=""
   while v:errmsg==""
     exe "normal /.\\{".a:n."}/e^M? ^Ma^M^["
  endwhile
 endfunction
 command! -nargs=1 WW call WW(<f-args>)
Any idea what this about - can I get rid of it somehow, to polish the thing off?
 
Old 08-15-2007, 02:07 PM   #6
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893

Rep: Reputation: 68
I'm not sure. Really this error is the condition on which endless loop exits. Try writing
Code:
 command! -nargs=1 WW silent! call WW(<f-args>)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sql command to copy column from a table tooparam Programming 1 06-19-2007 05:21 AM
Howto insert a pdf document into a mysql table jadewarrior Linux - Server 4 02-23-2007 05:18 AM
howto insert a string kscott121 Linux - Software 6 12-26-2006 03:50 PM
Mysql - Howto insert 'admin' account into table ??? b:z Linux - General 6 03-29-2005 03:44 AM
insert module into non-running kernel... howto? euph0rix Linux - Newbie 2 01-25-2005 06:29 PM


All times are GMT -5. The time now is 03:47 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration