Questions tagged [vimscript]

A scripting language embedded into Vim. It can be used to customize Vim to suit your needs and to create plugins. Also called VimL.

Filter by
Sorted by
Tagged with
3
votes
2answers
215 views

Replace text and move cursor to end of replacement in insert mode

How can I perform a search-and-replace and leave the cursor at the end of the replacement? I want to replace $...$ with \(...\) as I type in insert mode. Notice that I can't use an abbreviation, ...
2
votes
1answer
52 views

Any way to execute vimscript beside yanking and doing @" on the commandline?

I'm used to just yanking and pasting the register @" into the commandline to execute vimscript, when I'm doing testing experimentation (since it's faster than pasting it into the .vimrc) Any ...
1
vote
1answer
184 views

How to use the result of a function in a Vim keymap?

I have a function in Vim script that returns a string. " Return first directory with ".git" folder, if none is found " return the current directory function! s:project_root() for vcs in ['.git']...
1
vote
4answers
202 views

line-continuation in an an if-statement

For better readability I'd like to split if-statements with multiple expression into multiple lines. But when executing such a statement via a register vim always complains about it. This: if 1==1 &...
1
vote
1answer
192 views

Visual mode toggle block commenting with vimrc command

I am trying to get vim to do block commenting and uncommenting of highlighted lines while keeping the correct indentation level. I am so frustratingly close. let b:commentChar='//' autocmd ...
1
vote
2answers
74 views

Command-P 'paste script/shortcut'

I am using a mac, and I usually paste things in by doing the following: :set paste! cmdp :set paste! This is really the only way I've been able to paste things into vim reliably (with the help of ...
1
vote
4answers
278 views

How to write incremental lines in Vim?

There comes a time where I'm in a situation of downloading multiple files. And the name of files are in series. Something like. http://www.example.com/files/images/img01.jpg http://www.example.com/...
0
votes
0answers
68 views

How to get the same message and error behavior as normal `n` when calling `normal n` in function

I wrote a vimscript function that prevents the cursor from changing its location on the screen when I press n: function! my_n() let l:start=line("w0") let end = line("w$") let cursor = ...
7
votes
1answer
6k views

Most annoying slow-down of a plain-text editor! [closed]

I am a happy user of VIM, except for some bits which I really want vim to improve on. One main issue is frequent slowdown of VIM spoiling the amazingly productive text-editing experience on a fastest ...
13
votes
2answers
2k views

Is it possible to use a delegate or to pass a function as argument in Vimscript?

I am trying to create a small plugin to learn vimscript, my goal is to create some functions processing a selected text and replacing it with the result. The script contains the following items: Two ...
14
votes
1answer
2k views

How to check whether Vim is built as a 32-bit or 64-bit application?

How do I check if Vim is compiled as 64-bit or 32-bit from within vimscript? Specifically on Windows.
13
votes
4answers
2k views

How to reverse every 4 lines?

First of all, with this being my first post here, I'd just like to say that I've found VIM to be a great tool and the forum here to be very helpful in finding answers to questions, with lots of ...
17
votes
2answers
5k views

How to restore the position of the cursor after executing a normal command?

I am trying to code a function that replaces the character in the sixth column of my current line with a dollar symbol ($), but I would like my cursor to remain at the position it was before calling ...
16
votes
2answers
2k views

Is cpo&vim a special syntax?

In scripts it is customary to do something like: let s:save_cpo = &cpo set cpo&vim ... script ... let &cpo = s:save_cpo To ensure nocompatible mode for the script. Is: set cpo&...
10
votes
1answer
5k views

vimL sleep within a function

I'm trying to define a function that will make it easier to see where the cursor is location by putting cross-hairs around it for 100ms. Here's my attempt: function! Flash() set cursorline ...
9
votes
1answer
627 views

How to allow count *before* my custom operator?

I'd like to define my own operator. Vim's builtin help has a nice little tutorial on it, where they show you can create your own operator with opfunc and g@: nmap <silent> <F4> :set ...
8
votes
2answers
1k views

Drawbacks of using Python to develop new code in a VIM plugin

In one of the vim plugins I use, we have been thinking about using python vs vimscript going forward (https://github.com/python-mode/python-mode/issues/813#issuecomment-344754563). I understand that ...
6
votes
4answers
2k views

How can I get the character at the cursor position in a multibyte aware manner?

Consider the following line: € € This file is encoded in UTF-8 and between the two Euro marks is a Tab character (the Stack Exchange software expands this to spaces). The byte value is: e2 82 ac ...
5
votes
1answer
1k views

Why `NERDTree` doesn't trigger the autocommand `BufEnter`?

I am writing a vimscript function which should keep in a consistent state a "home made buffer manager". The idea is that I want to keep a trace of my buffers in the tabs they were open in. This "...
37
votes
2answers
15k views

How do I get the name and extension of the current file?

Is there a way to get the name and extension of a file using vimscript? If so I would like the name and extension separately.
13
votes
2answers
2k views

How to get the current byte offset in whole file

I saw that you could display the current byte offset in the statusline using %o, but I found no function or command which does the same. Is there a way of getting the current byte offset ...
10
votes
1answer
2k views

Prevent refresh of screen until function completes

I have a function that does a lot of moving and outputting of text into the current vim buffer, and when I run it, seeing all that happening at blinding speed is a bit disconcerting. How can I ...
7
votes
3answers
2k views

Within a function, Is there a way to determine if vim is in insert mode?

Within a function, is there a way to determine if vim is in insert mode (or was in insert mode prior to calling the function)?
7
votes
1answer
185 views

Is it possible to make a numerically-prefixed hotkey run a function that many times?

I find that if you enter a numeric prefix prior to executing an ex command, the convention that it applies is to set up the command to prep an operation across a line range. It is explained in the ...
7
votes
4answers
2k views

Is it possible to populate the quickfix list with the errors of vimscript functions?

I often write functions in my ~/.vimrc or in ~/.vim/autoload/myfunctions.vim, which when executed result in an error such as: Error detected while processing function SomeFunction: line xx: ...
6
votes
2answers
511 views

How can I watch variables when debugging Vimscript?

I would like to have another pane active when debugging a Vimscript function which would show the values of chosen local variables and function arguments dynamically. Currently I have to echo or let ...
6
votes
2answers
1k views

Read values from a highlight group

I can read a value from an option like: let b = &background Is there a way to read a value from a highlight group? Something like this pseudo-code: let b = &StatusLine['ctermbg'] ...
5
votes
2answers
510 views

Vim syntax highlighting for ChatScript language?

I'm looking for a ChatScript language Syntax highlighting, of course for the Vim editor. ChatScript is a great (underrated) open-source language to build chatbots dialog flows scripts, made by Bruce ...
5
votes
2answers
222 views

Is there a better way to create this “semi-strong” left motion?

I'm pretty new to vimscript, and I wrote this little function to map H so that the cursor will go to the first non-whitespace character of the line, or to the beginning of the line if the cursor is ...
4
votes
2answers
363 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) ...
4
votes
1answer
2k views

How to get the current mode in vim?

I'm writting scripts for vim, and I want to let the script do different things in different mode. I know that the statusline will show the current mode, but I don't know how to get the exact current ...
4
votes
3answers
721 views

Run a function once on every paragraph

I'm trying to write a plugin that highlights overused words. After much effort I managed to write a function (and a command, :Ditto) that will highlight the most used word in the file or in a ...
4
votes
2answers
212 views

Creating a repeatable mapping in Vim

I find myself often aligning certain code blocks by inserting one or more spaces, like in the following situation: case something of Just a▉-> ... Nothing -> ... Here ▉ is the position ...
3
votes
1answer
270 views

Slow startup time unrelated to plugins

When I open vim (or gvim) on MacOS X, the window opens immediately, but there is a pause of about a second before the cursor appears. I've profiled my plugins, but their startup times don't account ...
3
votes
2answers
220 views

How to automatically break down the description of a command?

I found the following description of a command which sets up which items must appear in the status line, here: set statusline=%F%m%r%h%w[%L][%{&ff}]%y[%p%%][%04l,%04v] " | | | | | | ...
3
votes
2answers
166 views

Can't repeat negated pattern using \@!

I have this text: ```python x = 1 ``` Basically markdown python cells. I would like to extract the content of the cell surrounding the cursor, if any. As you may have guessed already, I am trying to ...
3
votes
1answer
1k views

UltiSnips, Context and Tex

For best experience, I want to enable some snippets to be expanded only in Tex Math environments. A wonderful person, lacygoill helped me on GitHub to achieve just this with the following setup: Let ...
2
votes
1answer
42 views

Can I disable the >> and >{motion} commands, then remap the *single-keystroke* > to “indent this line”?

I never really use the other commands, and it would be nice to be able to indent/dedent lines this way. The solution for this may be similar to the solution for the question I asked here; perhaps ...
2
votes
0answers
163 views

What happens when error occurs during cdo?

:h :cdo says: When an error is detected execution stops. The last buffer (or where an error occurred) becomes the current buffer. Let's see what really happens: ...
2
votes
1answer
147 views

Error With Simple Function Using setline()

I'm trying to start writing some Vim Script, and I've come across this issue. fu Test() call setline(1, getline(".")) endf autocmd InsertCharPre * call Test() This is in my .vimrc. When I type ...
2
votes
2answers
1k views

Operate on a visual selection by looping through the lines

I'm looking for an approach, in Vimscript, to operate on the text lines of a buffer by looping through lines of a visual selection. Specifically, my goal is very simple: I have a (vertical) list of ...
2
votes
1answer
127 views

Concatenate vim command after :terminal

Is there any way I can concatenate a command after :terminal? For example :terminal | <some_other_vim_command> will pass | <some_other_vim_command> to the terminal that just opened, but ...
1
vote
2answers
2k views

How to execute commands in vim, piping the output back into the editor in real time?

So, to begin, this is similar to Pipe shell command into buffer in real-time. The difference is that the top answer there pipes the output to a new buffer. How do I pipe the output to the editor ...
0
votes
1answer
90 views

Get visually selected text - weird behavior when last selected char is multi-byte

I have made a simple function to echo the visually selected text. The function is simplistic as I am only interested in max one-line selection. The problem is, if the last character of the visual ...
0
votes
1answer
201 views

Renaming .vimrc and putting into other folder

I want to rename my. ".vimrc" as "bob.vimrc" And I want to put it inside "/Users/bob/bobs bits" As you can tell, I'm on a Mac. But everything keeps going wrong. I don't want a ".vimrc" symlink in ~,...
15
votes
3answers
9k views

Substitute with pure vimscript (without `:s`)

I have the following in my vimrc: func! AddSpaceBeforeEqual() s/\([a-z)_0-9"'\[\]]\)=/\1 =/ge endfunc I'm using vint to lint my vimrc, and got the following warning : ...
7
votes
1answer
1k views

Writing modified buffer to a temporary file

I'd like to write the modified buffer I'm currently editing to a temporary file periodically. In the background I could then, for example, use external tools to analyze the file. I don't want to ...
7
votes
1answer
1k views

Is it possible to add inline comments in a multiline statement?

I have a function that looks like fu! MultiLineTest(u, v) if a:u != 2 && \ a:v != 2 return 'yes' else return 'no' end endfu Of course, the actual ...
6
votes
2answers
2k views

Why do functions in Vimscript require a “call” statement?

In Vimscript, if you use a function as an R-value, then the function simply evaluates. For example: let count_pattern += str2nr( strpart( execute( command ), 1 ) ) However, if a function is used by ...
6
votes
2answers
743 views

Auto sorting of lists of include files

Many sections of stuff we right is such that it could be automatically formatted by user defined rules. Here are a couple of examples: List of include files in languages such as C. It is customary to ...