insert n characters before pattern - vim

I have a text file where I want to insert 20 spaces before the string 'LABEL'. I'd like to do this in vim.
I was hoping something like s/LABEL/ {20}LABEL/ would work. It doesn't.
This SO question is close to what I want to do, but I can't put 'LABEL' after the '=repeat()'. Vim regex replace with n characters
%s/LABEL/\=repeat(' ',20)/g works.
%s/LABEL/\=repeat(' ',20)LABEL/g gives me E15: Invalid expression: repeat(' ',20)LABEL
How do I get vim to evaluate =repeat() but not =repeat()LABEL?

After \=, a string is expect. And LABEL isn't a valid string
%s/LABEL/\=repeat(' ',20).'LABEL'/g
BTW thanks to \ze, you don't need to repeat what is searched.
%s/\zeLABEL/\=repeat(' ',20)/g
Note that if you need to align various stuff, you could use printf() instead
%s#label1\|other label#\=printf('%20s', submatch(0))#

Related

Detect if presence of a character in a string with Lua

I can have two types of string: nxs_flo_dev.nexus orfpdesk.
I want to test if there is a . in the string.
If there is a . I divide the string otherwise I do not do anything.
if(ngx.var.host.contains('.') then
content_by_lua 'ngx.say(ngx.var.host:match("(.-)%."))';
end
Is there a function to do that? Because .contains() doesn't work.
Use match again. Remember to escape the dot with %.:
if ngx.var.host:match('%.') then
If you want to do this inside content_by_lua do
content_by_lua 'if ngx.var.host:match('%.') then ngx.say(ngx.var.host:match("(.-)%.")) end';
Given your edit, this is the simplest solution:
content_by_lua 'ngx.say(ngx.var.host:match("(.-)%.") or ngx.var.host)';

Reading from a string using sscanf in Matlab

I'm trying to read a string in a specific format
RealSociedad
this is one example of string and what I want to extract is the name of the team.
I've tried something like this,
houseteam = sscanf(str, '%s');
but it does not work, why?
You can use regexprep like you did in your post above to do this for you. Even though your post says to use sscanf and from the comments in your post, you'd like to see this done using regexprep. You would have to do this using two nested regexprep calls, and you can retrieve the team name (i.e. RealSociedad) like so, given that str is in the format that you have provided:
str = 'RealSociedad';
houseteam = regexprep(regexprep(str, '^<a(.*)">', ''), '</a>$', '')
This looks very intimidating, but let's break this up. First, look at this statement:
regexprep(str, '^<a(.*)">', '')
How regexprep works is you specify the string you want to analyze, the pattern you are searching for, then what you want to replace this pattern with. The pattern we are looking for is:
^<a(.*)">
This says you are looking for patterns where the beginning of the string starts with a a<. After this, the (.*)"> is performing a greedy evaluation. This is saying that we want to find the longest sequence of characters until we reach the characters of ">. As such, what the regular expression will match is the following string:
<ahref="/teams/spain/real-sociedad-de-futbol/2028/">
We then replace this with a blank string. As such, the output of the first regexprep call will be this:
RealSociedad</a>
We want to get rid of the </a> string, and so we would make another regexprep call where we look for the </a> at the end of the string, then replace this with the blank string yet again. The pattern you are looking for is thus:
</a>$
The dollar sign ($) symbolizes that this pattern should appear at the end of the string. If we find such a pattern, we will replace it with the blank string. Therefore, what we get in the end is:
RealSociedad
Found a solution. So, %s stops when it finds a space.
str = regexprep(str, '<', ' <');
str = regexprep(str, '>', '> ');
houseteam = sscanf(str, '%*s %s %*s');
This will create a space between my desired string.

vim macro replacing \n with ','

I am trying to save a macro which replaces \n with ,
Input:
978818
978818
900298
900272
Output:
'978818','978818','900298','900272'
When I saved the macro using CTRL+R CTRL+R,B in vimrc it looks like below:
let #b = ":%s/\n/','/g^MI'^[A~#kb~#kb^["
But now when I run this macro it give the output as:
978818978818900298900272
and error:
E486: Pattern not found: ','
Don't know why it is trying to match ,
You probably need to escape the \n. vim thinks you want a new line character at that point in the string and replaces it with a literal new line. So the fixed macro should be.
let #b = ":%s/\\n/','/g^MI'^[A~#kb~#kb^["
Edit: If you want something that you can copy and paste-able I believe the macro below is equivalent to what you want.
let #b = ":%s/\\n/','/g\nI'\e$xx"

how to replace string with result of function in Vim?

I want to insert filename and line number into some places in the file. For example this line:
_debug('init');
I want to replace
:s/debug('/debug('(%current_filename_here%:%current_line_number_here%)\ /g
to get this
_debug('(filename.ext:88) init');
I try to use expand('%:t') to get filename and line(".") to get line number, but I don't know how to use it in replace expression.
How can I do this?
You can use \=. For example:
:s#_debug('\zs#\=printf('(%s:%d) ', expand('%:t'), line('.'))#
When the {replacement} starts with "\=" it is evaluated as an expression,

Add a number of '=' in a rest (reStructuredText) document that equals to characters from last line?

I want to use a shortcut to add needed = (from Section/Title reStructuredText syntax) according to the last line.
So, suppose (being | the cursor position)
Title
|
and pressing an specific mapping mapped to a function, add a number of = that equals to the last line (where Title is), becoming:
Title
=====|
This sequence will get you close:
kyyp:.s/./=/g
Duplicate the previous line, then in that line, change every character to an equals sign. Map that to a key sequence you like, and try it out.
Another way:
:execute "normal " . strlen(getline(line(".") - 1)) . "i="
strlen(getline(line(".") - 1)) returns the lenght of the line above the current position. The result is that the command Ni= is executed, inserting = N times.
For a mapping I would have used:
put=repeat('=', col('$')-1)
For something more interactive, I would have use the same solution as Ned's.
(I don't like my mappings to change the various registers like #" or #/)
My vim-rst-sections vim plugin will convert lines to section headings:
http://www.vim.org/scripts/script.php?script_id=4486
In your case, you'd put the cursor on the line, and type <leader><leader>d to get a top-level heading like this:
#####
Title
#####
A few repeats of <leader><leader>d will take you down to the standard hierarchy of Python ReST sections to the =.

Resources