Getting the current row number?

ViVim

Vi Problem Overview


Is there any key mapping that outputs the current row number of the line being edited? Or, even better yet, can we do formulas based on the output of the key mapping?

I want to get the row number and add 1 to the current text being edited.

Vi Solutions


Solution 1 - Vi

What do you mean by "output"? You can do:

:echo line(".") + 1

To display the current line number plus 1. You can bind a keystroke with map, eg:

:noremap <F1> :echo line(".") + 1<cr>

To actually insert the data into the buffer:

:noremap <F1> :execute "normal! i" . ( line(".") + 1 )<cr>

Solution 2 - Vi

Ctrl+G will tell you the line number and even the column the cursor is in. If you mean output it as text to your document, then not that I know of.

Solution 3 - Vi

  1. Use :set ruler. (Works only in vim) Reference

It shows the current line and column of the line being edited (line where the cursor lies), at the bottom right corner of the widow.

1,1 <position>

  1. If first line is edited, position is Top.
  2. If last line is edited, position is Bot.
  3. If no scroll is available (both start and end lines are visible), position is All
  4. If no first and last lines are visible, position is the percentage of the document visible.

To make it permanent, add set ruler in ~/.vimrc file (if file is not there, create one).

  1. Use :set number. (Works in both vi and vim) Reference

Displays the line number before every line.

Solution 4 - Vi

The answer was given by @ZyX in a comment to another answer:

> You have <C-r> in insert mode for this kind of things: > >nnoremap <F1> i<C-r>=line('.')+1<CR><Esc>

Solution 5 - Vi

You can get the visual width of the current cursor position with virtcol('.'); insert that into the buffer through the expression register, e.g. in insert mode with <C-R>=virtcol('.')<CR>

Note that the width is different from the number of characters when the line contains double-width characters, <Tab> or unprintable characters.

Solution 6 - Vi

not really related to original problem, but for me, to see a line number I prefer use :#, it will print out the line number and content as well

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpacvView Question on Stackoverflow
Solution 1 - ViWilliam PursellView Answer on Stackoverflow
Solution 2 - ViEricView Answer on Stackoverflow
Solution 3 - ViVignesh RajaView Answer on Stackoverflow
Solution 4 - VipacvView Answer on Stackoverflow
Solution 5 - ViIngo KarkatView Answer on Stackoverflow
Solution 6 - VilehanhView Answer on Stackoverflow