Hot answers tagged

7 votes
Accepted

How can I alias a command with arguments?

it doesn't seem to pass in the argument Because you haven't passed it. The right syntax is: command! -nargs=1 Vres vertical resize <args> Simply read :help :command until the very end to know ...
  • 18k
7 votes
Accepted

Keep specific parts of a :s command in nvim

Yes and no. Depends a lot on the complexity of the documents. Textile has a lot of various formats for links and Markdown has it's share. Keeping pattern When you do a substitute you can capture parts ...
  • 370
5 votes
Accepted

Define w< command

The reason why the approach using user commands (with :command!) does not work is that user commands are quite restricted in how they are allowed. They must always start with an uppercase letter (so ...
  • 26.5k
5 votes
Accepted

Is it possible to have vim run a command every time save `:w` is invoked?

Does something like this exist? In Vim this is called an autocommand. The relevant help topic can be found by typing :h autocommand (sic!) An example code to put into vimrc: augroup typescript_save ...
  • 18k
5 votes
Accepted

creating a ":KeepCursor" command

Can it be written? Yup. command! -nargs=* -complete=command KeepCursor \ let [s:view, s:win] = [winsaveview(), win_getid()] | \ try | \ execute <q-args> | \ finally | ...
5 votes

how does vim make the difference for "i" between the insert mode and the inner word

Vim keeps what you type in memory, even when it seems to have no effect on the screen, this allows you to apply modifiers to commands. To see what modifiers you typed, and will be applied to the next ...
  • 1,101
5 votes
Accepted

how does vim make the difference for "i" between the insert mode and the inner word

This is the description of c in the help file: ["x]c{motion} Delete {motion} text [into register x] and start insert. The register is optional, but the motion is mandatory. A lone c makes ...
  • 2,297
4 votes
Accepted

How to add modifier to user defined command?

You can use :h <mods> to do this, it expands to modifier or nothing. It's added at patch 7.4.1898 command Foo call FooSplit(<q-mods>) function FooSplit(mods) exe a:mods 'new' ...
  • 5,978
4 votes
Accepted

How to define a command if the function needs to take an argument from the script itself?

The problem is that, in the body of your EchoMessage() function, text does not refer to the value passed in, but instead to a different local variable. Use a: to refer to parameters passed in: echom ...
  • 29.1k
4 votes
Accepted

How to color VIM ":!{cmd}" output

The video shows Vim, not Neovim. In Neovim the output of "bang" is not colorized. If you like long stories you can read this and this and so on. TL;DR they think it's not a big deal, but ...
  • 18k
3 votes
Accepted

What does the command option '-range=N' do?

Using the -range flag when defining a user-command usually allows the user to provide a range to the command: command -range Mine echo <range> <line1> ',' <line2> %Mine 1,/bar/Mine "...
  • 21.9k
3 votes
Accepted

Why does `<Bar>` behave like `|` in the `:command` command in Vim?

So I went digging for this one and as far as I can tell this behavior has been present ever since the user-defined command feature was introduced in Vim 5.2. Initially I thought this was present ...
  • 26.5k
3 votes
Accepted

text replace problem when target contains '&' symbol

& in a replacement is a bit special: magic nomagic action & \& replaced with the whole matched pattern *s/\&* But you can escape it with \: %substitute/\Vif(cond1)/...
  • 21.9k
3 votes
Accepted

Repeating command for running external script?

Based on :help v:count: *v:count* *count-variable* v:count The count given for the last Normal mode command. Can be used to get the count before a mapping. Read-only. ...
  • 21.9k
3 votes
Accepted

'@' in file names and 'gf' (go to file)

Am I missing something here? Yes, as written in :h 'isfname': If the character is '@', all characters where isalpha() returns TRUE are included. Normally these are the characters a to z and A ...
  • 18k
3 votes
Accepted

Print Command Without Hitting Enter

I’d recommend an <expr> mapping—make the following changes: nnoremap <expr> <Leader>b BuildSystemTask() And then in your function: if inProject() return ':make! ' else return '...
  • 21.9k
3 votes

creating a ":KeepCursor" command

Can it be written? Possibly, but it might be a lot harder than expected. Because, what do you do with commands that close the current window? Or commands, that add lines above the current cursor ...
3 votes
Accepted

How to pass everything from a command into a function with escaped characters

You must add -nargs=1 to your command: :command -nargs=1 FooCommand :call Foo(<q-args>) This tells Vim not to treat white space as separator. Only white space between the command name and the ...
3 votes
Accepted

set a default value to a command that acccepts two arguments?

Here's the general idea... func! MakeHLine(...) abort " Default delim char and width let l:ch = '#' let l:tw = 78 " Override defaults per given parameters if a:0 ...
  • 18.5k
3 votes
Accepted

How do I quit vi/Vim/NeoVim?

In short, hit the Esc key to make sure you're in "Normal" mode, then type :q (which will appear on the last line of the screen) and press the Enter. If Vim complains that you have unsaved ...
3 votes
Accepted

Append inside brackets, quotes, double quotes

There's not a command that follows the ci... structure to do that. You can add a remap to do it (see below), or use core vim commands f and t (and their counterparts F and T) motions to get the cursor ...
  • 1,006
3 votes

How can I make a command that checks iteratively through parent roots until it finds something or reaches C:/?

You can use the findfile() function, together with the file-searching features in order to try to find the path to a file up the directory tree. For example: :echo findfile('top-level.txt', '.;') ...
  • 26.5k
2 votes
Accepted

How to use variables in a user defined command?

Can simply use exec and . to build the command: let PathToPluginsFolder = ~/.vim/plugins command! PS silent! exec ':w | PlugSnapshot! ' . PathToPluginsFolder . '/plugins.lock'
  • 596
2 votes
Accepted

In command mode make delete not act like backspace

To change the behavior of <Del> so that it only deletes the character under the cursor and doesn't behave like backspace when at the end of the line, you can use the following mapping: cnoremap ...
  • 26.5k
2 votes

In command mode make delete not act like backspace

You can use the following function: function KillEnd() let l:cmd = getcmdline() let l:pos = getcmdpos() let l:newcmd = strpart(l:cmd, 0, l:pos - 1) return l:newcmd endfunction To use it on ...
  • 21.9k
2 votes
Accepted

execute + colon character doesn't work

You're missing a final <CR> at the end of your normal command. If at the end of normal you have an incomplete command, it will cancel it as if <Esc> or <C-c> was pressed. So this ...
  • 26.5k
2 votes
Accepted

What does + preceding command do in vim

The + isn't preceding the setlocal command, so much as following the :new command: it's running the setlocal command in the newly created window. See :help :new and :help +cmd for more details.
  • 29.1k
2 votes
Accepted

Making custom command behave like ":echo" with respect to quotes

For this particular task it's much much easier to do simply :filter /txt$/ oldfiles The error is due to -bar argument: the quote is parsed like a comment sign, and so the rest of the command is lost (...
  • 18k
2 votes
Accepted

Is it possible to execute keys/commands in select mode?

By keys I mean like being Normal mode and pressing e (takes you to end of [next] word), by commands I mean like being in Normal mode and pressing Shift+; and typing :delete. Yes. From Select mode, ...
  • 26.5k
2 votes

optimise surround with quotes

If you're using the VS Code extension that I think you are then my understanding is that it supports at least some plugins and one of those is vim-surround. If that's the case and you're open to using ...
  • 18.5k

Only top scored, non community-wiki answers of a minimum length are eligible