r_vim – Telegram
r_vim
71 subscribers
1.24K photos
60 videos
1 file
19.5K links
Download Telegram
Vim: unsaved buffer edits remain even when switching buffers

Here's my workflow:

1. `vi foo bar` (two files that exist)
2. Make a change in the `foo` buffer
3. `:bn`—fails with the message "No write since last change (add ! to override)"
4. `:bn!`—switches to the `bar` buffer
5. `:bp`—switches to the `foo` buffer

At this point, I would expect to be seeing foo in its original state, i.e. without the edit I made at step 2. However, I *do* see the edits, so my questions are:

* Why does `:bn` fail if no 'harm' comes of it?
* What is the point of `:set hidden`? I've read that this command will instruct the current buffer to 'keep changes in memory', but that seems to be happening anyway.
* Is there a way to switch buffers and discard changes? I don't really need to do this, I'm just wondering if it's possible.



https://redd.it/1qobeg2
@r_vim
:move/:copy between buffers?

I’ve started working more frequently using vim with :split or :vsplit, and find myself wanting
to grab sections of text from one window(/buffer) and chuck them into a different buffer. If I were working in the
same file, I’d do a good old-fashioned visual-select-and-:'<,'>copy, but it :help :copy (and
:help {address}) didn’t give me any help as far as specifying a destination address in another buffer.

One solution to this would be to write up some vimnoscript, but I figured that it was worth checking
whether someone else knew some esoteric thing that would help out here.

https://redd.it/1qn0dlj
@r_vim
Quick Q: How do you re-type when you typo?

When I'm writing in vim insert-mode, and I notice I have written a typo in either the current word or one before, I find I tend to just hammer the backspace key. Sometimes I escape, 'b' then 'cw'. I'm curious to know what method you use to fix typos on the fly.

https://redd.it/1qrarsl
@r_vim
BufExplorer yields "Press ENTER or type command to continue"

BufExplorer is a plugin that lets users easily navigate between files that they have opened. When invoked, it shows these files in a split window, along with state metadata and the folder path. You can quickly go to the folder using Vim's gf command. I've grown highly dependent on BufExplorer, using it synergistically with tabs and windows within Vim. The files can be shown in various sort orders (by name, most recently used, and other criteria I don't recall). You can trim the list by deleting line times, which correspond to files that have been opened (it doesn't delete the file, just it's appearance in BufExplorer).

While indispensable, I do get tripped up by a wrinkle. I've seen the message in the subject line above for years if not decades. Not always, but once it starts, it never goes away. For someone use to finger muscle memory, it's not just friction-- it's a pothole.

Using ":set verbose=9", I found the problem to be:

E303: Unable to open swap file for "BufExplorer", recovery impossible

It turns out that my current working directory (":pwd") no longer exists. The solution was as simple as switching to a known existing directory, e.g., ":cd \~/tmp".

I find it easy to lose track of my working directory and get into this situation if:

I use Vim for days on end so that I'm relying on BufExplorer to switch between files
I rely heavily on a command line to pipe the full path of target file into the system clipboard and get Vim to switch to it using ":e <Ctrl+R>*"
I routinely use the Vim "gf" command in the path field of BufExplorer to a folder, making my "cwd" somewhat irrelevant
A commentor said it was odd for a buffer list to use a swap file. While I'm not certain, it is possibly a side-effect of sourcing a mksession file to resume a Vim session in a new instance of Vim at a later date. I'm not sure how that impacts the invocation :BufExplorer -- it's just a guess at a contributing factor.

https://redd.it/1qscvuh
@r_vim
Search and replace the two-character [ string

I use vim to edit LaTeX files among other things and I have run across a string pattern that I cannot figure out how to find with a sed-like substitution command. Suppose I want to replace the string "\\[" with "foo". Nothing I have tried in vim is capable of identifying the "\\[" sequence. Here are the things I have tried:

:%s/\\\\\[/foo/g
:%s/\\[/foo/g
:%s/"\\\["/foo/g
:%s/'\\['/foo/g

I thought the first one should work, but then I just started trying other stuff. In each case I get this error: "E486: Pattern not found: \\\\[/foo/g

Oddly enough, I *can* forward search to find the next occurrence of that sequence in the usual way: /\\\\[

Can someone please set me straight?

https://redd.it/1qu5rh9
@r_vim
Formatting of status line

Vim novice here. I understand that this is code for configuring the status line. Don't remember where I got it. I sense I may want to use it. I am not competent to read how it configures it. Help appreciated.

" Format the statusline

set statusline=CWD:\\ %{CurDir()}%h\\ \\

set statusline+=\\ \\ File:\\ %{HasPaste()}%t%m%r%y%w[%{&fenc}\]\\ \\ Line:\\ %l/%L:%c\\ \\ Value:\\ 0x%B\\ \\ %<%p%%

set statusline+=[wc:%{WordCount()}\]

Thanks.

https://redd.it/1qwu463
@r_vim
How to horizontally scroll large popups?

Say that I have a huge table displayed in a popup.

Although I can add some keys in the popup filter function to scroll up and down, with entries like:

 \# Move up   
if \["\\<C-n>", "\\<Down>", "j", "\\<ScrollWheelDown>"\]
win\_execute(id, "normal! \\<c-e>")
\# Move up
elseif \["\\<C-p>", "\\<Up>", "k", "\\<ScrollWheelUp>"\]
win\_execute(id, "normal! \\<c-y>")


I tried with:

  elseif key == "l"
win_execute(id, "normal! zl")
elseif key == "h"
win_execute(id, "normal! zh")


but it does not work.

Does anyone knows if it is possible? Because if not, then I could open an feature request on the issue tracker of vim.

https://redd.it/1qxlibf
@r_vim
Do you actually need a whole plugin just for surrounding?

lately i've been learning about vimnoscript after ignoring it for a long time, and i thought about doing some stuff in order to.. you know, learn by doing.

i got the basic surrounding working with 7 lines, and the 8th one for the mapping.

def Surround(): void
echo "Enter surround char..."
var pairs = {'(': ')', '{': '}', '[': ']', '<': '>'}
var lhc = nr2char(getchar())
var rhc = pairs->get(lhc, lhc)
execute "normal! `>" .. (visualmode() ==# "V" ? "g_" : "") .. "a" .. rhc .. "\<Esc>`<" .. (visualmode() ==# "V" ? "g^" : "") .. "i" .. lhc .. "\<Esc>"

enddef


I think it's obviously not the best surrounding snippet out there but am i missing something? this feels more than enough for basic quotes and brackets. what do you think?

Edit: code format

https://redd.it/1qzl7g0
@r_vim
Manual creating CNC code, is Vim a good fit?

Hey y'all. I need help learning if I can use Vim to make large swaths of code quickly or if something else is a better fit. Example here:

G13D0R0.4217Z-0.0900
G13D0R0.4218Z-0.0901
G13D0R0.4219Z-0.0902

G13D0R0.4336Z-0.1019
G13D0R0.4337Z-0.1020
G13D0R0.4338Z-0.1021



I was once a manual code editor a decade ago and used Vim with snippets to create lots of NC code at an old job, rather simple snippets back then. I moved jobs and have only programmed with MasterCAM the past decade but a recent feature would have been faster to write the code by hand. How can I learn? I remember some but it has been so long I barely remember :wq but I did find an example of :put =range(4217,4338) to get part of what I needed but can't figure out if I can get everything at once. The example on Vim tips wiki :for i in range(1,10) | put ='192.168.0.'.i | endfor makes me think I should be able too do that, can such a function handle multiple variables? Like i j k? I will need to wait for IT to install Vim so it may take a while. Is there better ways to get what I want? Thanks.

Edit: I'm on windows at work, WSL is disabled. Anything not native would need IT approval which might take a while.

https://redd.it/1qzrynd
@r_vim
hatsune miku color scheme

https://preview.redd.it/4oump8ckydig1.png?width=1900&format=png&auto=webp&s=0dc2f6622b2a0fc6a80d1b96204d008f0eda7664

I've been using this minimal colorscheme I hacked together for half a year now. Draws inspiration from Atom editor's default font, as well as elflord.

I would love to get some feedback on this! I mostly code in C so I'm sure there's a lot of different cases in different languages that I didn't catch. I would also love any suggestions that would help readability etc.

https://github.com/cccfire/vim-miku-colors/tree/main

https://redd.it/1qzt76z
@r_vim
Flash/Jump/Hop to fold headers?

So I love my easy motion style navigation, but I cant seem to find a way to use it to navigate to UFO folded headers because I guess the header is virtual text or not part of the searchable buffer.

Has anyone found a solution to this?

https://redd.it/1r086ku
@r_vim
Is there a way to include it in :messages but prevent to show it on-screen?

As per noscript.
I want to place a message in :message but I don't want it to appear on screen.
To me the obvious would be :silent echom "foo" but foo is not placed in :messages, nor it is shown on screen.

https://redd.it/1r07ir9
@r_vim
Vim 9.2 has been released: Vim 9.2 brings significant enhancements to the Vim9 noscripting language, improved diff mode, comprehensive completion features, and platform-specific improvements including experimental Wayland support.
https://www.vim.org/vim-9.2-released.php

https://redd.it/1r4ymj3
@r_vim