Running arbitrary vim commands from bash command line to script vim - vim

I want to script vim to edit files from the command line. For example I want to do something along the lines of:
vim -<SOME_OPTION> 'Iworld<Esc>bIhello <Esc>:wq helloworld.txt<CR>'
or:
echo 'Iworld<Esc>bIhello <Esc>:wq helloworld.txt<CR>' | vim
and have it save the file helloworld.txt with a body of hello world
Is this possible? I've tried a few different approaches but none seem to do it. I realize I can do things like vim +PluginInstall to run Ex commands from the command line, but I'd love to be able to string together arbitrary motions

This can be achieved with the + flag and the :normal command:
$ vim +"norm Iworld" +"norm Ihello " +"wq helloworld.txt"

I think what you are looking for is vim's -w/W and -s {scriptin} option. Well in your case you should make a scriptfile, and with -s file to let vim execute all your "key presses"
I think vimgolf has used these options too.

Related

Are there any Linux text editors that understands bash colors?

I am going through some logs written by a program that normally just logs to the console. Since it outputs to bash, I see a bunch of characters which are used for coloring in bash.
Is there any text editor out there that can interpret these character sequences and display the lines in color as bash does? Would be nice to be able to search through these logs without seeing a bunch of otherwise garbage characters.
If you don't want to use an editor you could use:
echo -e $(cat colorfile)
less -r colorfile
Or use vim addon AnsiEsc:
Download the AnsiEsc.vba.gz vim scripts file from (https://www.vim.org/scripts/download_script.php?src_id=14498)
Install details:
Open the AnsiEsc.vba.gz vim-scripts file in vim:
vim AnsiEsc.vba.gz
:so %
:q
Then open your file with vim and type
:AnsiEsc

In vim, how do I start vim at the end of the file and what commands are available with -c

By reading this question I learned that you can give -c to vim such that it will call a command. The question is then answered by calling vim -c startinsert. Good, but what if I want to give other commands? I only know the shortcuts like i for :startinsert.
For example I'd like to write vim -c gotoend but it doesn't exists.
After some reading, all : commands, which are ex commands are written here and in man vim, but where do I find a lis or method to know what can be done?
The commands for options -c are called ex-command named after ex(tendended line editor). The list of commands are in vimdoc and vimhelp.
The command to jump to the end of file is just :$ so you can do this in command line:
vim -c $ <file>
Please note you probably better escape or quote $ as it's a shell metacharacter:
vim -c \$ <file>
or
vim -c '$' <file>
:help :normal. Also note that instead of -c command you can use +command.
With that, I'd typically say vim +"norm G" foo.txt to start at the bottom. You could even alias it: alias vimbottom='vim +"norm G"'; then you can use vimbottom foo.txt to start at the bottom, if you prefer.
where do I find a lis or method to know what can be done
There is no better resource for Vim than its :help. And I don't know any place that has all the ex-commands in one place, since it would be a huge list. (EDIT: :help :index actually has that. Thanks, Christian Brabandt!)

Execute an external command on startup

I use vim regularily for all my needs. On very rare occasions I need to open binary files via a hex editor look, and for that I start vim on the file and then run it through xxd via the command: %!xxd
My question is, how can I have my command line open a file directly in this manner, if the option exists? something like typing: gvimbin <file> and then it opens in the right manner.
Edit: to be clear, I am looking for a complete solution that allows running vim exec commands on startup.
You can execute commands after Vim startup by passing them via -c:
$ gvim -c '%!xxd' file.bin
This can even be simplified so that when Vim is started in binary mode (-b argument), it'll automatically convert the file. Put the following into your ~/.vimrc:
if &binary
%!xxd
endif
and start with:
$ gvim -b file.bin
Also have a look at the hexman.vim - Simpler Hex viewing and editing plugin; it makes it easier to deal with hexdumps.
Like Felix say:
xxd <file> | vim -
You can put this into script for example vimxxd:
#!/bin/sh
xxd $1 | vim -
and use like: vimxxd file.txt

How to use the vim commands in script?

I would like to delete the second line of a text file. Using vim or ex any kind of text editor form script.
I have came up with these commands but does not work for me.
#!/bin/sh
iconv -f Utf-16le -t utf-8 ~/Desktop/upload.csv -o ~/Desktop/finalutf.csv
vim ':2d|wq' ~/Desktop/finalutf.csv
ex -sc '%s/\r//e|x' ~/Desktop/finalutf.csv
The script .sh is executable. First line of code works, 3rd as well but not the second one. I tried to see the documentation for the vim commands to delete the specific line, and tried it on terminal and it works (:2d)
trying to use it in script seems confusing. I am new to Ubuntu as well as vim and scripts, trying to learn seems hard enough, with so much of complex commands explained leaving far away, for beginners learning vim, in its documentation.
This is the sort of thing that sed is built for.
sed -i 2d ~/Desktop/finalutf.csv
However, if you must use vim you can do
vim -c "2d|wq" ~/Desktop/finalutf.csv
This works for me:
vim -c "2d|wq" infile

escape character in vim command

I want to run command like this:
vim -c "%g/blablabla/norm /str<ESC>cwSTR" file
How I write escape character in the command?
As you type the command, use control-v then escape to enter the escape.
However, I have to question whether vim is the right tool for this job. Normally, you would be better off with something like sed. That said, I'm not quite clear what the vim command is up to, so maybe you do need it.
This is what I would do:
vim -s -c ':exec "normal %g/blablabla/norm /str\<Esc>cwSTR"' file
Notice that I am using :exec to expand the "\" to the real . The advantage? it is more script friendly. However, I am not sure what this command is doing, are you using some of your custom maps here?

Resources