Is it possible to configure VIM in a such way that if I type
vim filename:123:89
it opens file filename goes to line 123 and column 89?
If not through VIM maybe with a hack for the shell?
You can install the file-line plugin to open a file to the line and column specified after the filename. (github mirror)
From the Readme on github
When you open a file:line, for instance when coping and pasting from
an error from your compiler vim tries to open a file with a colon in
its name.
Examples:
vim index.html:20
vim app/models/user.rb:1337
With this little script in your plugins folder if the stuff after the colon is a number and a file exists with the name especified before the colon vim will open this file and take you to the line you wished in the first place.
I'm not sure how to skip to the column, but I've wanted the same feature for ages, so I just hacked up the "jump to line" functionality. In your .bashrc, set
VIM=$(which vim)
function vim {
local args
IFS=':' read -a args <<< "$1"
"$VIM" "${args[0]}" +0"${args[1]}"
}
This splits the argument to Vim by :, then constructs a command line of the form
vim <filename> +0<line>
The +0 is a hack to make sure the default line number is zero.
(If you're not using Bash, you can adapt this into a script and put it in your path, or translate it to your favorite shell language. To edit filename:with:colons, use $VIM.)
I've been using the file-line plugin, but it has a few open issues, and breaks some other vim plugins. So I went fishing for a better solution. Here it is:
function vim() {
local first="$1"
case $first in
*:*)
shift
command vim ${first%%:*} +0${first##*:} $#
;;
*)
command vim $#
;;
esac
}
Limitations:
bash only
Only parses first argument, whereas vim +X parses the first file argument. A more complex version could easily be made with proper command line parsing.
Advantages:
doesn't break other vim plugins
you could easily use $EDITOR and use this with emacs for instance.
compared to Fred's answer it doesn't use IFS/read to parse the argument but uses bash parameter expansion.
also sends in the remaining argument, which might occasionally be necessary.
Related
It's often that I get paths in the format <path>:<line>:<column> (from text matches such as grep or errors on code).
When I double click, it matches the whole string, including line and column, then I usually remove the column, and replace :line with +line to match vi parameter.
Therefore, I'd like having a hack to rapidly open vim at the right point just with a paste. Is there any config on vim level or alias I could use?!
Thanks
There are plugins that trap the BufNewFile,BufRead events, parse out the file name and number(s), and redirect to the corresponding file:
file-file is the minimalistic original plugin
vim-fetch supports multiple ways of specifying the number(s), overloads some mappings (like gf), and even offers a :Fetch command
Use the quickfix list instead:
Vim has a startup option -q to read a quickfix file. So we have
options (depending on your shell): cmd > results ; vim -q results
Or my favorite: vim -q <(cmd)
I have to make a script using vim which opens a file, set the fileformat=unix, and then save the file and exit. Could you please help? Thanks
First, check out whether you have a dos2unix or dos2ux command; it already does this for you.
With Vim, this should do the job:
$ vim -c "wq ++ff=unix" filename
This one in-lines the fileformat change with the :w command; of course, you can also do this separately via -c "set ff=unix".
Notes
You can also do this via a variety of tools, e.g. sed, perl, ...; Vim is a quite heavyweight alternative.
This still starts up a full, interactive Vim instance. Have a look at this answer which additional command-line arguments can turn Vim into batch mode.
Tell me this, why this code does not work in Cygwin ?
I've tried use \. and different symbol combinations between variable.
Scopes tried too ( round, figure ).
Dunno what up.
My code is here and it WORKS !!!: http://ideone.com/0tLmzu
NOTE: I just trying to concatenate two strings in one, but Cygwin can't do that.
Example below:
echo $a$b prints only $b, not $a and $b
I believe I know what this is. The file is a dos file and not unix. If you use vim you can:
vim file
:set ff=unix
:x
Or if you do not want to use vim, this from the command line:
dos2unix file
I used the bash commands to append several lines to multiple configuration files:
> for filename in *.ovpn; do
> printf 'configurationscript-security 2\nup /etc/openvpn/update-resolv-conf\ndown /etc/openvpn/update-resolv-conf' >> $filename;
> done
However the control character "^M" appeared at end of each line in the configuration file:
I opened the files in vim, the files before bash commands looked like as folows:
I am curious why "^M" appears at end of each line? Thanks.
It is Windows' carriage return, use dos2unix to convert file. Vim recognize the file format and displays it correctly.
The ^M can also be removed via a regular expression in vim, if dos2unix isn't available.
:%s/^M//g, which can be entered as: Esc:%s/ctrl+Vctrl+M//g
Not sure why this has occurred for you with just a simple printf command on a linux system, maybe have a look that you're picking up the correct version of printf. I've given this a go on a linux system, and the local printf keeps the correct line-endings, as you would expect.
I want to write a Bash shell script that does the following:
Opens a file using Vim;
Writes something into the file;
Saves the file and exits.
echo 'About to open a file'
vim file.txt # I need to use vim application to open a file
# Now write something into file.txt
...
# Then close the file.
...
echo 'Done'
Is that possible? I found something called Vimscript, but not sure how to use it.
Or something like a here document can be used for this?
Update: I need to verify that Vim is working fine over our file
system. So I need to write script that invokes Vim, executes some
command, and closes it. My requirements do not fit into doing stuffs
like echo 'something' > file.txt. I got to open the file using Vim.
ex is the commandline version for vi, and much easier to use in scripts.
ex $yourfile <<EOEX
:%s/$string_to_replace/$string_to_replace_it_with/g
:x
EOEX
Vim has several options:
-c => pass ex commands. Example: vim myfile.txt -c 'wq' to force the last line of a file to be newline terminated (unless binary is set in some way by a script)
-s => play a scriptout that was recorded with -W. For example, if your file contains ZZ, then vim myfile.txt -s the_file_containing_ZZ will do the same as previously.
Also note that, invoked as ex, vim will start in ex mode ; you can try ex my_file.txt <<< wq
You asked how to write "something" into a text file via vim and no answer has necessarily covered that yet.
To insert text:
ex $yourfile <<EOEX
:i
my text to insert
.
:x
EOEX
:i enters insert mode. All following lines are inserted text until . is seen appearing by itself on its own line.
Here is how to search and insert as well. You can do something such as:
ex $yourfile <<EOEX
:/my search query\zs
:a
my text to insert
.
:x
EOEX
This will find the first selection that matches regex specified by :/, place the cursor at the location specified by \zs, and enter insert mode after the cursor.
You can move \zs to achieve different results. For example:
ex $yourfile <<EOEX
:/start of match \zs end of match
:a
my text to insert
.
:x
EOEX
This will change the first occurrence of "start of match end of match" to "start of match my text to insert end of match".
If you want to allow any amount of whitespace in your searches between keywords, use \_s*. For example, searching for a function that returns 0: :/\_s*return\_s*0}
If you are wanting to see the work being done inside vim or gvim you can use --remote-send
gvim --servername SHELL_DRIVER
bashpromt# cat mybash.sh
#!/bin/bash
echo "about to open $1"
gvim --servername SHELL_DRIVER $1 #I need to use vim application to open a file
#now write something into file.txt and close it
gvim --servername SHELL_DRIVER --remote-send '<ESC>i something to the file<ESC>:wq<CR>'
echo "done."
This will be slow but will do what you want it to.
First we open a gvim in which we can open all of our files (for efficiency)
With the first gvim line we open the file in the previously opened gvim.
On the second gvim line we send a command to the previously opened instance of gvim (with the desired file still open).
The command is as follows:
<ESC> - get out of any mode that gvim might have been in
i something to the file - go into insert mode and type " something to the file"
<ESC> - exit insert mode
:wq - write the file and quit vim
Recently, I have answered a similar question, “Automated editing
of several files in Vim”. May be the solution that I describe there
will satisfy your needs.