How to hard-wrap lines in VS Code at a specific character (NOT word wrap) - visual-studio-code

I have a Base64 encoded string in a text file that is one line. In other words it contains no line breaks. I want to insert a line break every 78 characters.
None of the "wrap" extensions I have found do this, since they're geared for word wrapping and use word boundaries. Running any of these functions does nothing since the string contains no spaces or word boundaries.
I can do it on Unix using something like fold -w 78 but this seems like something that should exist in VS Code or at least an extension.

I'm not aware of an extension that does specifically what you're asking for, but what I would do is use the Edit with Shell Command extension, and use it to run fold -w 78 on the text in question from within VSCode. The extension even has a "quick command" feature you can use to save that command for quick use if it is something you do often.
I use that extension fairly often for one-off transformations with things like sort, sed, tr, and fmt. It's really handy when you know how to express the desired transformation as a shell command.

Related

Is there a good place in .vim folder to store text filters?

I would like to create a filter folder, best inside .vim and be able to run a text filter just with one file name:! filter.pl
I put up a Perl text filter to change all special Characters in a LaTeX Math Formula, which is running fine so far - only problem it is running on the whole line not the selected formula, but I can live with it ...
#!/usr/bin/perl -np
use strict;
use warnings;
# this filter transforms all special characters in Mathformular for LaTeX
s/\\/\\backslash /g;
s/([\$\#&%_{}])/\\$1/g;
But to call this filter is cumbersome
: '<,'>!"/Users/username/Library/Mobile Documents/com~apple~CloudDocs/my_vim_cheat_sheet/perl_filter.pl"
Apple put in the path to the iCloud a white space, so I have to put "" around! Where I put a collection of text filters?
Thank you for your answers
marek
You can safely create a subfolder with any name different from ones Vim uses itself (see :h 'rtp'). So this is ok:
:*!$HOME/.vim/filters/perl_filter.pl
Also Vim has a predefined interface for a general purpose filter called 'equalprg'. To make use of it simply set a global-local (i.e. both set and setlocal are meaningful) option equalprg to a fully qualified name of your script. Then hit = in visual mode to apply filter (or ={motion} in normal mode). (Read :h 'equalprg' :h =).
If you need several filters at once, and switching equalprg is not convenient, you can still try different options to reduce typing.
For example, mappings, such as
vnoremap <Leader>f :!/path/to/my/filter<CR>
Then hitting \f (or whatever is your "leader" key set) in the visual mode will result in the executing :'<,'>!/path/to/my/filter (note that the visual selection will be applied automatically).
Another attempt is to set a dedicated environment variable (which will be inherited by all child processes including shell(s). For example,
:let $filters = '~/.vim/filters'
:*!$filters/myfilter.pl
Of course, you can put those set equalprg=... vnoremap ... let $filters=... etc.etc. in your vimrc.
I would like to create a filter folder, best inside .vim and be able to run a text filter just with one file name :! filter.pl
Simply add the script to somewhere within your $PATH. Or, if you really only intend to use that from within Vim, then add that directory to your $PATH in your .vimrc, so you have it available there.
For example, if you'd like to use ~/.vim/scripts for your external Perl or shell scripts, you can use this in your ~/.vimrc:
call setenv('PATH', expand('~/.vim/scripts').':'.$PATH)
After that, you can simply use :'<,'> !filter.pl to run it. And Tab completion will work with the name of the script, type :!fil<Tab> and Vim will complete it to filter.pl, assuming it's a unique prefix.
The snippet above for your .vimrc has one minor issue, that if you :source your .vimrc during Vim runtime, it will keep adding the entry to $PATH multiple times. That doesn't typically break anything, only the entry will become longer, you might run into variable length issues.
You can fix it by checking whether that's present in path or not before updating it, perhaps with something like:
let scripts_dir = expand('~/.vim/scripts')
if index(split($PATH, ':'), scripts_dir) < 0
call setenv('PATH', scripts_dir.':'.$PATH)
endif
But also, about this:
I put up a Perl text filter to change all special Characters in a LaTeX Math Formula
s/\\/\\backslash /g;
s/([\$\#&%_{}])/\\$1/g;
Consider writing that in Vim instead.
In fact, almost the same syntax will work as a Vim function:
function! EscapeLatexMathFormula()
s/\\/\\backslash /eg
s/\([$#&%_{}]\)/\\\1/eg
endfunction
You can call it on a range, with:
:'<,'>call EscapeLatexMathFormula()
Calling it without a range will affect the current line only.
You can also make it into a command, with:
command! -range EscapeLatexMathFormula <line1>,<line2> call EscapeLatexMathFormula()
In which case you can simply use:
:'<,'>EscapeLatexMathFormula
You can use tab-completion for the function and command names (though, of course, you can pick shorter names if you'd like, as well.)
Note that user-defined command names need to start with an uppercase letter. Function names can start with an uppercase letter too (there are more options for function names, but making this global with an uppercase is probably the easiest here.)

Only autocomplete on an exact match in Sublime Text 2

I'm making a custom .tmLanguage file to highlight the syntax I'm using correctly and generally make coding with it easier. I'm almost done, and I got the autocompletion working using a .sublime-completions file.
There's just one minor flaw I'd like to change. I have a pretty long list of functions, and almost all of them contain an abbreviation of the word 'parameter', PAR. When I start typing that word, the following are all in the list of completions:
PAR command
DEFPAR command
JDATA command (because the description contains PAR)
SPAA command (because there's a P in the command and an A and an R in the description)
What I want is only for the commands that begin with PAR to show up, so from the list above, only the first item.
So, like this:
In other words, I want the completions to show up based on the literal string I'm typing, and only from the trigger part of my completions file, before the \t only.
That completions file looks like this:
Highlighted in orange is what I want my completions list to be based on.
I hope this is understandable. Any help is greatly appreciated.
This is not possible. By design Sublime's autocomplete feature uses fuzzy matching, so if there are a number of options that all contain the same pattern, but you don't quite remember which one you want, you can type the pattern and have all of the options available. The more you type, the smaller the list of possible options becomes. This is a good thing®, otherwise you'd have to remember the exact command you're looking for, which kind of defeats the purpose of autocomplete and code hinting.

vim: Interactive search and replace with perl compatible regular expressions

According to this page you can use perl compatible regular expression with
:perldo s/pattern/insert/g.
This works fine.
But, how can I get interactive search and replace with PCRE syntax in vim?
Since this does not work with perldo I search for a different solution.
Till the current release version of vim, there is no way to do :s/[perlRegex]/bar/c
So you are asking for a feature that doesn't exist.
You can do matching with verymagic, however it is not Perl Regex compatible flag. It is still using the vimregex engine, just changed the way of escaping regex patterns.
For example, in perl, you can do lookahead/behind (?<=foo)..., (?=foo), (?!foo).., you can use the handy \K : som.*ing\Kwhatever etc, you cannot use those syntax in vim directly, no matter which 'magic' level you have set. Vim has the same feature, but different syntax:
\#=
\#!
\#<=
and also the \zs \ze are very handy, even more powerful than perl's \K.
Vim is an Editor, with vim regex, you can not only do text matching, but also match base on visual selection, cursor position and so on.
If you really need to do complex pattern matching and really need do them in vim, learn vim regex! It is not difficult for you if you "know pcre very well"
Probably the closest you can get is:
:s/\vfoo/bar/gc
I suggest to try eregex plugin. It maps perl compatible regular expressions to VIM regex syntax.
In your example, s/pattern/insert/g is a perl command, not a Vim command using a perl-compatible regular expression syntax.
If perl doesn't have an equivalent of Vim's /c flag you will need to find an alternate method like… writing an actual perl script.

Powershell lowercase tab expansion?

Powershell supports typing commands in lowercase. However, powershell's tab completion will camelcase all commands, which I personally find difficult to parse visually. Is there any way to let powershell's tab expansion/completion default to all lowercase instead of camelcase?
There's no way to change this preference today, but you can pretty well easily hook into the PowerShell ISE if you like. One thing you could do is grab the text off the editor, split on new-line chars ("`n"), and then iterate over the resulting array to parse out cmdlet names, make them lower-case, and append onto a new script-body. This all sounds fairly complex, but it is actually fairly simple to do. I've got something similar in an alias-resolution add-in I wrote, which can be found at https://github.com/briandrennan/PowerShellISE/blob/Edit/Resolve-ScriptAliases.ps1. Basically, you'd want to create some kind of an index (hash table would work well) that stores commands, and then just call ToLower() on the name, and insert in your script over the previous one. That script has all the bits you need to do what you're trying to do.

Using emacs in batch mode to dump a file with syntax highlighting?

I'd like to use emacs in some kind of batch mode to just render a file with syntax highlighting and exit. Specifically, I want to dump the fontified buffer with ANSI escape codes so that it shows up reasonably syntax-highlighted on my terminal. Is there any way to do this?
The ansi-lpr.el library seems to be kind of along the lines of what I want, but the output isn't colorified. I can't figure out how to get over that final hurdle — there are a lot of modules to digest ANSI escape codes into Emacs text properties (e.g. ansi-color.el) but I haven't found anything that does the reverse. If anyone can point me to something that does, I think I can piece together the rest.
Alternatively, I've seen some hacky approaches like this answer (using script and capturing the output) but in my experiments that approach has seemed unlikely to be fruitful — you get tons of undesirable control sequences mixed in with the highlighted text.
The overarching motivation here is to use emacs in a $LESSOPEN pipe to get syntax highlighting when I page files. In case you're going to say it, I've tried and "just page files in Emacs" is not acceptable for me.
I'm glad to announce a new package, e2ansi, that (hopefully) does what you asked for.
The package provides a command-line tool e2ansi-cat that starts Emacs in batch mode, opens files, syntax highlight them (using font-lock), and creates ANSI-colored versions of the syntax highlighted files.
You can integrate this into less by setting the following variables to, for example:
export "LESSOPEN=|emacs --batch -Q -l ~/.emacs -l bin/e2ansi-cat %s"
export "LESS=-R"
export MORE=$LESS
The end result looks like the following:
You can vary the colors and attributes like bold, underline, and
italics by using a suitable Emacs theme.
The e2ansi package is located at https://github.com/Lindydancer/e2ansi
Personal note
I would like to thank you for posting this question, it directly inspired me to write e2ansi.