Vim --- Read from an External Command, Inserting Right Where the Cursor Is - vim

:r !program opens a new line, inserts my program's output and then inserts a line after it.
I simply want to insert the output right where the cursor is without that additional mess.
I figured I can:
Run a before macro
mai^M^[`a
"Mark where I'm at, insert a line and go back
Run my command
:r !echo -ne "line1\nline2\nline3"
Run an after macro (cleanup the lines)
$mb:j!^M`a:j!^M`b
"Go to the end of inserted outpu
"Mark it b
"Join with the next line
"Go to the first mark
"Delete the inserted newline with :j!
"Go to the second mark
How can I combine this into a single command?
I'd like to be able to do:
:Readhere !echo -ne "line1\nline2\nline3"
where :Readhere would be my custom command.

This might do what you want. (You don't need the !)
command! -nargs=1 ReadHere exec 'normal! i' . system(<q-args>)
This creates a command called ReadHere that takes everything as a quoted argument and passes it directly to the system command. Then we use exec to insert everything in normal mode. (This might not be robust enough)
Example: Starting buffer is
one two three
Running :ReadHere echo -ne "line1\nline2\nline3" where the cursor is on the w produces
one tline1
line2
line3wo three

Related

why is vim's delete command so slow

I have a file that contains about 5000 lines and I want to delete all lines that have 'some_string' so I first search for /some_string then I execute :g//d. This takes over 5 minutes to delete ~90% of the lines. What gives?
In comparison, if I run sed -i '/some_string/d' some_file it takes 46ms.
Add an underscore to your command.
I experienced a similar problem and it turned out to be each line being copied to my system clipboard. By adding a _, you tell vim to use the blackhole register.
:g//d_
The help gives the following syntax for :d
:[range]d[elete] [x] Delete [range] lines (default: current line) [into register x].

How to write a string in a file using vim editor from command line

I want to create a new file using vi editor from command line and add a string to it multiple times say 100. Using vi -S command.script file.txt is supposed to do the trick where a new file file.txt will be created and the commands given in command.script file can write to this file. My command.script contains
:%100a hello world
:wq
But its's not working, what I am doing wrong?
If you interactively execute :%100a hello world in a Vim session, you'll get E488: Trailing characters. Looking up :help :a:
:{range}a[ppend][!] Insert several lines of text below the specified
line. If the {range} is missing, the text will be
inserted after the current line. [...]
These two commands will keep on asking for lines, until you type a line
containing only a ".".
tells you that the text has to be put in following lines (and concluded by a line with only a . character).
Or did you mean to use the normal mode a command? (That one takes a [count] to multiply; your %100 range is wrong, too!)
You can also use the low-level function append(), repeating the string with repeat().
summary
$append
hello world
[...]
hello world
.
execute "$normal! 100ahello world\<CR>"
" Easier with o instead of a:
$normal! 100ohello world
call append('$', repeat(['hello world'], 100))
non-Vim alternatives
But honestly, if that is your real use case (and not just a simplified toy example), you don't need Vim at all for this. Here's one example for the Bash shell:
$ for i in $(seq 100); do echo "hello world" >> file.txt; done

Capture the output of an interactive script in vim

I have a an interactive Perl script, which prints prompts to STDERR and reads lines from STDIN. The final output of this script is an IP address, printed to STDOUT. Here's a numpty version of such a script as an example.
my #pieces;
for (1..4) {
print STDERR "please enter piece $_ of the IP:"; chomp(my $in = <>);
push #pieces, $in;
}
print join '.', #pieces;
print "\n";
I use the vim-fireplace vim plugin. This plugin has a feature where I can say:
:Connect nrepl://127.0.0.1:9999
I want to know how to configure vim so that when I issue a particular command, let's say:
:InteractiveConnect
it will do the following:
Run the Perl script, allowing me to enter 4 pieces of the IP address.
Capture the IP address output by the Perl script.
Interpolate the IP address into the :Connect command
Run the :Connect command.
A bit more info based on some of the responses:
If I call this script using:
:!/path/to/myscript.pl
Then it executes fine and I am able to see the result from it printed in the vim window, followed by
Press ENTER or type command to continue
If the output of the script is being saved in some buffer after execution via !, is it possible to get access to that buffer in vimscript and just capture the bit I want (the last line) with a regex?
Okay, there's probably a more elegant way to do this, but how about this:
function! <SID>InteractiveConnect()
let tempfile=tempname()
exe '!/path/to/your/script.pl >' . shellescape(tempfile)
try
exe 'Connect nrepl://' . readfile(tempfile, '', -1)[0]
finally
call delete(tempfile)
endtry
endfunction
command! -nargs=0 InteractiveConnect call <SID>InteractiveConnect()
This creates a temporary file, writes to it with the script (using system() doesn't work because it doesn't wait for input), reads the last line in the tempfile to the Connect command, and then finally deletes the tempfile.
Maybe something like:
exec 'Connect nrepl://' . matchstr(system('your/script.pl'), '^.\+\%$')
(Untested.) This runs the script using system() then matches the output against the regular expression ^.\+\%$, (where \%$ means end-of-file; if your file is terminated with a newline, an additional \n might be neccessary before it) and feeds the matched str to the Connect command. .

function failed when call it from a command in vim

When I find the word in the current file, I need to first type "/keyword", but I can't see all the matched rows, So I tried to use the following command to do a shortcut, but it doesn't work, could you please help check why it failed?
function! FindCurrentFile(pattern)
echo a:pattern
execute ":vimgrep" . a:pattern . " %"
execute ":cw"
endfunction
command! -nargs=1 Fi call FindCurrentFile(<args>)
By the way, if you just need a quick overview over the matches you can simply use
:g//print
or
:g//p
(You may even leave out the p completely, since :print is the default operation for the :global command.)
When the current buffer has line numbers turned off, the results produced by :g//p can be difficult to take in fast. In that case use :g//# to show the matches with the line numbers.
Another trick that works for keywords is the normal mode command [I. It shows a quick overview of all the instances of the keyword under the cursor in the current buffer. See :h [I.
try to change the line in your function into this:
execute ':vimgrep "' . a:pattern . '" ' . expand("%")
<args> is replace with the command argument as is - that means that if you write:
Fi keyword
the command will run:
call FindCurrentFile(keyword)
which is wrong - because you want to pass the string "keyword", not a variable named keyword.
What you need is <q-args>, which quotes the argument.
BTW, if you wanted more than one argument, you had to use <f-args>, which quotes multiple arguments and separates them with ,.

Vim - how to start inserting at the end of the file in one step

Is there a single shortcut to start inserting in the new line at end of the file?
I'm aware of G + o combo.
There's also the command line option "+":
vim + myfile.txt
Will open myfile.txt and do an automatic G for you.
Not that I know of - G+o is what I would have suggested too, but that is 2 steps :)
You could always create a macro which does G+o, and then you can invoke the macro which will be 1 step.
Adding the following into ~/.vimrc will create one for you:
:nmap ^A Go
To type the "^A" first press Ctrl-V, then press Ctrl-A. You can then use Ctrl-A to append at the end of the file when not in insert or visual mode.
echo >> myfile.txt && vim -c 'startinsert' + myfile.txt
You can also save the above command in a script and then use $1 instead of myfile.txt, name your script myvim ( or whatever you like ) and always open your files and start writing away instantly.
myvim myfile.txt
You could stick the map definition in your .vimrc and then invoke it when the you open the file.
Or, if you only want to do this for a particular file, you could create an autocmd for that file type that does it automatically. See autocommand in the vim doc's.

Resources