Can I calculate the length of each line in vim? - linux

I have a file below.
My
first
name
is
Kim.
And I wanna find the way(vim plugin, regular expression or vim function) that make this file below.
My 2
first 5
name 4
is 2
Kim. 4
Is there any method to calculate the length of each line in vim?
I don't want to achieve this via perl/ruby script or script development.

One way to do it:
:%s/\v(.*)\zs/\="\t".strlen(submatch(1))/
To understand how is this supposed to work: :help \v, :help \zs, :help s/\=, :help strlen(), and :help submatch().
Depending on what you use this for, if you work with wide characters you might need to replace strlen() with strwidth() or strdisplaywidth(). They might make more sense as "line length".

Related

Is there any way to enclose a variable with print() statement or any function in vim especially using vim vs code extension

suppose I have a variable named
a_variable
is there a surround or some combination of keystrokes which will do
print(a_variable)
or
print("a_variable: ", a_variable)
when my cursor in on the line where a_variable is defined ?
I could not find any relevant material on the web .
Any help is appreciated.
This answers is written with Vim in mind. Some, all, or none of it may apply to your specific Vim emulator so YMMV.
With the surround plugin
First case
You can position your cursor on a_variable and do:
ysiwfprint(<CR>
to obtain:
print(a_variable)
Second case
It is currently impossible to achieve with surround only.
Without surround
First case
You can position your cursor on a_variable and do:
ciwprint(<C-r>")<Esc>
Second case
The scond case is a variant of the first case where you insert the variable name two times instead of one:
ciwprint("<C-r>": ", <C-r>")<Esc>
Turn it into a mapping if you need to do it often.
See :help c, :help iw, :help i_ctrl-r, :help "".

the difference between : and / in s/vi/VIM/g and s:^vi$:VIM:

I read vim regex example
s/vi/VIM/g
and
s:^vi$:VIM:
What' the difference between / and :,
Search but found few helpful materials.
Vim lets you change the character you use to start and end the search pattern arbitrarily. This is useful if you're going to have to escape the slash a lot in a particular expression.
For example, these two commands are equivalent:
s/\/\//ss/g
s://:ss:g
but the second one is much easier to type and read.
The two :s commands are different.
s/vi/VIM/g replace all vi by VIM no matter where vi is and how many times it occurred.
However, s:^vi$:VIM: replace lines containing only vi two characters by line: VIM
Regarding the / and : they are just separators of :s command. They make no difference in your command. If you want to read this part explanation, do :h E146 in your vim.

What is the meaning of the % character in vim?

The vim wikia page provides the following description for search and replace:
:%s/foo/bar/g
Find each occurrence of 'foo' (in all lines), and replace it with 'bar'.
:s/foo/bar/g
Find each occurrence of 'foo' (in the current line only), and replace it with 'bar'.
...
I can see that the the % character causes the whole buffer to be searched.
What is the meaning of the % character in vim? Is it a variable that refers to the current buffer?
Learn how to look up commands and navigate the built-in :help; it is comprehensive and offers many tips. You won't learn Vim as fast as other editors, but if you commit to continuous learning, it'll prove a very powerful and efficient editor.
Here's how you would have found the information:
Look up the command: :help :substitute
:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
Ah, the stuff in front is called range. Further down, there's a link to it:
Also see |cmdline-ranges|.
(:help [range] would have also taken you to it.) And that explains the meaning of %, as well as the help keyword for direct access.
% equal to 1,$ (the entire file) *:%*

How to replace current string line with another line in vim?

I'd like to replace current string line with another (for example the another line is placed in 5 lines above current line). I can do it with a pair of commands
dd
:-5t-1
Is there the shorter way to obtain same goal?
dd
:-5t-1
is already pretty short if you ask me. But you can squeeze everything into a one-liner:
:d|-5t-1
and remove the 1 because it's implied by -:
:d|-5t-
Barring making a custom command or mapping I don't see how you could make it shorter.
:-5y<CR>Vp
is it shorter?
if you need do that really often, add this into your vimrc:
command! -range R d|<line1>,<line2>t-
then you can just do :-5R replace current line with -5 line
or 2,4R to cp line 2-4 (3 lines) to current line, and replace current line.
If you don't mind a plugin, my LineJuggler plugin offers a ]r command (and many more):
]r Fetch the line [count] visible lines above the current line and replace the current line with it.
With it, your example would be the short and easy 5]r
In addition, the companion LineJugglerCommands plugin now offers a similar :Replace Ex command. Again, your example would be
:Replace -5

How to specify the line under cursor in Vim?

I want to count number of occurrences of the line under the cursor. I intend to do it with the
:%s/pattern/&/gn
command. So how do I specify the line under cursor in place of pattern?
You can insert the current line via the expression register. For a literal match, switch the regular expression to very nomagic mode (\V), and escape any backslashes and the separator in the line:
:%s/\V<C-r>=escape(getline('.'), '/\')<CR>/&/gn
Depending on what you want to count exactly, you may also need to anchor (\^...\$ in very nomagic mode) the pattern.
Instead of direct insertion via <C-r>, you can also build the command via :execute. This is more suitable in a function.
:execute '%s/\V' . escape(getline('.'), '/\') . '/&/gn'
If you're looking for a canned plugin solution, my SearchPosition plugin can count occurrences. With it, V<A-m> will show a summary like this:
On sole match in this line, 8 following, 2 in previous lines; total 10 for /this line\n/

Resources