Questions tagged [functions]

Question about the use of functions in Vimscript defined in the `.vimrc` or a script file.

Filter by
Sorted by
Tagged with
0
votes
1answer
22 views

Using a multiline closure to execute commands in vimscript

I'm writing a script in which I am in need of closure functionalities. Take this function, for instance: func! SetOpNorm(norm_comm, motion_key = 'y') let l:norm_comm = a:norm_comm let l:...
2
votes
1answer
34 views

How to use func to quickly add specific comments to certain lines of a file?

I wrote a function, the function is used to add comments to some lines, the content is as follows: func Add(x) call setline(a:x+1, "\ //*********************************************************...
1
vote
3answers
138 views

Function That Substitutes Across Multiple Files

I'm trying to create a function that implements the functionality outlined here: Vim search replace all files in current (project) folder My current attempt: nnoremap <C-F> :call ...
0
votes
1answer
29 views

How do I open a relative path under the cursor

I often have file log where there are several relative path to files. The paths are relative to the location of the filelog. My understanding is that I'm able to open the relative path if the CWD of ...
1
vote
2answers
29 views

Using ! in a function

I have the following command for arranging a few lines in a file using sed and column: :-3,+2!sed 's/- 2021/\&2021/' | sed 's/ 2021/\&2021/' | column -s '&' -t This works fine. For ...
4
votes
1answer
122 views

What is the most idiomatic way to achieve filetype-specific functions?

Is it possible to declare a function only for a specific filetype? Currently, if I have the following in ftplugin/tex.vim: function! Foo() echom "yes" endfunction Then when I load a *....
0
votes
1answer
39 views

Is it possible to call a shell command which includes default register contents inside a function?

I want to write a function which opens a vim plugin's Github page, eg calling the function while on this line: Plug 'tpope/vim-rhubarb' ... will do !open https://github.com/tpope/vim-rhubarb. Here's ...
3
votes
1answer
35 views

Backward search in current line with search() and z option

I am trying to write a couple of simple functions to find the next and previous links in Vimwiki and move to them. Links in Vimwiki are indicated with the syntax [[link to something]]. Since I only ...
0
votes
1answer
35 views

Change text without interrupting abbreviations

I'd like to add a semicolon automatically in my current line. So I have this in my init.vim: function Test() if matchstr(getline(line('.')), ';') != ';' && strlen(getline(line('.'))) > ...
0
votes
1answer
33 views

Make function run only on specific filetype

So i have a specific function I'd like to edit in my .vimrc, so that it work only on specific filetype (eg: .vim). The code for this example is taken from this answer. function OrgFold(lnum) let ...
0
votes
1answer
42 views

How to return keybinding from a function?

I want the below function to return keybindings but all it returns is a string. nmap <expr> <space>t ManageTerminal() function ManageTerminal() if bufname('terminal') ==# 'terminal' ...
3
votes
2answers
82 views

Reliably saving and restoring registers across a function call [duplicate]

When writing functions to use in mappings and user-defined commands (for plug-ins), it's often convenient to use a Normal-mode command to yank or delete buffer contents. In those cases, it's also nice ...
1
vote
2answers
138 views

<Space> doesn't insert a space within a function

So I'm trying to have a function type out a space which goes as follows: function! UnderlineHeading(level) if a:level == 1 normal! yypVr= elseif a:level == 2 normal! yypVr- else ...
0
votes
1answer
99 views

Vim function : how to expand vim variable in shell command?

I'm trying to write a vim function to update ctags and cscope databases. I'm trying to save the vim variable projectRoot in environment variable then expand this enviroment variable in the find ...
0
votes
1answer
18 views

Any way to create a VimL equivalent of Python's @property decorator?

As the title says, I'd like to be able to use something like Python's @property decorator (its semantics ofc, not the syntax) when writing a 'class' in VimL. Currently I have something along the ...
1
vote
2answers
49 views

Insert output of function with variable into buffer

I'm trying to make a vim function that takes a variable and uses that as part of a shell command and buts it into the current buffer. I tried using read but that just interprets the var as a string. ...
0
votes
1answer
44 views

switch toggle function - background

I'm trying to assign a key toggle to switch between dark or light background. I wrote this function: function ToggleBG() let l:bg='set background?' if l:bg==dark :set background=light ...
0
votes
1answer
298 views

Inside a mapping check if a variable is empty in vim

So i'm new to vim, and specially new to vimscript, and messing around i made the following code to get the name for a file and then create said file in a new buffer. I want a way to check if FileName_ ...
1
vote
1answer
36 views

in-place replacement of code in HTML documents with TOhtml?

I'm working on some old HTML documentation, where are multiple code snippets (several lines long). I need to replace this old HTML-ized code with new one using modern styling. What I have now: ... ... ...
2
votes
2answers
69 views

How to list / show all built-in functions?

How can I list / show all built-in functions, similar to the :function command which lists all (non-builtin?) functions?
1
vote
1answer
31 views

Is there a function which returns the filepath at the cursor?

I'm looking for a function(probably doesn't exist) which can return the filepath under the cursor. I know that there is a command/shortcut gf that helps me jump to that file if the file exists. But I ...
2
votes
1answer
32 views

Where is the help function defined or located?

I'm trying to find where the help function/command is defined. So far I tried: :verbose function help but since the h isn't in uppercase, it doesn't work (not sure if it's even a function at this ...
3
votes
1answer
48 views

bufnr return unexpected number

from neovim help: bufnr([{expr} [, {create}]]) The result is the number of a buffer, as it is displayed by the ":ls" command. For the use of {expr}, see |bufname()| But it ...
4
votes
1answer
53 views

Set variable equal to search match subsection

Unnecessary Context: I want to write a function that closes open HTML tags, because closetag.vim seems complicated, and I like to understand all the stuff I run in Vim. My plan for this is to match ...
1
vote
2answers
320 views

Global options as function local variables

I'm asking about this code snippet: :function TextwidthIsTooWide() : if &l:textwidth ># 80 : return 1 : endif :endfunction This is what vim-help says on the l (local) prefix: Inside ...
0
votes
1answer
35 views

What's the fastest way to go about defining functions in Python?

def counter(num): I always get stuck inside of the parenthesis and find myself pressing ESC, going back to Normal mode, then pressing A to append the colon to the end of the line. Is there a faster ...
1
vote
1answer
56 views

Get correct length for unicode characters

Basically title, the length of unicode characters as calculated by the len() function is off by one, for example :echo len("à") 2 or :echo len("°") 2 is there a way to fix this, ...
1
vote
1answer
154 views

Improve copy to clipboard using xclip

I'm trying to improve the answer here for copying to clipboard in a vim without the +clipboard option. The problem with the above approach is that :w always writes the entire line, so it is not ...
0
votes
1answer
89 views

Formatting using BufWritePost ends up with WARNING

I am trying to format PHP files using the following function: function! PHPCSFixer() let l:fixer_path = '$HOME/....
1
vote
1answer
40 views

Buffer opened when compiling succeeds

I am writing a vim plugin for the fortran filetype. In an autoload script, I have done: " File Name: autoload/makes.vim let s:Compiler = get(g:, "fortran_compiler", "gfortran"...
1
vote
2answers
58 views

How to I insert a return value from a function call into an existing buffer?

This following vim script calls a function that returns an array called lines: call vimwiki#diary#generate_diary_section() There seems to be a bug and nothing is done with the lines array—the content ...
0
votes
1answer
47 views

Use normal command output as variable

I want to be able to use some normal commands I tend to use in the day to day as an input for another function, for example 4X, I would like 4X to be expanded to XXXX and then use it in a function as :...
1
vote
3answers
37 views

How to recover the color to previous status?

I wrote a function to show some special characters with red color: function! ShowChar() set list set listchars=tab:→\ ,space:·,nbsp:␣,eol:¶ highlight SpecialKey ctermfg=blue ctermbg=red ...
0
votes
0answers
43 views

How to clear and write current buffer in a function

New to vimscript. I'm writing a function that I want to be able to clear the contents of the current buffer and write this to the corresponding file (or alternatively, clear the contents of the file ...
0
votes
1answer
32 views

Refreshing vim file from within a function

I am trying to do a shortcut to resave a file with cmd-S. And if the file is of type vim, it will also re-source %. Here is what I have: function SaveFile() w if &filetype ==? 'vim' ...
2
votes
1answer
36 views

How can I fold some lines on opening a buffer?

I want to fold from "Copyright" to "CONNECTION" every time I open a .c or .h file, so I don't have to see it. The license does have newlines in it in my files, so selecting shouldn'...
1
vote
1answer
34 views

write easy accessable function

I want to run an external command by typing :runMyCommand (just run a shellcommand). I thought I'd put the shell command execution into a function, so I can call it with :call runMyCommand, but I want ...
0
votes
2answers
36 views

How to store state (e.g. variables) between user actions?

The main question is in the title, that is, I have a function, which is invoked by the user and it should 'remember' necessary stuff between invocations, so the next time the user calls it (like, ...
0
votes
1answer
120 views

Vim snippets with arguments

I'm trying to create snippets without using any plugins. For instance, nnoremap ,eq :-1read $HOME/.vim/snips/.eq.tex<CR>o<TAB> inserts the contents of .eq.tex: \begin{equation} \end{...
1
vote
2answers
140 views

Function to remove all trailing whitespace

I have the following function to remove every trailing white space from the document, without changing my cursor position function! StripTrailingWhitespaces() let l = line('.') let c = col('.') ...
0
votes
1answer
44 views

How to pass a parameter to a function called from an on_exit event of an asynchronous task

Imagine the following asynchronous job to convert a given file to odt : let mdfile = 'testfile.md' call jobstart (['bash', '-c', 'pandoc ' . mdfile . ' -s -f markdown -o test.odt'], {'on_exit': '...
2
votes
1answer
164 views

execute + colon character doesn't work

I want to :retab selection in my vimscript function. When I write execute "normal! :'<,'>retab" nothing happens. If I write: let cmd = "normal! :'<,'>retab" echom cmd The output is ...
2
votes
1answer
166 views

Remapping `v` in netrw/calling NetrwSplit(5) directly

So I've created some helper functions to help work around netrw. They are some keybind simplifications that help with things like window/cursor management when opening files, where to open them, ...
1
vote
2answers
230 views

sourcing .vimrc function does not work

I am using nvim v0.4.3. This is the latest version available in repository. I have an Arch build with the most up to date packages and kernel 5.6.4-arch1-1. I have the below function in my ~/.vimrc: ...
1
vote
0answers
61 views

Is there anyway to check if cursor is on pumvisible suggestion or not?

I wrote the function below, https://github.com/ryuta69/dotfiles/blob/master/dotfiles/.vimrc#L129-L146 fun! DeoEnter() if !pumvisible() return "\n" endif if vsnip#available(1) ...
1
vote
1answer
14 views

Saving and resetting alternate file

I have the following in my vimrc to open pdfs externally with xpdf. function! OpenPdfExternally() silent execute "!xpdf " . shellescape(expand("%:p")) . " &>/dev/null &" edit # ...
1
vote
1answer
79 views

How to check whether a command is run with range or not?

For example, I have the following code command! -range Test call TestFunc() function! TestFunc() abort " if run with range " get selected content " else " get current word under the ...
1
vote
2answers
116 views

How to create an abbreviation with space

I'm trying to create an abbreviation with the command ab: if I type if ( under INSERT mode, I want some piece of code to be printed immediately. Here is what I've tried: ab if<Space>( if<...
2
votes
6answers
644 views

How to delete a pair of parenthesis with backspace

I'm using vim to code C++ and I want to handle the parenthesis more easily. I knew that there was a plugin named Surround, which can help us but it is not what I expected. I set some shortcuts as ...
1
vote
3answers
98 views

How to loop backwards through getline

I want to code a vim function, which searches backwards a specific char from the cursor at the current line. Here is what I've done: function! CustomSearch() let currentLine = getline(".") "...

1
2 3 4 5