Questions tagged [string-manipulation]
The string-manipulation tag has no usage guidance.
43 questions
0
votes
1
answer
29
views
How to automate console.log random string with word (or selection) under cursor?
I am using this code snippet for adding console.log:
imap cll console.log()<Esc><S-f>(a
" Console log from visual mode on next line, puts visual selection inside parentheses
vmap cll ...
2
votes
1
answer
179
views
How to split strings that contain ^M^@ characters?
I am working with gvim on WSL2 on Windows 10 and I receive strings of the form:
some_text^M^@some_other_text^M^@more_text
when I use split(message, '\r'), where message is a variable containing the ...
0
votes
1
answer
167
views
Is there any invisible string character that can be ignored by "set fillchars", in the status line?
Currently, I'm trying to make a pretty minimal custom status line that suits my needs. One element of it, however, is a diagnostics report that depends on another plugin (coc.nvim). With a function, I ...
2
votes
0
answers
69
views
How do I concatenate these strings? [closed]
I have two string variables:
let url = "https://URL/path/to/repo"
let branch = "GIT-HASH"
I want to concatentate them and echo it on the shell:
let result = url . branch
exec '!...
1
vote
1
answer
27
views
Inconsistent string handling [duplicate]
I've been assembling strings in vimscript using a . as the join command.
But when I try and use the same command to join a string when setting an option, such as:
:setlocal foldtext=v:folddashes . &...
0
votes
2
answers
609
views
How to remove 2 characters from the beginning of a string after using filename-modifiers?
How to remove 2 characters from the beginning of a string after using filename-modifiers?
So assume that I have a files that looks like this:
/home/drskoolie/projects/examples/ex40.c
/home/drskoolie/...
1
vote
2
answers
934
views
How to remove underscore and capitalize the next character in the first word?
I want to modify the first word of each line: remove underscore character and capitalize the next character.
Input:
word_example_one other_words ...
word_example_two other_words ...
...
0
votes
1
answer
142
views
Escape `^@` in a variable
Goal: print git diffs of pwd in a popup
let diff = system('git diff')
call popup_create(diff, {})
Problem: It prints ^@(I've got to know that is newline chars):
diff --git a/.config/fish/config.fish ...
1
vote
1
answer
81
views
Best Way to Strip Most Significant Digit from a Number
My goal is, given a positive integer, write a function to remove the leading digit and return the number given by the remaining digits. I have two ways of doing it presented here, and I'd like the ...
0
votes
2
answers
90
views
How do I convert a file mark to a string?
If I for example create a mark inside the file ~/.vimrc with mV.
How do I convert the mark 'V to the path of the file the mark is in?
When I type :marks I can see a list of marks and the corresponding ...
8
votes
2
answers
1k
views
On string concatenation, what's the difference between . and .. (single dot and double dot)?
From the :execute help (:h :exe):
*:exe* *:execute*
:exe[cute] {expr1} .. Executes the string that results from the evaluation
of {expr1} as an Ex command.
...
0
votes
1
answer
60
views
Why are these strings not equal?
let os = system('uname -o')
function Strcmp(str1, str2)
if a:str1 < a:str2
return -1
elseif a:str1 == a:str2
return 0
else
return 1
endif
endfunction
echo ...
3
votes
1
answer
199
views
Unexpected substitution behavior
I need to put a comma between any two digits in a string of digits. For example, I want this
12345678910
to become this
1,2,3,4,5,6,7,8,9,1,0
The approach I took was the following
s/\([0-9]\)\([0-9]\...
3
votes
1
answer
709
views
Are opening and closing double quotes expected to be of different color in a bash script?
I've just noticed, after years using Vim, that the two double quote characters opening and closing a string in a bash script have different colors, which I can verify by simply writing var="var&...
0
votes
1
answer
1k
views
How to split a string that is a full path, into its directories?
Say I have a string for the full path of a directory
let myPath = '/go/src/github.com/MyDomain/MyProject'
I would like to have an array like
['go', 'src', 'github.com', 'MyDomain', 'MyProject']
...
3
votes
1
answer
371
views
How to store a region of text into a variable?
Suppose I have \label{some text} somewhere in my file and I want to store some text into a variable.
Right now what I am doing to achieve this is to first visually select some text and then store it ...
7
votes
1
answer
1k
views
What is the difference between <C-V>x and \x?
Run the following commands, where <80> is a literal byte entered by typing Ctrl-Vx80:
let literal = "<80>"
let quoted = "\x80"
echo(literal) and echo(quoted) will now both output <80&...
12
votes
1
answer
3k
views
How do I use a variable or return value in option, command, or mapping?
I have a variable foo with let foo='value', and a function F like
function F()
return 'value'
endfunction
and I want to use them
in an option: :set option=foo, :set option=F(); or
in a command: :...
1
vote
2
answers
210
views
Insert text in the middle of a string vimscript
I'm trying to make a function in vimscript that converts markdown to html files using pandoc and putting that converted file in a specified directory.
Currently i am putting the file in a directory ...
2
votes
1
answer
311
views
How to assign a Python list to a vim variable and escape its strings correctly
Here is a Python list that I'm trying to assign to some Vim variable.
lines = ['"""Something foo\'s thing."""', '', 'foo = "asdfasdf" \\', 'bar']
command = 'let l:corrected_lines = {lines}'.format(...
1
vote
3
answers
369
views
Command to copy from current window and paste the text into terminal buffer
I am having trouble crafting a command that would copy the text from the current window and paste this text into a terminal buffer opened in the split below.
Here are a few things I tried:
normal ...
2
votes
0
answers
664
views
What is the correct character for splitting a string on line breaks in Vimscript?
Consider the following text file.
line1f line1n
line2
Suppose I visual select this, yank it into register a, and then pass it to the following vimscript function:
function TestSplit(block)
for x ...
5
votes
2
answers
1k
views
Reliably get the character at a byte index in a string
I'm doing some string processing in vimscript. I obtained a byte index for a character in the string and now want to obtain the character at that byte index.
For single-byte charsets (like latin1) ...
1
vote
4
answers
933
views
In Vim ex mode how to re-use a string from the default buffer in the command
I'm trying to modify this file, by extracting the very first line in the file and prepend it to each of the consecutive lines until end of the file
black
widow
card
Friday
berry
I used ex command (...
21
votes
2
answers
6k
views
How to check if a string is a substring of another string
I was wondering what is the proper way to test whether a substring is included in some other string. If I want to check if string a is part of string b, I'm currently using
matchstr(b, a) != ""
to ...
7
votes
2
answers
8k
views
How to generate a string by concatenating another string with an environment variable
I need to create a variable with a fool folder direct which need to concatenate from a environment variable and the end notes
function! MyFoo(arg)
let repo=$Project + "/cfora" #Q1 ...
2
votes
2
answers
803
views
How to autoformat a multi-line string
I want to format a multiline string according to a textwidth of 32.
group:
- normal: scalar
- multiline: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor ...
1
vote
2
answers
391
views
String comparison not working as expected!
So I want to figure out the Operating System and execute a piece of code based on the operating system in my .vimrc. However, I'm not able to understand where I'm going wrong.
let operating_system = ...
1
vote
1
answer
215
views
Get <Leader> value as <Space> and not ' '
Given that I defined let mapleader="\<Space>", I would like to get in the vimscript the value of the leader as <Space> and not as ' '. The latter may be obtained by using get(g:,"mapleader"...
2
votes
1
answer
3k
views
How can I extract text between two parts in a single line in Python? [closed]
Let's say the lines in words.txt look like this, for example:
Cute Stack 10
Dog Fire 384
Phone Real 485
I want to check if the middle text in each line (stack, fire, real) is equal to a ...
12
votes
1
answer
5k
views
vimscript: how to repeat a string N times?
I know . operator concatenate strings.
But how to repeat a string N times, similar to "abc" * 3 in python?
4
votes
4
answers
2k
views
How to concatenate two registers?
Is there some built in functionality in Vim to merge the contents of two registers into a single register by appending the two?
3
votes
1
answer
4k
views
What is the vimscript version of sprintf?
I'm trying to create a dynamic line to execute, currently I have
execute a:modes[i] .'noremap <silent> <buffer> '. l:key .' <Esc>:call HardModeEcho(g:HardMode_hardmodeMsg)<CR>...
0
votes
1
answer
147
views
How do you sort lines by longest substring to shortest substring?
Here are some sample strings, sorted by longest string to shortest sting:
"Bi(NO<sub>3</sub>)<sub>3</sub>·5H<sub>2</sub>O","bismuth nitrate pentahydrate");
"Be(C&...
11
votes
1
answer
5k
views
How do I alphabetically compare two strings
Is there a standard way to compare two strings in vim so that I can quickly determine which string is sorted before the other.
Something like
strcmp(str1, str2)
to return 0 if str1 == str2, 1 if ...
26
votes
2
answers
4k
views
What is the difference between single and double quoted strings?
What is the difference between single and double quoted strings?
Is there a way to get a variable expanded inside a string, or do they always need to be joined like: 'Hello, ' . s:name . '!'
4
votes
1
answer
384
views
Align code functions documentation
Is there an easy way to align a function documentation (I'm using Python) from this:
arg1 :: Some text
arg2345 :: Some very long text text text text
arg3:: Other text
to something like:
arg1 ::...
2
votes
1
answer
69
views
Running find-and-replace in Visual Mode :'<,'>s/^/string/g inserts "string" at 0 position instead of ^ (first non-whitespace) position
I have some visual-mode commenting macros that should place a comment character g:CommentChar before the first non-whitespace character of each line in the selection. However, it places it right of ...
2
votes
2
answers
246
views
How to extract part of the current working directory to the statusline?
I have a lot of projects of the form:
/home/karl/source/project-name/further/hierarchy/...
I would like to extract just the project-name to put into my statusline. I use autochdir, so I know I can ...
5
votes
3
answers
590
views
How can a character be inserted during an edit session by its Unicode numeric value?
I'm trying to set up a plugin to enable indentation guides, following an answer to this question. The plugin is Yggdroot/indentLine. It has instructions to set up a custom glyph to be shown instead of ...
6
votes
0
answers
39
views
Expand strings into multiline strings on line wrap [duplicate]
This is a feature in Eclipse that I would love to find a solution for in Vim.
I have Vim set up with tw=80 so that the lines wrap at 80 columns. When I'm in the middle of a string, and the line ...
17
votes
2
answers
8k
views
How to deal with string containing spaces in Vim script?
I have the following command in my vimrc:
execute "set path+=".getcwd()."/**"
It works well if current working directory is like /home/tamlok/my_work/. However, if the directory contains spaces, ...
15
votes
3
answers
7k
views
How do I reverse a string with Vim script
I am trying to reverse a string, so that 'abc defg hij' becomes 'jih gfed cba'. The solution I came up with is
join(reverse(split(l:text, '.\zs')), '')
Is there a more elegant solution or should I ...