• Rob's side project: I recently started Gun-Forums.com and we currently have a $100 raffle - if you're into guns, join up and enter the raffle to win $100.00. Gun Forums

text editor with command line features

mgfacioli

New Member
Hi!!

I need a text editor where I type a command and both the command and its result are incorporated into the document I am editing.

Something like xiki editor.
 


JasKinasis

Well-Known Member
I've never heard of, or used xiki, so I'm not sure offhand.
Personally the only editor I ever use is vim. And I always run it in the terminal!
Vim does everything I need and tons more besides.

Emacs also runs in the terminal and has tons of powerful extensions available. So I would imagine that will probably also be able to do what you need!
 
Last edited:

mgfacioli

New Member
I've used xiki before. However, it was built with Ruby. Its installation is not friendly at all and invariably overloads the system.
 

mgfacioli

New Member
Hello friends!!
What I'm looking for is something like this: you're editing a handout with shell commands for your students. In this document, in addition to comments, you want to include the shell commands you are presenting and their output.
That's where xiki had a really cool improvement. Let's say you entered a command like

$ ls -alh

If, after typing the command, you press Enter, your command is treated as a normal line of text and you can continue editing your workbook.
But, if after the command you type Ctrl + Alt + Enter (I don't remember exactly which shortcut), the editor executes the command and returns the output to the body of your document. You can edit the output like a normal text or continue editing your document.
It was not necessary to go from your editor to the shell, execute the command, copy the result, go back to the editor and continue working.
This feature saves a lot of time when preparing long handouts and technical documents.

I've tried other text editors: Vim, gedit, leaf, etc., but I haven't found anything like it.

Do you have any other suggestions?
 

JasKinasis

Well-Known Member
Offhand - I don't know of anything that does what you've described in xiki.

But it is possible to read in the results of shell commands in vim and it's not a complicated process.

And we can even set up a macro to use in vim to allow you to enter a command in your text, then after moving to the next line, you will be able to fire off the macro and it will execute the command you've entered in a shell and will read the results into the document.


NOTE: In the blurb that follows - anything that is inside square brackets is a special key.
e.g. [ctrl+r] means hit the ctrl-r key-combo. [enter] means hit enter/return.

Also this process is assuming that you're using $ at the start of the line to indicate a bash-prompt in your document:

1. Enter edit mode on a new line using o
2. Type your command in your document:
$ ls -alh[enter]
3. Now switch to command mode and start recording a macro in register e
[esc]qe
3. Use the following to visually select the command you just typed and yank it to the copy/paste buffer and move back to the line below:
[esc]0kwv$yj
4. Use the following to execute your command and read the output into your document
:r![ctrl+r]"[enter]
5. Hit o[esc] to ensure you have a blank line at the end of the output.
6. Now press q to finish recording the macro

Now you've set up a macro in register e in vim. That should be saved when you quit vim, so the next time you reload vim it will still be available to you. You won't need to set that up again when writing another document.

So now you should be able to type another command in your document and execute it in your macro without leaving edit mode:
e.g.
1. Enter edit mode e.g. using i, or o
2. Type your command and press enter
e.g.
$ ls -t | head -n 5[enter]
3. Press [ctrl+o]@e to run your macro.
Your macro will run, the output will be added to your document and you'll be dropped straight back into edit mode, so you can continue editing your document.

If you switch to command mode after entering a command using [esc] and then move the cursor to the line after the command - you can use the command @e to execute the macro.
Or again, if you're still in edit mode, use [ctrl+o]@e whilst on the line after the command.

In both cases - in order for the macro to work, your cursor must be on the line directly underneath the command you want to execute.
And again - it assumes that your commands will have a $ at the start of the line.

So that will yield similar functionality to what you described having in xiki!

And you don't have to store the macro in register e you can use any of the registers from a-z.
Whenever I create a macro in vim - I try to store it in a register that I can associate with a particular word that is relevant to what the macro does! So in this case e for execute, or perhaps x for execute.

Any problems - give me a shout!
 
Last edited:

mgfacioli

New Member
Hello, JasKinasis!!

I really liked your approach precisely because I am enjoying Vim more and more. However, I must be making a mistake when recording the macro, as it is not running properly.

Looking at the record, she looks like:

:reg e
--- Reg...---
"e ^[0kwv$yj:r!^R^Mo^[

When I run it, the result is not as expected.

When recording the macro, I must be making a mistake in this piece of commands:

:r![ctrl+r]"[enter]

Is there any way to create or edit this macro manually?
 
Last edited:

JasKinasis

Well-Known Member
What does the macro do when you run it?
Do you get any error messages from the shell? or any kind of output read into the file?

I have to admit - I didn't actually test this. Probably should have mentioned that. I just posted the steps I would take in vim in order to record a macro to do what you wanted to do.

So there may be some minor errors in the process in my post. Perhaps a step that isn't quite 100% right.

Looking at the content of your register, yours appears to be missing the double quote " between the [ctrl+r] and [enter] keypresses.

So the output of :reg e should look something like:
Code:
"e ^[0kwv$yj:r!^R"^Mo^[

If that isn't the problem - the only other possible thing I can think of offhand is the newline in the selection we yanked.

Depending on the line-endings in your file (standard unix line-endings vs MSDOS line endings) - that may have an effect on the pasted command in the [ctrl+r]"[enter] part.

If the line-ending is the problem - then during the visual selection part of the macro recording - you could use v $ to select to the end of the line then press h to move the cursor back one character - to avoid copying the newline character at the end. Then press y to yank the line.

So to avoid the possible line-ending problem - the keypresses to record the macro from start to finish (starting on a blank line immediately after the line containing your command) would be:
q e [esc] 0 k w v $ h y j : r ! [ctrl+r] " [enter] o [esc] q

And once again anything in square brackets is a special key, or a key-combo. And we're assuming that all of your example commands that you are selecting and executing are single line commands and that the first character in the line is a $ (which we skip over and avoid).

Then when you run :reg e again, it should output something like this:
Code:
"e ^[0kwv$hyj:r!^R"^Mo^[

Re: editing registers
It is possible to edit the contents of a register.
Typically it would involve pasting the content of the register into a blank line in your document, then edit it, before deleting it back to the buffer.

So you could achieve that by using something like i [ctrl+r] e - to paste the content of register e into a blank line in your document and then make your edits, before visually selecting the edited line and then deleting back to register e using something like v $ " e d

However, because we've used the [ctrl+r] keybind - when you paste the content of the buffer, the [ctrl+r] character will recall the content of the " register and will expand and include the current content of that register into the dumped macro - which would not be helpful!

So because we've used special keys - it's probably easier to try recording the macro again, until you get it working properly.

If the missing " isn't the problem, then the only other thing I can think of is the potential line-ending problem. In which case, the workaround I've posted should fix that.

If I get time this evening, I'll have a go at recording the macro on my Linux laptop and will check it over.

But I'm pretty certain that this should work though. Like, 99.99% certain!
 
Last edited:

JasKinasis

Well-Known Member
ADDENDUM:
Looking at some of my vim related notes - there is another way to edit registers - and this way should work with the special keys included in the macro........

Start typing the following command:
:let @e='

** IMPORTANT ** - don't press [enter] yet.
The following instructions will tell you when to press [enter]!

Next, press [ctrl+r] [ctrl+r] e to paste the current contents of register e into the command you're in the middle of typing. (And yes - that's pressing ctrl+r twice!)

Then make the required edits to the line.
Use the arrow keys to move around in the command-line.
Insert a h before the y and ensure that there is a " between the [ctrl+r] character (^R character) and the [enter] (^M character).

When you have made the required edits, move to the end of the command and close it with a ' and now, finally press [enter] and that should store the modified contents to register e.

Now you should be able to run the modified macro using @e in command mode, or [ctrl+o] @e in edit mode!

EDIT:
It should also be noted that pressing ctrl+R twice before pressing the key for the named register - should allow the other register editing method (from my previous post!) to work, without expanding the contents of the " register.
 
Last edited:

mgfacioli

New Member
JasKinasis, finally works!!

I used the macro recorder ([esc]qe) and followed all the steps you indicated earlier. I'm not sure what I did, but the result was like this:
5565


It's a little bit different (with a strange ^J at the end).

But what matters is that it is working as you predicted. Using a $ as a token I can execute any shell command and retrieve its contents directly into the body of my text.

Thank you!!
 


Members online


Latest posts

Top