vim: deleting non-matching lines in command line - linux

I'm using vim to parse a file in the command line (I'm running gentoo linux):
One of the commands is not working as [I think] it should:
vim -c "v/pattern/d | wq" file
For some reason, this is not deleting the lines without the specified pattern when adding | wq. If I open the file with vim and run the same command, it works. If I run it in the command line without | wq it works.
Why is this not working?

The reason is that Vim sees this as :v/pattern/(d | wq), see :help :bar for an explanation. To avoid this, either wrap the :v in :execute:
vim -c "exe 'v/pattern/d' | wq" file
or, simpler, submit this as two separate commands:
vim -c "v/pattern/d" -c "wq" file
PS: Of course, #Bogdan has a point; you don't need Vim for that simple a task.

I suggest you use sed for that:
sed -i '/pattern/d' ./file

Related

Vim, custom tab and vertical split organization on open

Say the files in my working directory are src/example.c src/second.c src/third.c include/example.h include/second.h include/third.h.
I want to open vim in a way that automatically opens three tabs (example, second and third), where each tab contains a vertical split screen between a .c and corresponding .h file. Like the following commands would.
:tabnew include/example.h | vs src/example.c
:tabnew include/second.h | vs src/second.c
:tabnew include/third.h | vs src/third.c
Is there a way I can make a special script that will do this when I open vim?
It is safe to assume files will have the same name.
Ideally, this would happen from a shell script rather than modifying my .vimrc, if that is possible.
well if you want to do that, you clearly need a way to execute vim commands from your shell. Lets see if the vim application supplies that, by using the help command which we should ask first for every shell command:
?> vim --help | grep cmd
--cmd <cmd> Execute <cmd> before any config
+<cmd>, -c <cmd> Execute <cmd> after config and first file
So all that is needed is to chain these commands:
vim -c 'tabnew include/example.h' -c 'vs src/example.c' -c 'tabnew include/second.h' -c 'vs src/second.c' -c 'tabnew include/third.h' -c 'vs src/third.c'
as #Enlico pointed out in the comment, you should use edit or e instead of tabnew in the first command, else you will get 4 tabs. But I used your commands so you can see how easily you would have been able to solve this by reading the --help output.

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!)

Running arbitrary vim commands from bash command line to script 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.

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

Resources