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
45
votes
5answers
13k 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 ...
15
votes
1answer
9k 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 ...
20
votes
2answers
1k 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 ...
19
votes
1answer
2k 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 . '!'
18
votes
2answers
2k views

What is the difference between set and :set (commands with or without a leading colon)?

I was looking at Tim Pope's vim-sensible plugin and I noticed this in the README... See the source for the authoritative list of features. (Don't worry, it's mostly :set calls.) When I looked at ...
8
votes
1answer
951 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
373 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 ...
4
votes
3answers
2k views

How to format a mapping to an if statement?

I'm had some trouble defining a mapping in my .vimrc. I want <leader>sv to source my .vimrc, but first I want to write, if I'm currently editing it. Goal The intention of the code is this: if ...
2
votes
1answer
544 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
199 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 ...
26
votes
6answers
5k 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 ...
26
votes
2answers
8k 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 ...
16
votes
4answers
658 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, ...
8
votes
1answer
1k views

Visual scrolling/visual <C-e> and <C-y> across wrapped lines?

This has been asked on stackoverflow before, but it seems it still has no solution. Perhaps that has changed with Vim 8.0. I'd like to be able to scroll (i.e. scroll-wheel, or <C-y>/<C-e> ...
8
votes
1answer
355 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 ...
32
votes
3answers
6k 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 ...
16
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
898 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 ...
8
votes
2answers
2k 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 ...
11
votes
3answers
1k 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 ...
9
votes
1answer
1k views

Several lines instructions

Is there a way to split a one-line instruction into several lines? For example, I'd like to transform setlocal variable_name = condition1 ? "1" : condition2 ? "0" : condition3 ? "a long string" : "...
8
votes
2answers
591 views

What's the functional difference between the key notations “\<xxx>” and '<xxx>'?

I stumbled recently upon a mapping in the Vim Tips Wiki that uses an expr-quote: nnoremap <silent> <Space> @=(foldlevel('.')?'za':"\<Space>")<CR> and realized it still worked ...
6
votes
4answers
913 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
1answer
841 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 % ...
1
vote
1answer
479 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 ...
8
votes
1answer
3k views

What's the difference between `/x` and `:/x`

Assume buffer content: aa bb yy xx aa bb What's the different between /xx and :/xx?
7
votes
2answers
858 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
575 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
263 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
251 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/\(*\+\)\(\[\[\(.*\)\)\...
5
votes
1answer
560 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 ...
4
votes
1answer
585 views

How does one include a trailing space in a mapping?

I'm currently trying to create the following mapping in my vimrc: nnoremap <Leader>s :split The :split is followed by a trailing space. This makes sense so I can enter the file name without ...
3
votes
2answers
167 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, ...
1
vote
1answer
55 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
224 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/...
1
vote
1answer
176 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
58 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 = ...
12
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 ...
3
votes
1answer
4k 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 ...
20
votes
1answer
2k views

What are the new “popup windows” in Vim 8.2?

The latest version of Vim, 8.2, has a new feature called "popups" or "popup windows". They sound interesting but I'm not quite sure what they're good for. Can you enlighten me...preferably with some ...
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 ...
14
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
2answers
2k 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 ...
9
votes
1answer
4k 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
1answer
509 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 ...
7
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 ...
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 "...
29
votes
2answers
12k 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.
16
votes
2answers
830 views

How do I generate Vim documentation from Markdown notes or inline comments, or are there better ways to add documentation?

This is kind of two questions in one. Lately I've been spending a lot of time learning vimscript in-depth, learning vim's internals & taking my first steps writing vim plugins. During this process,...
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 ...