Hot answers tagged

5

Can it be written? Yup. command! -nargs=* -complete=command KeepCursor \ let [s:view, s:win] = [winsaveview(), win_getid()] | \ try | \ execute <q-args> | \ finally | \ if win_id2win(s:win) | \ call win_gotoid(s:win) | \ endif | \ keepjumps call winrestview(s:view) | \ endtry Can it ...


5

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 | au! autocmd BufWritePost *.ts !tsc <afile>:p:S augroup end


4

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 all the options.


4

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 a:text See :help internal-variables (and specifically :help function-argument) for more details.


4

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' endfunction Above example use <q-mods> instead of <mods>, as you need it as a string.


3

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 " the next one is equivalent to .Mine by default Mine However, sometimes you want to be able to do 0Mine Or else use the number provided as a <count>: ...


3

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 position? One problem you are running into is described unter :h function-search-undo. The last used search pattern and the redo command "." will not be changed ...


3

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 '' An alternative would be to use the feedkeys() function


3

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 first non-white space character is removed from the argument passed to Foo(). All other spaces and tabs, even trailing ones, are included in the argument.


2

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


2

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 to Z, plus accented characters. To include '@' itself use "@-@". So @ in isfname, iskeyword etc. actually means "all alphas", not literal "@". And you still ...


2

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 <expr> <Del> getcmdpos() <= strlen(getcmdline()) ? "\<Del>" : "" See @D.BenKnoble's answer for a solution to deleting until the end of ...


2

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 the command line, you need to do (I choose <C-x> arbitrarily since it wasn't used for anything else, and reminds me of X in normal mode): cnoremap <C-x&...


1

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 should fix it: execute "normal! :'<,'>retab\<CR>" (Using \r inside the double quotes is also possible.) But in this case, there's probably no need ...


1

There actually is a built-in map which does this, <c-^> or control+^ If you don't like this (it is indeed difficult to press), you can remap it simply. nnoremap gb <c-^> Note: I am using gb here since B is already a vim command. You can use B if you really want, but be aware that you would be overriding a built-in. I am also going to show ...


1

Hmm. This is interesting. Here's what get saved as the command (:com Test0): nnoremap <NL> :echom "c-j"<CR> (<C-J> is equivalent to <NL> in Vim) So what do we know about the <> format? That it can be used in mappings and abbreviations as a substitute for the represented keys/chars. That it can be used in exe command arguments ...


1

It's faster as :buf won't reload the file from disk, whereas :edit will, which is different behaviour. This is why it's slightly slower in some cases. For most purposes, this is not a huge issue, although it can be over NFS connections, or with larger files. The performance should be identical when opening files that are not yet loaded by Vim. You can test/...


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