Skip to main content

Questions tagged [string-manipulation]

Filter by
Sorted by
Tagged with
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 ...
illdo's user avatar
  • 1
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 ...
Barzi2001's user avatar
  • 808
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 ...
Idervas's user avatar
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 '!...
Jin's user avatar
  • 185
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 . &...
markling's user avatar
  • 409
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/...
Al-Baraa El-Hag's user avatar
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 ... ...
Fisher's user avatar
  • 1,096
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 ...
홍한석's user avatar
  • 111
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 ...
Phil R's user avatar
  • 525
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 ...
Alexanus's user avatar
  • 119
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. ...
awvalenti's user avatar
  • 303
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 ...
MrAdityaAlok's user avatar
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]\...
Larara's user avatar
  • 133
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&...
Enlico's user avatar
  • 2,276
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'] ...
Hakan Baba's user avatar
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 ...
caffeinemachine's user avatar
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&...
Rich's user avatar
  • 32.6k
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: :...
D. Ben Knoble's user avatar
  • 27.7k
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 ...
millenito's user avatar
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(...
ColinKennedy's user avatar
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 ...
Karolis Koncevičius's user avatar
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 ...
merlin2011's user avatar
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) ...
Ralf's user avatar
  • 9,417
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 (...
Inian's user avatar
  • 63
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 ...
SalmonKiller's user avatar
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 ...
SLN's user avatar
  • 557
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 ...
user8162's user avatar
  • 123
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 = ...
codingCoffee's user avatar
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"...
igorepst's user avatar
  • 143
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 ...
cute's user avatar
  • 21
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?
qeatzy's user avatar
  • 968
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?
lindhe's user avatar
  • 487
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>...
Evan Carroll's user avatar
  • 1,543
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&...
Anon's user avatar
  • 905
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 ...
René Nyffenegger's user avatar
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 . '!'
Tom Hale's user avatar
  • 2,731
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 ::...
PerroNoob's user avatar
  • 143
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 ...
Luke Davis's user avatar
  • 1,407
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 ...
Karl Bielefeldt's user avatar
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 ...
d.k's user avatar
  • 774
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 ...
Zach Kirsch's user avatar
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, ...
tamlok's user avatar
  • 851
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 ...
René Nyffenegger's user avatar