Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

35
votes
5answers
9k views

Detect OS in Vimscript

Can I retrieve the current operating system (Windows, Linux, OS X, ..) using pure Vimscript (no Python or Perl)? I want to enable different settings in my (synchronized) .vimrc for different types of ...
17
votes
1answer
815 views

The simplest way to start Vim in private mode

I want to be able to easily start Vim in a private mode. Let's define a private mode as one that utilizes this set of commands: set history=0 set nobackup set nomodeline set noshelltemp set ...
13
votes
1answer
5k views

How to use a variable in the expression of a 'normal' command?

How can one use a variable when executing something via normal! in vimscript? Example: function! MyFunction(someArg) normal! (a:someArg)l endfunction This should move the cursor someArg steps ...
16
votes
1answer
1k 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 . '!'
6
votes
1answer
691 views

Is there any way to distinguish between neovim and regular vim?

I was trying to migrate to neovim from vim. But in the end, there's some plugins which require python3 support and neovim package doesn't build with python3 by default( I'm on ubuntu, install neovim ...
4
votes
1answer
333 views

Making a box in vim via python

I am attempting to learn more about scripting vim and writing plugins with python, but I'm having trouble getting started. Although I've read :h python (first step, read the manual), I had trouble ...
2
votes
1answer
458 views

How to remove inner content of html tag conditionally?

I've html file (e.g. curl -o page.html http://example.com/) where I would like to remove inner html block for given html tag. I've the following code which works: $ ex -V1 page.html <<-EOF /&...
1
vote
1answer
111 views

Unmapping filetype mappings

I'm trying to unmap some key sequences mapped by python filetype plugin. While I know a better way, I'm still curious if it can be done with autocommands. Here's what I've got so far: augroup ...
24
votes
6answers
4k views

How can I really have some fun in Vim? [closed]

I love Vim. I spend my whole life using it: At work I develop code with Vim, I edit config files with it, I write my emails with it. At home I write my todo lists and my buckets lists with it. On my ...
14
votes
3answers
489 views

Set the paste option, but for one insertion only

I want to have a quick way of setting the Vim 'paste' option, inserting some text, and reverting to the previous value of 'paste'. A typical use case insert the OS clipboard content literally, ...
30
votes
3answers
5k views

How do you “chomp” a string in Vim?

Suppose you run the following piece of vimscript: let @z = system("date") This will put a string version of the current date into the z register, but the string will end with a newline that I don’t ...
15
votes
3answers
2k views

Why does vim allow integer division by zero?

I just discovered that vim obviously allows division by zero: :let a=42/0 :echo a prints 2147483647 (which is the value of a). Is this documented somewhere and why does vim allow division by zero?
10
votes
1answer
796 views

Is it possible to create custom “set” commands in my rc file and call them from a modeline?

I recently asked a question in which I learned that it is not possible to pass anything except parameters to :set in vim modelines. I understand the security concerns, but I'm wondering if there isn't ...
6
votes
1answer
283 views

Display current subroutine/subprogram in statusline

I am currently coding fortran, and I have huge "subprograms" (called "subroutine" in fortran). So I would like the name of the current subroutine (the subprogram my cursor is currently in) to be ...
6
votes
1answer
528 views

Store the number of matches in VimScript function?

Is there any way to store the number of matches in a variable in a VIMScript function? For instance I'm using: %s/,//gn
4
votes
2answers
1k views

How to iterate over buffers in Vimscript

How can I iterate over the current open buffers and say, load the buffer numbers into an array in vimscript? I know there exists arrays or similar data structures in vimscript. I know that every ...
4
votes
1answer
724 views

How to make a movement function work with delete, yank, etc

In hope of answering Using the % command on a line without a bracket I tried to write my first Vim script. My thought was to check if there was a { on the current line. If there was one I wanted % ...
0
votes
1answer
300 views

How can I disable square-bracket-prefixed mappings, so I can then remap a single-press square bracket without having to wait for the “timeoutlen”?

I was looking through the default mappings for square brackets, and I really don't need most of that functionality. I would like to disable these mappings, then use nnoremap [ [[ and nnoremap ] ]] to ...
7
votes
2answers
562 views

vimscript conditional based on grep on current buffer

I'm looking for a way to execute some code within a function conditionally depending on the presence of some text within the edited file. I found a way to do it which works but doesn't feel very "...
6
votes
1answer
488 views

Why don't I get syntax highlighting when I open a file in an autocmd?

While trying to devise a solution to How do I make vim look in a set of directories for a file, if it doesn't exist in the current directory?, I have got so far using Python: function LookupFiles ...
6
votes
1answer
218 views

Is there an autocmd-like event for when operator pending mode is entered?

I'd like to make a plugin to display currently pending keys while in operator-pending mode. As the first part of that, how would I register an autocmd to run when operator-pending mode is entered? I'm ...
5
votes
1answer
402 views

Interrupting (blink-highlighting) function if mapping is invoked again

I have an adapted version of Damian Conway's Die Blinkënmatchen in my ~/.vimrc that "highlight-flashes" search matches as I step through them with n / N: " 'hlsearch' makes it hard to tell which is ...
5
votes
1answer
210 views

Putting the value of an argument into part of a regex in vimscript?

Is it possible to pass a variable to a function in vimscript, and then use that value in a search and replace regex? For example: function! AddTW5Links(tiddlerSetName) %s/\(*\+\)\(\[\[\(.*\)\)\...
1
vote
1answer
113 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 ...
0
votes
0answers
37 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 = ...
11
votes
2answers
1k 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 ...
18
votes
2answers
5k views

Extract the result of a shell command to a variable in a .vimrc file?

In my .vimrc file, I would like to store in a variable the result of an external command, in my case : $ echo $LANG So that my vimrc file would have : let language = output(!echo $LANG) if language ...
14
votes
4answers
1k 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 ...
9
votes
2answers
1k views

How can you use vim as a stream editor? [duplicate]

Like most programmers, I perform a lot of repetitive tasks. In optimising my workflow, I'm taking some of those repetitive tasks, and refactoring them into shell scripts. One thing that I'm trying to ...
8
votes
1answer
3k 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 ...
7
votes
2answers
665 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 ...
5
votes
1answer
775 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 "...
3
votes
1answer
3k 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

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
1k 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 ...
9
votes
1answer
1k 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.
8
votes
1answer
164 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 ...
6
votes
1answer
286 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 ...
6
votes
3answers
1k 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)?
5
votes
2answers
204 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
330 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 ...
4
votes
3answers
509 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 ...
3
votes
1answer
170 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 ...
2
votes
1answer
40 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 ...
1
vote
2answers
598 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'] ...
0
votes
2answers
188 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 ...
19
votes
2answers
8k 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.
11
votes
2answers
3k 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 ...
8
votes
3answers
659 views

How to grep in ex command output?

How does one search for a pattern in ex command output? For example, how to search for a file name pattern in the output of :scriptnames command? In the help for all flavours of grep and vimgrep there ...
6
votes
1answer
743 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 ...