msgbartop
Tips, notes, HOWTOs
msgbarbottom

18 Nov 09 Simple Vim Macros

I found myself editing a configuration file (multiple actually) at work today and ran into an issue.  The config file looked similar to:

Var_1=A
Var_2=B
Var_3=C
...
Var_146=ABC
Var_147=ABD

As it happened I needed to remove one of the lines at position 23.  The program that reads/uses these config files has an issue if there is a break in the sequence.  So after removing the line with “Var_23″ I needed to shift all of the numbers in following lines down by 1.  Here is how I accomplished that.

In vim I put my cursor on the 24 in the line that now follows the “Var_22″ line and performed the following commands:

qa
CTRL-X
j
q
125@a

Here is what happened broken down by line:

1: Here I am recording a macro (q) with the name or identifier of ‘a’.  Note you will probably see some indication in the last line that you are in “recording” mode.

2: CTRL-X decrements the number under the cursor.  Since I am on the 24 that decrements it to 23.

3:  I move down 1 line and now the cursor is on the 25 in the next line.

4: I quit recording the macro.  I know have a named (a) macro that includes the commands “decrement the number under the cursor and move down one row”.

5: I perform that named (a) macro 125 times.  Now realistically I only needed to do it 123 times.  It didn’t seem to matter that I used 125 but YMMV.  The first line after the last line I wanted to change was a blank line so I assume the macro quit when the CTRL-X failed.  You are probably better off using the exact number.

And there you have it.  I successfully renumbered all of the variables below the line I deleted to be in sequence.

Tags: , , , , ,

18 Dec 08 Remembering The Shell’s Special Variables

I can never seem to remember what all of these default parameter variables are.  I found this small shell script that prints them out when I forget.

#!/bin/sh -vx
#######################################################
# example_1.1 (c) R.H.Reepe 1996 March 28 Version 1.0 #
#######################################################
echo "Script name is            [$0]"
echo "First Parameter is                [$1]"
echo "Second Parameter is               [$2]"
echo "This Process ID is                [$$]"
echo "This Parameter Count is   [$#]"
echo "All Parameters            [$@]"
echo "The FLAGS are                     [$-]"

It produces output like this:

#!/bin/sh -vx
#######################################################
# example_1.1 (c) R.H.Reepe 1996 March 28 Version 1.0 #
#######################################################
echo "Script name is            [$0]"
+ echo 'Script name is            [/home/me/bin/shellhelp]'
Script name is            [/home/me/bin/shellhelp]
echo "First Parameter is                [$1]"
+ echo 'First Parameter is                []'
First Parameter is                []
echo "Second Parameter is               [$2]"
+ echo 'Second Parameter is               []'
Second Parameter is               []
echo "This Process ID is                [$$]"
+ echo 'This Process ID is                [15190]'
This Process ID is                [15190]
echo "This Parameter Count is   [$#]"
+ echo 'This Parameter Count is   [0]'
This Parameter Count is   [0]
echo "All Parameters            [$@]"
+ echo 'All Parameters            []'
All Parameters            []
echo "The FLAGS are                     [$-]"
+ echo 'The FLAGS are                     [hvxB]'
The FLAGS are                     [hvxB]

via Richard’s Unix Shell Scripting Universe

Tags: , , ,

SEO Powered by Platinum SEO from Techblissonline