Visit Our UNIX and Linux User Community


vim Script: Switching between modes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting vim Script: Switching between modes
# 1  
Old 06-08-2011
vim Script: Switching between modes

Hi All,

I've been using vi for about 10 years, but this is my first attempt at creating a vi script. I am reformatting some files to create CSVs for import into a database. In the script, I have a bunch of substitute lines that work fine, but I'm trying to figure out how to get the script into insert mode and then back to command mode so I can run commands such as:

d1G
dG
1G

I've tried typing <ESC> into the script, but vi doesn't seem to recognize it. What can I use to switch modes?

Thanks,

Dkr.
# 2  
Old 06-08-2011
Did you try pressing <CTRL>+<V> key before pressing the <ESC> key ?

---------- Post updated at 11:35 PM ---------- Previous update was at 11:23 PM ----------

You can use macro in vi

macro in command mode
:map <key> <command>

in text mode
:map! <key> <command>

use tab to expand a macro.

So you can try to define macro that work in text mode then expand it ?

If you put an example of how your input file look like and the output you expect maybe we can help you to format it another way (sed/awk like).

---------- Post updated at 11:36 PM ---------- Previous update was at 11:35 PM ----------

to load the file containing macro
:so <filename>

---------- Post updated at 11:38 PM ---------- Previous update was at 11:36 PM ----------

...but it is a VERY VERY VERY long time since i used it so ... not sure the vi version i used behave the same way than your current one.

---------- Post updated at 11:49 PM ---------- Previous update was at 11:38 PM ----------

if you are just trying to remove first and last line of a file :

Code:
sed -i "" '1d;$d' yourfile

# 3  
Old 06-08-2011
What I have is an HTML product catalog that has written text like product descriptions and the like and below it there is product specifications which is what I am grabbing for my database. Basically, I'm just looking to delete the first n lines of the file. However, n is dependent on wherever the written text ends. I can do it easily with vi commands in insert mode, but not command mode. I may just end up writing a macro and calling it from my script for the parts I can't do in command mode.

Dkr.
# 4  
Old 06-09-2011
"However, n is dependent on wherever the written text ends"

Please post an example of input and expected output so we may be able to help you to generate the output as you need

Previous Thread | Next Thread
Test Your Knowledge in Computers #831
Difficulty: Easy
Asynchronous JavaScript and JSON (or AJAJ) refers to the same dynamic web page methodology as AJAX, but instead of JSON, XML is the data format.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Switching server in UNIX via shell script (SSH )

Requirement: I am writing a shell script which take some file from say Server1 and I have to execute some command on Server2 then I have to manipulate the data and based on that I have to produce report. Problem:I can schedule the shell script on only one server (Server1 or Server2) So, In the... (3 Replies)
Discussion started by: raks2301
3 Replies

2. Shell Programming and Scripting

Switching user inside a shell script doesn't seem to work

Linux version : Oracle Linux 6.4 Shell : Bash The following script will be run as root. During the execution, it should switch to oracle user and execute few commands. After googling and searching within unix.com , I came up with the following syntax ## Enclosing all commands in double... (7 Replies)
Discussion started by: John K
7 Replies

3. UNIX for Dummies Questions & Answers

Access Modes

What access modes (permissions) are given to new files you create? (2 Replies)
Discussion started by: naasei
2 Replies

4. Shell Programming and Scripting

problem switching to directory using shell script

Hi, I have an issue with switching to other directory through shell script. I have used cd /Music" but that doesn't help me. Then I have also tried using alias proj 'Music' alias then I get the error permission denied but i set the chmod 777 Music. I am using cShell for my... (3 Replies)
Discussion started by: baluk
3 Replies

5. Solaris

Very Importan - Vim Settings - Error while opening a File using vim

I downloaded vim.7.2 and compiled the vim source . Added the vim binary path to PATH (Because iam not the root of the box) when i load the file using vim it throws me an error Error detected while processing /home2/e3003091/.vimrc: line 2: E185: Cannot find color scheme darkblue line... (0 Replies)
Discussion started by: girija
0 Replies

6. UNIX for Dummies Questions & Answers

Switching user inside a shell script

Dear All, I want to switch a user inside a shell script.My current user say x and user y to whom i want to switch both have login on the same server,the one on which i want to execute my script on. "I want to do something like this su - y Password should be provided in the script itself.And... (6 Replies)
Discussion started by: navjotmannan
6 Replies

7. Shell Programming and Scripting

switching to another user in shell script...

Hi, I have a shell script in which I need to switch to another user and execute some commands and then come back to the original user. To make it more clear - I have to log in as user root then 'su' to jag - execute a script called backup.sh and then logout and come back to root again.. ... (1 Reply)
Discussion started by: bjagadeesh
1 Replies

8. Post Here to Contact Site Administrators and Moderators

different modes?

What are the Guided and Enhanced Modes? This is something new right? Or have I been the most unobservant guy on the face of this planet? At first I thought it was something for Gollum the forumbot, but its there everytime you start a new thread. What is the difference between the two? And how do... (1 Reply)
Discussion started by: blowtorch
1 Replies

9. UNIX for Advanced & Expert Users

linux communications modes

what r the ways in which a linux application communicate with an external system like linux or windows? (1 Reply)
Discussion started by: shil
1 Replies

Featured Tech Videos