In VIM in command line mode a "%" denotes the current file, "cword" denotes the current word under the cursor. I want to create a shortcut where I need the current line number. What is the symbol which denotes this?
4 Answers
. (dot) stands for the current line.
To clarify:
This is meant for stuff like :1,.s/foo/bar/g
which will transform every foo
to bar
from the beginning of the file up to the current line.
I don't know know of a way to get the current line number expanded for a shell command, which is what you are trying to do by doing :!echo .
You can find out about the expansions that are done (like %
and #
for example) in :he cmdline-special
.
-
This does not work. When I give a command :!echo . The output is just a "." instead of the line number expected.– akshatCommented Nov 6, 2008 at 7:30
-
No, this does not work, see my clarification. Maybe if you ask another question where you explain your planned shortcut in detail we can help you with another way to achieve what you intending.– WMRCommented Nov 6, 2008 at 8:39
-
thanks WMR. looking back at my question I realize that I did not correctly explain my requirement. PS: I had looked at cmdline-special but did not find this. I was hoping there was something not mentioned.– akshatCommented Nov 6, 2008 at 16:20
If you want to pass the current line number to a shell command, you could do
:exe "!echo " . line(".")
Commands in vim works on the current line so:
:s/foo/bar/g
will transform every foo in bar on the line you are currently on.
-
I think the 'g' will cause it to substitute globally throughout the entire file. Commented Nov 6, 2008 at 7:10
-
No, in this case the 'g' will cause the regex to be applied globally to the current line.– TomalakCommented Nov 6, 2008 at 7:35
-
1To do it globally, you need to put :%s/foo/bar/g OR :1,$s/foo/bar/g Commented Nov 6, 2008 at 7:44