Keyboard shortcut to surround a column name with [ and ]
Asked Answered
O

3

8

I'm wondering if there's a keyboard shortcut in SSMS to surround a selected column name (or an uninterupted sequence of characters for the given cursor position) with the [ and ] chars.

So if I have user_id and would press something like CTRL + SHIFT + [ + ] it would turn into [user_id]. Is there such a keyboard shortcut?

Outspan answered 25/3, 2015 at 8:34 Comment(4)
Can I ask why you wish to do this? Square brackets are generally used to wrap keyword used as column names or column names that contain spaces. They aren't required otherwise, unless you think it improves readability?!Bowerman
Sometimes I have columnnames with characters that need to be escaped. So to be consistent with the formatting I prefer to wrap them up. Readability it is indeed.Outspan
I couldn't find it in the documentation, but just incase I've missed it, take a look yourself msdn.microsoft.com/en-us/library/ms174205.aspxVoorhis
Thanks, I was looking for it on the same page as you were. Didn't find anything either.. Too bad I guess.Outspan
A
13

There's no keyboard shortcut out of the box that surrounds text in brackets, but you can you create your own using a custom snippet. You can check out this blog post to get clear steps but I'll list them briefly here.

Open notepad and paste in this xml. Save it as brackets.snippet:

<?xml version="1.0" encoding="utf-8" ?>  
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">  
<_locDefinition xmlns="urn:locstudio">  
    <_locDefault _loc="locNone" />  
    <_locTag _loc="locData">Title</_locTag>  
    <_locTag _loc="locData">Description</_locTag>  
    <_locTag _loc="locData">Author</_locTag>  
    <_locTag _loc="locData">ToolTip</_locTag>  
   <_locTag _loc="locData">Default</_locTag>  
</_locDefinition>  
<CodeSnippet Format="1.0.0">  
<Header>  
<Title>Brackets</Title>  
                        <Shortcut>br</Shortcut>  
<Description>Snippet for Brackets</Description>  
<Author>SQL Super Hero</Author>  
<SnippetTypes>  
                                <SnippetType>SurroundsWith</SnippetType>  
</SnippetTypes>  
</Header>  
<Snippet>  
<Declarations>  
                                <Literal>  
                                <ID>OpenBracket</ID>  
                                <Default>[</Default>  
                                </Literal>  

                                <Literal>  
                                <ID>CloseBracket</ID>  
                                <Default>]</Default>  
                                </Literal>  
</Declarations>  
<Code Language="SQL"><![CDATA[$OpenBracket$$selected$$CloseBracket$$end$]]>  
</Code>  
</Snippet>  
</CodeSnippet>  
</CodeSnippets>

In SSMS, Go to Tools > Code Snippets Manager.

Click import. Find the Brackets.snippet file and click Open

Choose “My Code Snippets” as the location and click finish

Close and reopen SQL Server Management Studio

You should now be able to use the snippet to surround highlighted text in brackets. The keyboard shortcut to access the snippet is ctrl+k, ctrl+s

The most efficient way to do this is with the following sequence of key presses:

Highlight Desired Text > Ctrl+K,Ctrl+S > M > Enter > Enter > Enter

Avocet answered 6/2, 2017 at 17:20 Comment(0)
K
1

place cursor left of word to surround

Type [ then ctl-return

This will use auto-complete to surround the current word with square brackets.

***Works only if the current script is running in the appropriate database for auto-complete to find the name. {column, table, schema, etc.}

Karney answered 12/3 at 15:8 Comment(0)
L
0

I use an AutoHotKey script to do this. You can Google around to figure out how to set up AHK.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; GET RID OF BRACKETS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; winkey + Z

#z::

ClipSaved := ClipboardAll

Send ^c
Clipwait
Sleep 100
str := clipboard
str := RegExReplace(str, "\[|\]")       ; Match '[' or ']' - have to escape each with backslash - and replace with nothing
clipboard := str
Sleep 100
Send ^v

Clipboard := ClipSaved

return


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ADD BRACKETS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; winkey + V

#v::

ClipSaved := ClipboardAll

Send ^c
Clipwait
Sleep 100
str := clipboard
str := "[" . str . "]"
str := RegExReplace(str, "\.", "].[")
clipboard := str
Sleep 100
Send ^v

Clipboard := ClipSaved

return

Usage: highlight your text and press the corresponding hotkey. Currently these macros are set to use the Windows key + Z / V respectively but that can be changed easily.

The add brackets macro is a little finnicky in that it will add a bracket to the end and beginning of whatever you have selected, even if there is whitespace - that could be fixed but I personally haven't had trouble selecting exactly what I want.

Ligure answered 26/7, 2019 at 23:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.