text editing with macros
I run into situations where I have to repeat a single line of text with some minor editing.
For example: 1. insert into table_name (col_one, col_two) values (val_1, val_2); 2. insert into table_name (col_one, col_two) values (val_3, val_4); 3. in开发者_JAVA百科sert into table_name (col_one, col_two) values (val_5, val_6); and so on... (1000 records)I mean, I will only type the first query and write a macro using the text editor to generate the required number of queries. Hope you get the picture.
Please suggest a better text editor for this purpose. As I have already tried and failed with Ultraedit/Textpad. Maybe I am doing something wrong, as I cudnt get Vim to do this either.
For the example given where only the numbers change, this should be possible using the Zeus editor and a macro script. For example this numbers script does something very similar.
To achieve the required result take the numbers script from above and make some minor changes to create an odd numbers and even numbers script.
Then column mark the 1000 lines of the odd column and run the odd numbers script. Then column mark the 1000 lines of the even column and run the even numbers script.
Choices would be Vim or Emacs. Eg. Emacs offer macros and regular expressions, so you should be able to do what you want. Might be non-obvious though.
I think the problem might be more easily solved if you use a shell script to generate the text, rather than macros in a text editor.
An editor is probably the wrong tool here - if you want to generate all those insert statements, you are probably better off writing a small program in a scripting language like perl or python.
Just about any text editor will let you copy lines and edit them, certainly vim.
The right way to tackle this is to do this programatically: create a template file that generates the text file you want. m4, a widely used macro-language is well-suited to this task, since you can mix text and macros freely. Cf. http://en.wikipedia.org/wiki/M4_(computer_language)
E.g. (untested)
define(`txt`, "$1. insert into table_name (col_one, col_two) values (val_$2, val_$3);") forloop(`i',1,1000,txt(i,eval(2*i-1),eval(2*i)))
I'm posting this because the OP asked for a link for how this can be done in Vim or Emacs in one of the comments
;; First set a special variable used for counting in macros to an initial value of 1
C-x C-k C-c 1
;;type your text but use <C-x C-k C-i> in place of the numbers
;;<C-x C-k C-i> inserts the value of the counting var and automatically increments it by 1
insert into table_name (col_one, col_two) values (val_<C-x C-k C-i>, val_<C-x C-k C-i>)
;;repeat your macro 1000 times.
M-1000 C-x C-e
精彩评论