Can Your Editor do This?

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Often when doing web design jobs I've had to laboriously re-work price lists from clients into HTML tables (am sure most here have had to do the same thing at some point!). They send things in Word or Excel formats (totally useless for web designers, but that's all part of the service), I start by copying and pasting into my editor, then add td's and tr's by hand. Tedious! Until I started using Vim, and worked this out ...

(sorry the above is Flash, I have an original video file around if anyone hasn't got Flash)

See that price list magically change into a html table? All I had to do was record doing the conversion for the first line then tell Vim to repeat it 58 times. Later I used Vim to add formatting to those prices by adding zero's, decimal places and £ symbols too.

I should note that only the prices are a standard width, look at the menu items: some are longer, some shorter.

I used the 'A' to append at the end of line for the at the end of the line. 'b' to move back one word, price in this case, followed by 'i' to insert text. There were three columns of prices, so I did this three times to insert the '' items. Then finally used 'I' to insert text at the start of line so the first '' could be inserted.

All this was recorded to 't' (by typing 'qt' at the start), so all I had to do to repeat the command 58 times was type:
'58@t'

I've converted everything in far less time than expected and the job looks professional. My question is: can your editor do this? Smiling

a Padded Cell our articles site!

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Yep. In Emacs you would use keyboard macros.

C-x ( to start recording, move around and edit as normal, C-x ) to stop recording. Then either C-x e to repeat once, C-x e e e ... to repeat a few times, or C-u n C-x e to repeat n times. Alternately, run M-x apply-macro-to-region-lines etc to apply the macro to a selected area or various other ways. You can record pretty much any arbitrary Emacs action.

Some more neat features of keyboard macros are that you can edit or append sequences to them, name and save them to file, bind them to custom/temporary shortcuts, and program them to do different things on each step or run.

Smiling

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

In ultra edit you can also record macros and play them a certain number of times (or until the end of a file, or until a search no longer is found)

Also, if you have everything lined up as text, you can enter column mode and type things in a whole column to enter same thing for many lines.

-Greg

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Abhi wrote: You can record pretty much any arbitrary Emacs action.

Very good, but can Emacs (or Ultra Edit) jump back words, or jump to the start of a line? These were useful and essential as the items in the first 'column' were all different lengths.

Abhi wrote: Some more neat features of keyboard macros are that you can edit or append sequences to them, name and save them to file, bind them to custom/temporary shortcuts, and program them to do different things on each step or run.

Same for Vim, the joy being that it's so simple. It stores scripts as a series of keystrokes within a buffer, so it can be pasted, changed, then copied back to the buffer and the script is changed.

Greg wrote: Also, if you have everything lined up as text, you can enter column mode and type things in a whole column to enter same thing for many lines.

This is such a useful feature! Naturally Vim and Emacs can do this too, but it's definately worth mentioning!

The more I think about it, the more I realise that Emacs has keyboard shortcuts, whereas Vim is keyboard shortcuts.

To give everyone an idea of just how great, powerful and useful Vim and Emacs are I point you to: the Tetris plugins! Both editors have plugins written, under their own scripting language, to allow the user to play Tetris. That just cannot be beaten. Smiling

a Padded Cell our articles site!

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

I'm not as cool as you guys with your fancy text editors but I use excel to make the tables. It's a piece of cake. You take the data they give you and space it out. For example

Blank column | One chunk of data | Black column | Another chuck of data | Blank Column | Some more data | Blank column
Blank column | One chunk of data | Black column | Another chuck of data | Blank Column | Some more data | Blank column
Blank column | One chunk of data | Black column | Another chuck of data | Blank Column | Some more data | Blank column
Blank column | One chunk of data | Black column | Another chuck of data | Blank Column | Some more data | Blank column
'

Then you put a in the first blank column, a in the next blank, and so on until you get to the end and do . Then you use that drag tool thing, and drag each of the tags to the end of the rows. Then just copy and paste into your site. Beat that Sticking out tongue.

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

teammatt3 wrote: Then just copy and paste into your site. Beat that

Lol, that actually makes sense in a weird way. Just substitute: 'excel' for: 'Open Office Calc' and you've got me sold. Wink

Although that's a method very specific to just the task of making tables, whereas the powa(tm) of Vim extends to all tasks! Laughing out loud

a Padded Cell our articles site!

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

JeevesBond wrote: can Emacs (or Ultra Edit) jump back words, or jump to the start of a line?

Sure. I've bound those functions to Home, End, C-, C-, etc. Depending on your mode, you can also move between function/class/variable definitions and other blocks of text, as well as normal whitespace-delimited text.

JeevesBond wrote: It stores scripts as a series of keystrokes within a buffer

Does it store keystrokes, or the code that the keystrokes represents? Emacs keyboard macros represent vanilla Emacs Lisp code, so when you save it you just get a bit of reusable Elisp code. It's not limited to one buffer (terminology may differ), so you can record macros over multiple buffers or files, even open/write files and run shell commands.

JeevesBond wrote: Emacs has keyboard shortcuts, whereas Vim is keyboard shortcuts.

I think that's a poignant observation, at least for the Emacs side. I'm fond of saying "Emacs is an editor written in Emacs Lisp; Emacs Lisp runs on Emacs". Wink Keybindings, and indeed most of the user interface, are loosely coupled to the underlying Lisp, so you've hit the nail on the head. That's what makes it so deeply programmable. 'Emacs' is derived from 'Editor Macros', after all.

Matt, the sentiment is the same: to have the machine do the tedious work for you. What Jeeves is trying to show is how that philosophy can (and should) be part of your regular editor, rather than forcing you to invent jerry-built solutions. Smiling

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Abhi wrote: Does it store keystrokes, or the code that the keystrokes represents?

The keystrokes. My little vim script (like My Little Pony but not suitable for small children), pasted from it's register (I used the wrong terminology before) looks like: -
o</tr><tr><ESC>jA</td><ESC>bbbi<t<BACKSPACE>/td><td><ESC>bbbbbi<t<BACKSPACE>/td><td><ESC>bbbbbi</td><td><ESC>I<td><ESC>'
I had to replace control characters with and . Aside from that, above is exactly how Vim stores it's scripts. There is a scripting language with Vim, but that's something different.

The script works because Vim starts in something called 'Normal mode', which is mainly for navigation and quick editing/deletions. In Normal mode 'o' means 'Insert new line below the current one and enter Insert mode (where text is entered), takes it back to Normal mode. 'j' moves down one line then 'A' inserts text at the end of the current line. 'b' moves back one word and 'i' puts Vim into Insert mode. I could go on, but don't think that's needed! I like to think of these Vim scripts (macro seems like a better word, but I was told-off for calling them that) as being like another person using your Vim. They function the same way the user uses Vim. Smiling

Notice when I make a mistake and Vim meticulously records it, that can be really annoying for Vim users, who'll see their mistake every time they type :registers. Smiling

Abhi wrote: It's not limited to one buffer (terminology may differ), so you can record macros over multiple buffers or files, even open/write files and run shell commands.

I used the wrong terminology before, sorry. I meant register, not sure whether a script can span multiple buffers. *strokes chin* must try that sometime.

a Padded Cell our articles site!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.