New answers tagged

0

Putting set cpoptions=aABceF At the end of my .vimrc worked (instead of at the start).


1

It seems you're under the impression that the 'commentstring' option can be used to set which character or format is recognized as a comment in Vimscript... But that's not at all what it is about! It's about telling Vim which format is recognized as a comment in the current language (as in, the current filetype), so that if Vim needs to insert a comment ...


2

You can't just start lines with * in your vimrc and have Vim ignore them. * is not really a comment character, it's a valid command. Under 'nocompatible' (which is most surely the mode under which everyone is using Vim these days), :* is a shortcut to :'<,'>, which is the range of lines comprising the last Visual selection. (See :help cpo-*, keeping in ...


2

I've had some success by creating the popup window without focus: let m = range(50) \ ->map({_, i -> string(i)}) \ ->popup_create(#{ \ line: 1, \ col: 1, \ minwidth: 1, \ minheight: 1, \ maxheight: &lines - 1, \ cursorline: 1, \ wrap: 0, \ firstline: 1, \ scrollbar: ...


2

I tried using windo set scrollbind, but this doesn't work on a popup window (as far as i know). It cannot work because :windo only works on windows which have a number; popup windows don't. Any ways to do this? OTOH, popup windows do have an ID. So, you could try: call win_execute(winid, 'setlocal scrollbind') Or: call setwinvar(winid, '&scrollbind',...


2

Try to limit the height of the popup, at least 1 line less than the height of the terminal window, by including this key in the options dictionary passed to popup_create(): maxheight: &lines - 1 For example: call range(50) \ ->map({_, i -> string(i)}) \ ->popup_create(#{ \ line: 1, \ col: 1, \ minwidth: 1, \ ...


1

Surround has a philosophy of being very « vim-like »—it uses operators to work on objects, much like many of vim’s most powerful normal mode constructs. I would argue that it is not what some expect because they expect an auto-pairs-like experience that they are used to from other editors, or because they are still learning about normal-mode and the many ...


1

There are two approaches to detect weither we are within an empty pair. The simple one: getline('.')[col('.')-2 : col('.')] == '()' that's simple to understand. To support more pairs we can test a regex instead: getline('.')[col('.')-2 : col('.')] =~ '\V()\\|[]\\|{}\\|<>\\|$$' The one that supports multicharacters pairs like <del></del>. ...


2

Scroll-binding is definitely an option, but there are some downsides to it in that you might create interference with the use of scrollbind elsewhere, for instance if you're also using a diff at the same time... From previous questions, I understand your use case is to use custom line numbering covering specific blocks of your buffer, and you're considering ...


2

With the cursor in or on the parentheses, you can use Ctrl+o combined with dab to delete the parentheses and any text contained within. Ctrl+o lets you execute one normal mode command, then return to insert mode. dab deletes a block text object, including ( and ). This also works with an empty block (). Example: Say I am in insert mode with the following ...


3

Yes. Choose one: :windo se scb :windo set scrollbind This sets scrollbind for every window. :help scrollbind: See also |scroll-binding|. When this option is set, the current window scrolls as other scrollbind windows (windows that also have this option set) scroll.


1

TL;DR: Use :source % instead. Local functions and variables are indeed a limitation of the :@ approach. As I actually mentioned in my answer to your previous question: There are some limitations, such as you can't define local functions and variables (in the s: namespace.) If you want to be able to only load parts of it using the :@ command, then you ...


1

The :@ command, to run a set of Ex commands, is the most direct one to run a block of Vimscript directly. There are some limitations, such as you can't define local functions and variables (in the s: namespace.) Until recently, Vim would break a :@ if line-continuations starting with backslash were used, but that was fixed in version 8.2.0997. I think my ...


1

That code is using operator =~ in the 'v:val =~# a:filename' expression and =~ is trying to evaluate a:filename as a pattern (regex) to match it against the values in the list. See :help expr-=~. On the other hand, your a:filename includes the character ~ (twice) and that character is special in a Vim regex: it references the last replacement string used in ...


4

From :h notation: Curly braces denote parts of the command which must appear, but which can take a number of different values. So, a placeholder.


2

Version 8.2.935 introduced a flatten() function (and version 8.2.937 fixed a bug in that function). With this function you can also write let s:asciiart = flatten(["", "", systemlist("command params"), "", ""]) to insert the elements of the inner list into the outer list.


3

com! -nargs=+ PS let @/ = printf('\v%s(\W+\w+){,%d}\W+%s', <f-args>) The way this is intended to work is to act as if you entered, after hitting /, the same search term that was provided in the answer to your question. So when you run the command you just need to use n and N to go forward and back to any matches. The / register always contains the ...


2

I think that index() would be the most efficient way for a strict equality. There is match() when pattern matching is required IIRC. As Christian said, :h list-functions is a perfect place to start. Still many list related functions are missing. Over the years I've defined the one I'm missing in my library plugin: https://github.com/LucHermitte/lh-vim-lib/...


3

In an extremely related Q/A, I've listed all the tools I provide and use to help tracking errors in vim scripts: https://vi.stackexchange.com/a/25863/626 They come in complement of :debug.


1

It's fairly difficult to trigger such a mapping. The problem with norm <SID>MyMapping is that the :normal command doesn't really recognize <...> sequences, so it ends up executing the <, S, I, D, >, etc. keystrokes literally. The sequence <S fails because S is not a valid motion or text object, so the whole sequence ends up doing nothing....


1

Vim is written in C language. According to Wikipedia, Vim is written in both C and Vimscript, not only Vimscript. The source code of Vim is available here, on Github. The two biggest folders are /src: 26.321M, mostly written in C. Most of the files are .c and .h in the folder itself (not in sub folders), but there is also some .po files containing stuff like ...


1

By throwing an exception you should be able to obtain the full (functions) callstack. I've described part the process in a section of the documentation of my library plugin along with the related functions I provide to decode v:throwpoint. IIRC, there is another Q/A where I delved more in details about how its works... Found it! And also here. There are a ...


2

I have similar problem so I have developed and posted a plugin (https://github.com/rickhowe/partialnumber.vim). In your example, :g/^##.##\n\ze[^#]/+1,/[^#]\zs\n##.##$/ SetPNU would show numbers in sign column.


2

You cannot. From :h 'number' (emphasis mine): Print the line number in front of each line You can only enable number or relativenumber for the whole window. See: :help 'number' :help 'relativenumber' Edit: In Vim, this isn't feasible because 'signcolumn' has a max width of 2 characters. For some reason, even though Neovim supports larger sign columns, ...


2

There are a few plugins that have started to use {name}://{whatever} to name scratch buffers (e.g. tags://patternsearched, tasks://..., ). This is kind of helpful when other plugins try to do things on buffers but wish to ignore scratch buffer. A typical example is a local-vimrc plugin that applies project configuration settings on every buffer under a ...


1

If you look at the help for the :pedit command, you will see this: :ped :pedit :ped[it][!] [++opt] [+cmd] {file} Edit {file} in the preview window. The preview window is opened like with :ptag. The current window and cursor position isn't changed. Useful example: ...


1

Shift+v simply produces an uppercase V, so that's what you typically pass to Vim. Whether it is to create a mapping for that keystroke: :nnoremap V :echo "Hello World!"<CR> Or to use :normal! to simulate that keystroke to start a visual line selection: :normal! V The <S- prefix does exist, but it's generally only used with special non-...


2

The problem with [Scratch] itself is that that's not really the name of a buffer, but just Vim telling you that this scratch buffer is actually unnamed... It's much easier if you start with a named buffer and use bufadd() to create it. For example: let bufnr = bufadd('MyFancyScratchBuffer') execute 'sb' bufnr setlocal buftype=nofile bufhidden=hide noswapfile ...


2

You still need the execute, in order to interpolate the contents of the result Vimscript variable into the shell command: execute 'read !grep "'.result.'" file' Note that now this is a shell command and no longer just a Vim command, so consider using shellescape() to properly escape the string for use in a shell command. (But note that you may ...


3

You need to add a literal newline (displayed as ^M). You can type this character into your _vimrc by holding down Ctrl and typing v followed by m, and then releasing Ctrl. So for each line you would have: normal i __ __ __^M normal i / | / |/ |^M " etc... where ^M is not the characters "^M" literally, but the result of typing Ctrl+...


4

What I've tried is a exe ':'.pupid.'windo norm! zi' however the command fails upfront with and 'Invalid range` error. The pupid is being obtained via popup_findpreview() and is correct. That's because popup_findpreview() gives you a window ID, but the range passed to :windo expects window numbers: :[range]windo {cmd} Execute {cmd} in each window or if [...


2

To apply Ex command to an arbitrary selection you can do the following: Copy selection to the end of file Select those new lines and run the command Cut the result Replace old selection with the result Of course, it's no good in doing this manually, so the plugins exist. I know of VIS (written by DrChip), and vim-opera (that one is mine; it also provides ...


0

If you don't want to use text expanding tools, you could put every snippet in a separate vim file and simply read in the snippet (file) you need by :r snippet1 (or whatever filename you have given to the intended snippet). You have to be in normal mode to do this. Make sure Vim knows which directory contains your snippets.


2

You could use abbreviations as we used to do 2 decades ago, but I wouldn't recommend it for multilines snippets as the ones you've shown. Of course it's possible, to use abbreviations. As we can do with abbreviations almost everything we can do with snippet plugins -- on the details that you'll want to eat away the extra space you'll be inserting with the ...


3

TL;DR: Check out :help abbreviations. Example: Let's say I wanted to make a snippet to quickly create a JavaScript object in the following format with the abbreviation "obj" (| represents the cursor position after doing the abbreviation): var foo = {|}; I could do: iabbrev obj var foo = {};<Left><Left> Then, whenever I type obj in ...


2

Try this: :echo index(v:argv, '-u') != -1 ? get(v:argv, index(v:argv, '-u') + 1, '') : '' Require the patch 8.1.2233. See :h v:argv. At this moment, Neovim 0.4.4 does not have the patch and Neovim 0.5.0 has the patch.


0

^[ is a representation of <Esc>, the escape key. When you scroll up or down using your mouse or trackpad, your OS sends an escape code to the terminal. <Esc>OA and <Esc>OB are the escape codes for Up and Down. The escape codes for Right and Left are <Esc>OC and <Esc>OD. Kinda annoying that you're seeing this instead of a more ...


2

Here's one way that works... function! TaskSearch() ..snip... let task_note = system('find ~/.task/notes -name "' . task_id . '"\*') exe 'e ' . task_note endfunction Call with :call TaskSearch(). Some of the key changes... Escaping of glob (*) in the find call. Use of system() which is a vim function that returns result of executing ...


2

I ended up combining these three answers to make my function. No, there is not a way to get just a capture group from a search, but if your goal is just one capture group, you can use \zs and/or \ze as your group start and end points to determine highlighting and where your cursor lands. " Ex: change <\(\w\+\) to to <\zs\w\+\ze (replace capture ...


5

Vim now has win_execute() which you can use to execute commands in any window. It takes a window ID as the first argument, which you can retrieve with e.g. win_getid() starting from a window number. Example: let id = win_getid(1) " ID of first window call win_execute(id, 'wincmd J') NOTE: Neovim does not have this feature yet. Late reply, but might be ...


1

Side note: a global-local option may not have been overridden at the local level. This means we could work either with a global setting or one that has been overridden in a some buffers. Usually users don't need much to bother with that. Even in most plugins, we can simply test &{optionname} that will return the most specialized value. The troubles come ...


2

This is all told under :h :let-option, but, to put it short, let &option="value" is the same as set option=value, let &l:option="value" is the same as setlocal option=value, and let &g:option="value" is setglobal option=value. So your last point is actually about the difference between set/setglobal/setlocal commands....


4

So I found that gn was the one I need; from :help gn: Search forward for the last used search pattern, like with n, and start Visual mode to select the match. If the cursor is on the match, visually selects it. If an operator is pending, operates on the match. That means I can combine it with yank and get the text from the register into a variable. So I ...


1

A bit overkill but I refer you to How to run a python command based on a matched group of a pattern? Based on the pattern(first match group), you can then run a python command. Here I set a vim variable. :%GL/PATTERN/py vim.command("let var='" + match + '"') some escaping problems might occur.


4

I don't understand those advanced regexes, but this one should do: ^```python\_$\_.\{-}\_^```$ To understand it, let :help ordinary_atom guide us: \_$ matches end-of-line. \_. matches any character, including end-of-lines. \_^ matches start-of-line. And with :help non-greedy, \{-} is the same as * but uses the shortest match first algorithm. Remember ...


4

This should match a string starting with ```python, then everything except triple quotes, then triple quotes, but it doesn't. That's because the * multi does not repeat any character; it just repeats a negative lookahead, which doesn't add anything to the overall match. The regex engine can't reach the closing triple backticks. /```python\(\(```\)\@!\)*``` ...


1

The documentation in :help has() has a pointer to the features it can check for: The {feature} argument is a string, case is ignored. See feature-list below. If you then look at :help feature-list, you'll see it mentions the three kinds of features that can be tested using has(): There are three types of features: Features that are only supported when ...


Top 50 recent answers are included