2022 Developer Survey is open! Take survey.

Questions tagged [string-manipulation]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
1 answer
128 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 ...
user avatar
  • 111
1 vote
1 answer
31 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 ...
user avatar
  • 326
0 votes
2 answers
37 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 ...
user avatar
  • 101
3 votes
2 answers
382 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. ...
user avatar
0 votes
1 answer
35 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 ...
user avatar
3 votes
1 answer
195 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]\...
user avatar
  • 133
3 votes
1 answer
330 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&...
user avatar
  • 1,562
0 votes
1 answer
58 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'] ...
user avatar
2 votes
1 answer
166 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 ...
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&...
user avatar
  • 27.4k
11 votes
1 answer
2k 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: :...
user avatar
  • 20.6k
1 vote
2 answers
146 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 ...
user avatar
2 votes
1 answer
196 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(...
user avatar
1 vote
3 answers
268 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 ...
user avatar
1 vote
0 answers
398 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 ...
user avatar
5 votes
2 answers
550 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) ...
user avatar
  • 8,510
1 vote
4 answers
593 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 (...
user avatar
  • 63
17 votes
2 answers
4k 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 ...
user avatar
2 votes
1 answer
2k 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 ...
user avatar
  • 407
2 votes
2 answers
581 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 ...
user avatar
  • 123
1 vote
2 answers
288 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 = ...
user avatar
1 vote
1 answer
138 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"...
user avatar
  • 143
2 votes
1 answer
2k 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 ...
user avatar
  • 21
10 votes
1 answer
4k 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?
user avatar
  • 881
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?
user avatar
  • 417
1 vote
1 answer
3k 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>...
user avatar
0 votes
1 answer
126 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&...
user avatar
  • 825
7 votes
1 answer
4k 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 ...
user avatar
22 votes
1 answer
3k 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 . '!'
user avatar
  • 2,221
4 votes
1 answer
333 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 ::...
user avatar
  • 143
2 votes
1 answer
66 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 ...
user avatar
  • 1,237
2 votes
2 answers
190 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 ...
user avatar
4 votes
3 answers
562 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 ...
user avatar
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 ...
user avatar
15 votes
2 answers
7k 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, ...
user avatar
  • 781
12 votes
3 answers
4k 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 ...
user avatar