13

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 4

14

. (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.

3
  • This does not work. When I give a command :!echo . The output is just a "." instead of the line number expected.
    – akshat
    Commented 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.
    – WMR
    Commented 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.
    – akshat
    Commented Nov 6, 2008 at 16:20
11

If you want to pass the current line number to a shell command, you could do

:exe "!echo " . line(".")
2
  • 1
    Hi, That is an awesome answer. exactly what I was looking for.
    – akshat
    Commented Nov 22, 2008 at 7:26
  • Thanks for the missing ingredient for my line and include the output :exe "r!" . getline(".")
    – bsb
    Commented Aug 9, 2013 at 0:21
2

To return the line number of current line at bottom of screen, use:

:.=       
1

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.

3
  • 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.
    – Tomalak
    Commented Nov 6, 2008 at 7:35
  • 1
    To do it globally, you need to put :%s/foo/bar/g OR :1,$s/foo/bar/g
    – Vijay Dev
    Commented Nov 6, 2008 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.