Shell: Run vim command using shell - shell

How can I run the vim command ":retab" using a shell script to all the files in the current dir?

I found something that could help you
for F in *.{c,h}pp ; do vim -c ":retab" -c ":wq" "$F" ; done
This should do what you'd like ;) maybe you'll need to change the for loop condition to your needs

Related

Script to download a list with wget in bash

I have a list of download links (list.txt):
https://web.com/file1.zip
https://web.com/file2.zip
https://web.com/file3.zip
and so on up to 100...
I'm trying to build a script that will download my files using wget, but the emphasis here is that every time one file is downloaded, the ls command will be executed.
This is what my long script looks like, but I want to shorten it and make it smarter, and instead of manually writing line by line each time, that the script is read line by line and downloaded in order with the execution of the ls command in each download when the first one to turn has finished
wget https://web.com/file1.zip && ls ; wget https://web.com/file2.zip && ls ; wget https://web.com/file3.zip && ls ;
And so on up to 100
use forloop to squash your similar wget commands.
for i in {1..100}
do
wget https://web.com/file"$i".zip && ls
done
Please be aware of that this is bash shell style. You can also write other shell style.
#!/bin/bash
for iurl in $(cat list.txt); do
wget $iurl && ls
done

command substitution not working in alias?

I wanted to make an alias for launching a vim session with all the c/header/makefiles, etc loaded into the buffer.
shopt -s extglob
alias vimc="files=$(ls -A *.?(c|h|mk|[1-9]) .gitconfig [mM]akefile 2>/dev/null); [[ -z $files ]] || vim $files"
When I run the command enclosed within the quotations from the shell, it works but when run as the alias itself, it does not. Running vimc, causes vim to launch only in the first matched file(which happens to be the Makefile) and the other files(names) are executed as commands for some reason(of course unsuccessfully). I tried fiddling around and it seems that the command substitution introduces the problem. Because running only the ls produces expected output.
I cannot use xargs with vim because it breaks the terminal display.
Can anyone explain what might be causing this ?
Here is some output:
$ ls
Makefile readme main.1 main.c header.h config.mk
$ vimc
main.1: command not found
main.c: command not found
.gitignore: command not found
header.h: command not found
config.mk: command not found
On an related note, would it be possible to do what I intend to do above in a "single line", i.e without storing it into a variable files and checking to see if it is empty, using only the output stream from ls?

With Bash or ZSH is there a way to use a wildcard to execute the same command for each script?

I have a directory with script files, say:
scripts/
foo.sh
script1.sh
test.sh
... etc
and would like to execute each script like:
$ ./scripts/foo.sh start
$ ./scripts/script1.sh start
etc
without needing to know all the script filenames.
Is there a way to append start to them and execute? I've tried tab-completion as it's pretty good in ZSH, using ./scripts/*[TAB] start with no luck, but I would imagine there's another way to do so, so it outputs:
$ ./scripts/foo.sh start ./scripts/script1.sh start
Or perhaps some other way to make it easier? I'd like to do so in the Terminal without an alias or function if possible, as these scripts are on a box I SSH to and shouldn't be modifying *._profile or .*rc files.
Use a simple loop:
for script in scripts/*.sh; do
"$script" start
done
There's just one caveat: if there are no such *.sh files, you will get an error. A simple workaround for that is to check if $script is actually a file (and executable):
for script in scripts/*.sh; do
[ -x "$script" ] && "$script" start
done
Note that this can be written on a single line, if that's what you're after for:
for script in scripts/*.sh; do [ -x "$script" ] && "$script" start; done
Zsh has some shorthand loops that bash doesn't:
for f (scripts/*.sh) "$f" start

Executing Bash functions from within Vim - how can I do it? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Commands executed from vim are not recognizing bash command aliases
I have the following function in my ~/.bashrc file (on my Ubunut box)
# convert tex to pdf, no bib
function t2p {
latex $1 && latex $1 && dvips $1 -o $1.ps && ps2pdf $1.ps && rm -f $1.dvi }
# convert tex to pdf, with bib
function tb2p {
latex $1 && bibtex $1 && latex $1 && latex $1 && dvips $1 -o $1.ps && ps2pdf $1.ps && rm -f $1.dvi }
For example, to convert a tex file f.tex to a pdf file and bibtex it in the right order, I call tb2p f. This works very well if I'm in a Bash terminal. However, to get to the Bash prompt from within Vim I actually have to execute the command :sh first.
To simplify the above procedure I tried to execute the functions t2p and tb2p inside Vim by :!t2p f. Vim then tells me that it cannot find the function t2p. I did some Googling and read that I should put these functions into the file /etc/bash.bashrc to make them visible to Vim. Unfortunately, this didn't work in my case. Vim still doesn't know about the function.
At the end of the day I would like to be able to call my functions from within Vim by using a keyboard shortcut. My questions are therefore as follows:
How can I let Vim know about ~/.bashrc functions?
How do I setup a keyboard shortcut in ~/.vimrc for a function in ~/.bashrc?
Thank you very, very much.
Try using :!bash -c t2p in Vim. If your alias is limited to interactive shells also add the -i flag.
Vim runs bash commands with the -c argument, which makes the shell non-interactive and non-login, and that bypasses reading the startup files.
You can override this with an environment variable.
From man bash:
When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV
in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to
read and execute. Bash behaves as if the following command were executed:
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search for the file name.
Alternatively, vim itself can be asked to run command shells as login shells. From vim :help shell we get the following:
On Unix the command normally runs in a non-interactive
shell. If you want an interactive shell to be used
(to use aliases) set 'shellcmdflag' to "-ic".
For Win32 also see |:!start|.
Try adding set shell=bash\ --login to your .vimrc.
To convert the current file, type :!tb2p %, the % will be expanded by Vim for you when executing the script.
Once it's working, you can add a mapping to make the whole process even faster:
nnoremap <F12> :!tb2p %<CR>
You can always define your functions in a separate file and put that file in a folder
in your PATH environment variable.
In my case my personal functions that I would use go to ~/bin
In your case for t2p:
create a file t2p in ~/bin with the contents:
#!/bin/bash
# convert tex to pdf, no bib
latex $1 && latex $1 && dvips $1 -o $1.ps && ps2pdf $1.ps && rm -f $1.dvi
then make the file executable:
> chmod +x t2p
make sure ~/bin is in your path, by putting the following in your ~/.bashrc:
export PATH=${HOME}/bin:${PATH}

pdf2swf using MAC OS Batch command or Apple script

How do I write a batch process on the Mac for pdf2swf, I want to convert all pdfs in a folder into swf. But pdf2swf doesn't have a option to convert a folder of pdfs to swfs, you have to do it one at a time. I'm not sure how if I should use a Apple script or a Shell script, either one I'm not sure how to get or assign a file name variable.
pdf2swf file_name_variable.pdf -o file_name_variable.swf -T 9 -f
Thanks
Open up Terminal and do something like this:
$ for f in `find /path/to/my/pdf/directory -name \*.pdf` ; do
> echo "Processing $f..."
> pdf2swf $f -o ${f/.pdf/.swf} -T 9 -f
> done

Resources