Using VIM command on all open files - bash

I have a list of files i've opened in VIM using this command:
vim `cat list.txt`
I'm looking to run this command on all open files:
:99,104 norm i#
(from lines 99 to 104, insert a comment "#") <--simple, works
When I run the above command to insert it works as expected on the current file. How do I get VIM to run the exact same command on all open files? I tried :argdo, :windo, :bufdo but didn't have any luck.
Any suggestions?

I guess bufdo would work, if you have set hidden (to allow bufdo to change buffers without saving) before:
:set hidden
:bufdo 99,104 norm i#
Then save all files with :bufdo! w or :wa.
(Note: set hidden could be replaced with set autowrite as well.)

I think sed would be a better option if you can use it. sed is a command line editor rather than an interactive editor like vim.
sed -i '99,104s/^/#/' list of files

Because you've supplied all files as arguments, :argdo is the correct command. (Unless you open other files, :bufdo would work, too.) If you don't want to :set hidden (as indicated in the other answer), you have to immediately persist the buffer via :update. Because you're using a :normal command, you have to enclose it in :execute to be able to concatenate another Ex command:
:argdo 99,104 execute 'norm i#' | update

Related

How do I edit current shell command without executing it?

There seems to be quite a lot of information on how to edit and execute a command using your editor using "edit-and-execute-command (C-x C-e)", but what I would like to achieve is take the current shell command, apply certain filtering (using a script) and then return it to prompt for further approval/manual changes before execution. Is this possible with bash?
Latest update based on my experience
The part 0"+y$dd in the following mapping is really something that you should carefully think about and tailor it to your taste/workflow/experience.
For instance, very frequently I've found myself ending up with multiple lines in the buffer, where I only want to execute the one the cursor is on; in this case I can use 0"+y$dd:%d<CR> instead of 0"+y$dd.
And this is just one of the possible scenarios.
Final answer for those who like vim
Set vim as your EDITOR/VISUAL, so that when editing a command line, you will use vim to edit it.
Put au BufEnter /tmp/bash-fc.* nn <Leader>d 0"+y$dd:wq<CR> in your ~/.vimrc file to map Leaderd (which you will rarely use when editing a command) to the action "delete the current line into the + register without the trailing EOL".
you can use either the + or the * register in the mapping above; the ways to paste into the terminal will likely differ; you need the +clipboard option for these registers to be available.
When finished editing a command in the vim editor, hit EscapeLeaderd.
Paste the clipboard into the terminal (this is terminal-dependent).
Original answer
I often need to do the same, and I do it as follows. (I normally use the set -o vi in bash, so points 1 and 2 in the following are different if you use set -o emacs, the default; based on your question it looks like points 1 and 2 are unified in Ctrl+x followed by Ctrl+e, which is harder to type, imho.)
hit Escape to be in normal mode,
hit v to enter the editor to edit the command,
edit the command as I like,
(This is where you ask the question.)
hit Escape0"+y$dd:wq,
Note: 0"+y$, not simply "+yy, as the latter would copy the newline too, and this would result in executing the command upon pasting it in the command line,
paste the clipboard on the command line
how to do this depends on the terminal you are using, I guess; I hit Ctrl+Alt+v in URxvt.
proceed to approval/manual edit.
Clearly this is just a workaround, consisting in copying the edited command into the clipboard before deleting the whole command, so that nothing gets executed upon exiting the editor; however it's the best I can get for myself.
Update
As my EDITOR (and VISUAL) is equal to vim, when I edit the command, I edit it in vim.
In this respect, I have noticed that the buffer is named /tmp/bash-fc.random, where random is a 6-characters alphanumeric random string.
This gives space to a lot of possiblities, if you use vim as your editor, as you can define some mapping in your .vimrc to execute the whole sequence Escape0"+y$dd:wq. For instance, one command that you'd rarely use when editing a command line is Leaderd; therefore you can put the following mapping in your .vimrc file
au BufEnter /tmp/bash-fc.* nn <Leader>d 0"+y$dd:wq<CR>
so that step 4 in the above recipe becomes
hit EscapeLeaderd
It's not possible to do that in Bash/readline but it's possible in zsh
using edit-command-line command:
darkstar% autoload edit-command-line; zle -N edit-command-line
darkstar% bindkey "^X^E" edit-command-line
Now press Control-x Control-e to open your editor, edit line, leave the editor - you will see the updated command line but it will not be executed automatically.
Now that I think about it, maybe a variation of what #kenorb suggested in a comment is the best workaround (as it seems no solution exists), if we want to stick to bash.
What you can do is prepend a # (the comment character in bash) to the command, rather than echo. Then when you exit the editor, the command will be ineffective, and you will only have to press arrow up (or k, if you use set -o vi), remove the # and confirming.
Note that this strategy adds just a few keystrokes, so it can be fairly efficient, depending on your typing level.
These pieces might get you closer:
a) replace the the normal binding for newline newline (ctrl-M)
bind -x '"\C-M":date"
b) grab the current line from the history using !#
replace date with whatever script you want.
c) edit manually
d) if necessary, somehow tie in !!:p which prints the new command to the command line but does not execute it, thus letting you manually edit it.
e) using ctrl-J submit edited command rather than a newline
or they might not ....
There is an option in bash to modify command from history without executing it. I'm not sure it it's possible to use script for this, doesn't seem to be likely. Although, you can make modifications using history modifiers.
Enable option histverify to prevent execution of modified command
Use chain of modifiers to change last command
Use "!!" to put your result to command line for final edit
Here is how it looks:
$ shopt -s histverify
$ ls *.sh
script1.sh script2.sh script3.sh script-return.sh
$ !!:s/*/script1/:p
ls script1.sh
$ !!:s/1/2/:p
ls script2.sh
$ !!
$ ls script2.sh
script2.sh
I'd like to point you to the Composure framework for Bash (I'm not affiliated with it): https://github.com/erichs/composure
It provides draft and revise functions that sound like they could help with what you're trying to do. Here's a (long) quote from the project's readme file:
Composure helps by letting you quickly draft simple shell functions,
breaking down your long pipe filters and complex commands into
readable and reusable chunks.
Draft first, ask questions later
Once you've crafted your gem of a command, don't throw it away! Use
draft () and give it a good name. This stores your last command as a
function you can reuse later. Think of it like a rough draft.
$ cat servers.txt
bashful: up
doc: down
up-arrow
$ cat servers.txt | grep down
doc: down
$ draft finddown
$ finddown | mail -s "down server(s)" admin#here.com
Revise, revise, revise!
Now that you've got a minimal shell function, you may want to make it
better through refactoring and revision. Use the revise () command
to revise your shell function in your favorite editor.
generalize functions with input parameters
add or remove functionality
add supporting metadata for documentation
$ revise finddown
finddown ()
{
about finds servers marked 'down' in text file
group admin
cat $1 | grep down
}
$ finddown servers.txt
doc: down
It does not seem possible with a keyboard shortcut, at least:
$ bind -P | grep -e command -e edit
complete-command can be found on "\e!".
edit-and-execute-command can be found on "\C-x\C-e".
emacs-editing-mode is not bound to any keys
possible-command-completions can be found on "\C-x!".
vi-editing-mode is not bound to any keys
This can be done in native bash using readline specifically READLINE_LINE and READLINE_POINT variables. I use this functionality all the time though not through vim, you would need to get the value of $selected from your vim command and if not empty it takes your original line + your input and replaces your original line with the combination without executing. output as a variable
_main() {
selected="$(__coms_select__ "$#")"
origonal_text=$READLINE_LINE READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}"
READLINE_POINT=$(( READLINE_POINT + ${#selected} ))
}
bind -m emacs-standard -x '"\C-e": _main '
bind -m vi-command -x '"\C-e": _main '
bind -m vi-insert -x '"\C-e": _main '
Edit
Just remembered these two utilities that will let you do this as well.
Vipe allows you to run your editor in the middle of a unix pipeline and edit the data that is being piped between programs.
vp, up, vipe, Neomux (upgrade of nvim terminal) you can do some pretty neat throwing buffers between the terminal and split window.
and Athame (full vim on the command line)
https://github.com/ardagnir/athame
careful with that one though plugins work on the cli and it can get funky if you got tons of plugins

running vim from a windows batch file; quoting quotation marks therein; inserting text

To do repetitive edits, like replacing a string with another in many files, I've been using running vim from windows batch files (help on stack overflow to do that was very useful!). My follow up questions are how to handle it if:
a) the vim command contains a quotation mark as well as spaces
b) how do I "escape" upon inserting in vim, i.e. what is the batch file
equivalent of escape in vim
For instance in regard to (a) I'd like to do:
call vim fn.txt +"1,$s/$/\"/" +wq
To add a quotation mark at the end of every line in file fn.txt, but of course this will not work because windows does not know that the 2nd quotation mark int the line above is quoted.
As an example of (b), how do I insert a line containing the text "inserted line" after line number 7 in file fn.txt
call vim fn.txt +:7 +oinserted line????
where the question marks above indicate where I don't know how to continue.
Alternatives
Unless you really need special Vim capabilities, you're probably better off using non-interactive tools like sed, awk, or Perl / Python / Ruby / your favorite scripting language here.
That said, you can use Vim non-interactively. However, as you've found out, the escaping on the Windows command-line is tricky. The escape character should be ^ (so " becomes ^"), but I also couldn't get it to work in some few quick tests. Just read the documentation for the /S switch on cmd.exe /? and try to understand it :-( Because of that, it's better to put the Vim commands into a separate script, as pass only the script's filespec to Vim. The following details all use that trick.
Silent Batch Mode
For very simple text processing (i.e. using Vim like an enhanced 'sed' or 'awk', maybe just benefitting from the enhanced regular expressions in a :substitute command), use Ex-mode.
REM Windows
call vim -N -u NONE -n -i NONE -es -S "commands.ex" "filespec"
Note: silent batch mode (:help -s-ex) messes up the Windows console, so you may have to do a cls to clean up after the Vim run.
# Unix
vim -T dumb --noplugin -n -i NONE -es -S "commands.ex" "filespec"
Attention: Vim will hang waiting for input if the "commands.ex" file doesn't exist; better check beforehand for its existence! Alternatively, Vim can read the commands from stdin. You can also fill a new buffer with text read from stdin, and read commands from stderr if you use the - argument.
Full Automation
For more advanced processing involving multiple windows, and real automation of Vim (where you might interact with the user or leave Vim running to let the user take over), use:
vim -N -u NONE -n -c "set nomore" -S "commands.vim" "filespec"
Here's a summary of the used arguments:
-T dumb Avoids errors in case the terminal detection goes wrong.
-N -u NONE Do not load vimrc and plugins, alternatively:
--noplugin Do not load plugins.
-n No swapfile.
-i NONE Ignore the |viminfo| file (to avoid disturbing the
user's settings).
-es Ex mode + silent batch mode -s-ex
Attention: Must be given in that order!
-S ... Source script.
-c 'set nomore' Suppress the more-prompt when the screen is filled
with messages or output to avoid blocking.

edit/save a file within shell script

I think this question fall under pipes, am bad at it.
Using one of my shell script, a file is generated with millions of rows.
Before I can use it with another command, I need to edit this file. I need to add a text e.g 'txt' in front of every line.
What i am currently doing now is,
-exit the shell script after file is generated
-open it in vim
-use command :g/^/s//txt/g to add txt at start of each line
-save file
-use it in remaining shell script
I am sure there would be a more efficient way, which i am not aware of. thanks for the help.
As some people said in the comments, you can use GNU sed to do that:
sed -i 's/^/txt/' yourfile.txt
The -i stands for --in-place and edit your file instead of printing to stdout.

How to execute command inside vim?

I want to run shell script inside Vim editor.
I heard it is possible but do not know.
Command:./shell.sh inside vim.
There are multiple ways to do it. A primary question is "Do you want the output from the script in the file?"
If you want the output in the file:
:r!./shell.sh
If you don't want the output in the file:
:!./shell.sh
If you have the line ./shell.sh in the file, you can include the output in the file with:
!!sh
If you've done it before, you have more options.
If you save the command in a named buffer you have still more options.
If you want the script to have a portion of the file (edit buffer) as its standard input, you have an enormous number of options you can use in conjunction with either of these mechanisms.
Prefix the command with a !. For example, open Vim and write:
:!ls
This will execute the shell ls command.
Note that you'll have to be in the correct directory within Vim for this to work.

How to apply regex commands in vi without opening a file

I have a set of multiple files requiring the same set of edits, I am trying to create a bash script for editing them in vi, however I don't know how to use vi within the scripts to make the edits? Any suggestions would be helpful.
I'd highly recommend using sed or awk, both programs use regular expressions for selecting and processing text.
But here's how you can do it using vim too:
Vim has an ex mode (aka commandline version) which solves this purpose and is much easier to use in scripts. Taking the solution from this answer:
You could simply include the following in your bashscript:
ex $yourfile <<EOEX
:%s/$string_to_replace/$string_to_replace_it_with/g
:x
EOEX
For example:
ex file.txt << EOEX
:%s/hello/world/g
:x
EOEX
Or you can use the -c option to pass ex commands to vim.
For example:
vim file.txt -c ':%s/hello/world/g' -c 'wq'
If you want to do a single :s command, then you may we well use sed, or you can use vim -c as #fnatic_shank suggests. For more complex scripts, you can use -S script.vim instead of several -c arguments. I like to use
$ vim -e -s -N -V0vim.log -S script.vim infile.txt
See the help for -N, -V, -S, and especially
:help -s-ex
which includes the warning
If Vim appears to be stuck try typing "qa!". You don't
get a prompt thus you can't see Vim is waiting for you to type
something.
(I ran into this while testing my answer. I kept infile.txt open in vim, so it asked what to do about the existing swap file when I tried to start a new vim as above.) If you want to send the file to stdout, then you might have these two lines at the end of script.vim:
g/^
silent q!

Resources