When need a single or otherwise improperly paired quotation mark, is there a way to stop Kate from turning all the subsequent code red? - kate

I'm writing Tcl with SQlite in Kate and have a replace( ...text..., '"', '\"' ) in one of my SQL statements to escape the quotation marks. This causes Kate to turn all the code after this line red because of the unpaired quotation marks. The code runs successfully but it's a bit annoying to look at now.
Am I dong something wrong and/or is there a way to change this behavior?
Thank you.
ADDITION:
This isn't a Kate solution exactly but SQlite has a char() function that returns strings based on their unicode value. So, this removed the need for typing the quotation marks.
replace( ...text..., char(34), '\' || char(34) )

Related

Python - Removing parentheses and quotation marks

I've been trying to remove pair parentheses(including text between them), unbalanced parentheses and quotation marks from the string.
What I've done so far:
import re
sample_text = '""sads"add"sfsfdsfds()()(0sefdAAAsfs)dasdad(asd'
res = re.sub(r'\([^)]*\)', '', sample_text))
It matches with only ()()(0sefdAAAsfs) part of the text. Unbalanced and quotation marks left unmatched. What can be done to improve above regex?
This isn't really something that a regular expression is suited for, so is not the right tool for the job. Having said that, you can use the following pattern to see if there is an opening paren, then zero or more non-paren, and a matching closing paren:
\([^)+]*\)
Substitute a " or ' or [ or whatever for the other types of matching components.
But again, this would not work with something like this:
(asdf))))))))
Long story short: it's not a problem that a regular expression is capable of solving. Try testing it out here: https://regex101.com/r/bdiK5W/2.

Should I use single or double quotes in my .vimrc file?

What’s the difference between single (') and double (") quotes in Vim? Does it make speed differences? Is it better to use one or another when running functions inside it? Does it matter at all?
I’m interested specifically in their use in the .vimrc file.
I’m asking because I find people use both in the same thing, and I’m wondering what are the differences. I tried to Google this, but wasn’t able to find anything.
Double quotes allow for interpolation whereas single quotes do not.
For example, using double quotes :echo "foo\nbar" will output foo and bar on separate lines whereas :echo 'foo\nbar' will not interpret \n as a line break and will output foo\nbar literally.
For more info on different types of quotes type :h 41.2 for the help file and read the part near the end of the section with the heading STRING VARIABLES AND CONSTANTS.
This said, don't confuse quotes for strings with the double quote at the beginning of a line comment. Single quotes never start line comments, only double quotes do.

New lines in tab delimited or comma delimtted output

I am looking for some best practices as far as handling csv and tab delimited files.
For CSV files I am already doing some formatting if a value contains a comma or double quote but what if the value contains a new line character? Should I leave the new line intact and encase the value in double quotes + escape any double quotes within the value?
Same question for tab delimited files. I assume the answer would be very similar if not the same.
Usually you keep \n unaltered while exploiting the fact that the newline char will be enclosed in a " " string. This doesn't create ambiguities but it's really ugly if you have to take a look to the file using a normal texteditor.
But it is how you should do since you don't escape anything inside a string in a CSV except for the double quote itself.
#Jack is right, that your best bet is to keep the \n unaltered, since you'll expect it inside of double-quotes if that is the case.
As with most things, I think consistency here is key. As far as I know, your values only need to be double-quoted if they span multiple lines, contain commas, or contain double-quotes. In some implementations I've seen, all values are escaped and double-quoted, since it makes the parsing algorithm simpler (there's never a question of escaping and double-quoting, and the reverse on reading the CSV).
This isn't the most space-optimized solution, but makes reading and writing the file a trivial affair, for both your own library and others that may consume it in the future.
For TSV, if you want lossless representation of values, the "Linear TSV" specification is worth considering: http://paulfitz.github.io/dataprotocols/linear-tsv/index.html
For obvious reasons, most such conventions adhere to the following at a minimum:
\n for newline,
\t for tab,
\r for carriage return,
\\ for backslash
Some tools add \0 for NUL.

In vim, is there a plugin to use % to match the corresponding double quote (")?

The % key is one of the best features of vim: it lets you jump from { to }, [ to ], and so on.
However, it does not work by default with quotes: Either " or ', probably because the opening and closing quote are the same character, making implementation more difficult.
Thinking a bit more about the problem, I'm convinced that it should be implemented, by counting if the number of preceding quotes is odd or even and jumping to the previous or next quote, accordingly.
Before I try to implement it myself, I'd just like to know if someone already has?
Depending on your reason for needing this, there may be a better way to accomplish what you're looking for. For example, if you have the following code:
foo(bar, "baz quux")
^
and your cursor happens to be at the ^, and you want to replace everything inside the quotes with something else, use ci". This uses the Vim "text objects" to change (c) everything inside (i) the quotes (") and puts you in insert mode like this:
foo(bar, "")
^
Then you can start typing the replacement text. There are many other text objects that are really useful for this kind of shortcut. Learn (and use) one new Vim command per week, and you'll be an expert in no time!
Greg's answer was very useful but i also like the 'f' and 'F' commands that move the cursor forward and backward to the character you press after the command.
So press f" to move to the next " character and F" to move to the previous one.
I have found this technique very useful for going to the start/end of a very long quoted string.
when cursor is inside the string, visually select the whole string using vi" or vi'
go to start/end of the string by pressing o
press escape to exit visual select mode
this actually takes the cursor next to the start/end quote character, but still feels pretty helpful.
Edit
Adding Stefan's excellent comment here which is a better option for anyone who may miss the comment.
If you use va" (and va') then it will actually visually select the quotes itself as well.
– Stefan van den Akker
I'd like to expand on Greg's answer, and introduce the surround.vim plugin.
Suppose that rather than editing the contents of your quotes, you want to modify the " characters themselves. Lets say you want to change from double-quotes to single-quotes.
foo(bar, "baz quux")
^
The surround plugin allows you to change this to
foo(bar, 'baz quux')
^
just by executing the following: cs"' (which reads: "change the surrounding double-quotes to single-quotes").
You could also delete the quote marks simply by running: ds" (which reads: "delete the surrounding double-quotes).
There is a good introduction to the surround plugin here.
I know this question is old but here is a plugin to use % to match the corresponding double quote:
https://github.com/airblade/vim-matchquote

Replacing quote marks around strings in Vim?

I have something akin to <Foobar Name='Hello There'/> and need to change the single quotation marks to double quotation marks. I tried :s/\'.*\'/\"\0\" but it ended up producing <Foobar Name="'Hello There'"/>. Replacing the \0 with \1 only produced a blank string inside the double quotes - is there some special syntax I'm missing that I need to make only the found string ("Hello There") inside the quotation marks assign to \1?
There's also surround.vim, if you're looking to do this fairly often. You'd use cs'" to change surrounding quotes.
You need to use groupings:
:s/\'\(.*\)\'/\"\1\"
This way argument 1 (ie, \1) will correspond to whatever is delimited by \( and \).
%s/'\([^']*\)'/"\1"/g
You will want to use [^']* instead of .* otherwise
'apples' are 'red' would get converted to "apples' are 'red"
unless i'm missing something, wouldn't s/\'/"/g work?
Just an FYI - to replace all double quotes with single, this is the correct regexp - based on rayd09's example above
:%s/"\([^"]*\)"/'\1'/g
You need to put round brackets around the part of the expression you wish to capture.
s/\'\(.*\)\'/"\1"/
But, you might have problems with unintentional matching. Might you be able to simply replace any single quotes with double quotes in your file?
You've got the right idea -- you want to have "\1" as your replace clause, but you need to put the "Hello There" part in capture group 1 first (0 is the entire match). Try:
:%/'\(.*\)'/"\1"
Shift + V to enter visual block mode. Highlight the lines of code you want to remove single quotes from.
Then hit : on keyboard
Then type
s/'//g
Press Enter.
Done. You win.
Presuming you want to do this on an entire file ...
N Mode:
ggvG$ [SHIFT+:]
X Mode:
'<,'>/'/" [RET]

Resources