# TeXstudio: How to put already written text in brackets and set the cursor at opening bracket?

No MWE, for this pertains to the editor itself. I use TeXstudio mainly, but have briefly used TeXmaker a little.

When I write LaTeX documents, I very often find myself writing some text only to realise later, I want that text to be bold, italic, or in some command.

I am looking for an all-purpose way of marking text and then, preferably, some keyboard shortcut to encapsulates the marked text in curly brackets and put the cursor before the first bracket - so that I can type a command before the text or whatever markdown I need.

Marking text and then pressing keys for beginning parenthesis places the cursor at the end of the text, not even at outside the ending parenthesis. So in essence I am asking if it is possible to change what beginning curly bracket with marked text does in TeXstudio.

• what version of TXS are you using on what system? Jul 9, 2018 at 11:05
• TeXstudio: 2.10.8 (hg 5802:5ee1d048e556) Using Qt Version 5.5.1, compiled with Qt 5.5.1 R ; OS: Linux Mint 18.3 Sylvia. Nordic "hardware" keyboard with Danish keyboard settings. Jul 9, 2018 at 11:15
• have you read section 4.5 in the TXS manual? maybe you can create a macro for your purposes. Jul 9, 2018 at 11:25
• I have read about macros before and shortcuts, but the only macro I have ever created was a lot simpler, so I didn't know if this thing could be done with macro. I hoped it maybe would be "just" changing some built-in function in TXS, so telling it to place cursor somewhere else than it does now. But it seems it is indeed a fair but more complex than that. Jul 9, 2018 at 12:01
• The shortcuts for fonts automatically warp the selected text in curly brackets. For example select the text and cmd+I (or whatever the equivalent on your os is) will produce normal text \textit{selected text} normal text Jul 9, 2018 at 12:39

I'm not sure whether this question is asked at the right place, but nonetheless I found it interesting to try. Apparently you can add custom TeXstudio macros. In the main menu, click Macros > Edit macros.... At the right you can name your macro appropriately. The Abbreviation and Trigger option are not needed for this. The Type must be set to Script. Now you can add the following content:

%SCRIPT
c = cursor
var arr = new Array('\\{',c.selectedText(),'}');
str = arr.join('')
c.insertText(str)
c.moveTo(c.lineNumber(),c.columnNumber()-str.length+1)


Now you can achieve what you want with Shift + F1. I think you can even assign your own shortcut in the Options > Configure TeXstudio... menu.

Explanation
c=cursor This line declares a cursor object named c
var arr = new Array('\\{',c.selectedText(),'}'); This line defines an array that starts with \{ and ends with }. In between it places the text that is currently selected in the editor. The second \ is needed to escape, because it is an active character.
str = arr.join('') This line joins the array in one string (using the empty string '' as delimiter) and places it in str.
c.insertText(str) This line simply inserts the text in str.
c.moveTo(c.lineNumber(),c.columnNumber()-str.length+1) This line places the cursor to the position right after the \. The function c.moveTo takes two arguments, the first being the line number, and the second the column number. For the column number I used the current column number, minus the length of str, plus one to account for the \.

• I knew about creating macros, and chancing/adding keyboard shortcuts but the level of complexity concerning the "selected text" is too high for me to figure out for myself. So I would have had no idea how to code anything like the snippet you wrote. I will try with that! I guess I should have prefaced my question with that maybe this is too integral to how TeXstudio has decided its editor should be, and I would live just fine if there is no "easy" solution, especially if this solution lies beyond what is TeXstudio functionality and as such should now have been asked here. Jul 9, 2018 at 11:56
• this script is very specific to {. is it possible to make it more abstract, to have brackets or parentheses as well? or does one have to create scripts for each case? Jul 9, 2018 at 12:04
• @naphaneal I think it would be possible to generalize it for more characters, but that would be at the cost of usability. Personally I would create such scripts for each case, of course with an additional variable for the opening and closing character.
– Max
Jul 9, 2018 at 12:09
• It works. Only "problem" now is that I would like it to be set to the same shortcut as a normal { (or }, anyone of them). For my keyboard, that's AltGr+7 or AltGr+0. But I cannot add use AltGr as a manually set shortcut. Yes it works with Alt+Ctrl of course, but that's one more key. I realize that the use of AltGr for special signs are a country, hardware and system depended issue, so I do not automatically expect TXS to have a solution. But would be cool if it had. Jul 9, 2018 at 12:17
• I am afraid I cannot help you with that. It is possible to use Alt+7 but Alt Gr+7 doesn't work for me either. If you consider your question as answered, would you mark my answer as accepted?
– Max
Jul 9, 2018 at 12:29

If the command that you want to insert is recognized and offered by the TXS autocompleter, the canonical solution is to

i) highlight the part you want to be enclosed in brackets,

ii) type the command (with the command appearing in the autocompleter), and then

iii) pressing Enter.

• if there is only one word you want to highlight in step (i), you can just use the Ctrl+D shortcut.

• as @samcarter mentioned, having font styles like \textbf, \emph, \textit are already available as keyboard shortcuts. Highlight the desired content to be emphasized, then apply the keyboard shortcut, e.g. Ctrl+B etc.

It also works for sub-/superscripts with their associated keyboard shortcuts (Ctrl+Shift+D / Ctrl+Shift+U).

• If the command you selected has multiple arguments/placeholders, your selected text will be entered into the first placeholder (see gif).

• the latest version is 2.12.10, do update if it's available as an option to you.

• It's very unintuitive that the text disappears but is actually still "saved" within TXS and will reappear when you are done typing the command. But good to know about this functionality. It exists for simple font styles, I wanted a way so it could work for all font styles or other non-command changes, like sub/super scripting. The latter has buttons too, yes, but I would like to do this without too much mouse work. As opposed to marking and writing commands, it does not seem that highlighting, and then typing ^ saves the text. Jul 9, 2018 at 13:12
• @snowzky It works for subscripts and superscripts too when using their keyboard shortcuts. I'll edit my post to add that in. There's no mouse work at all.
– Troy
Jul 9, 2018 at 13:14
• OT: @Troy what tool did you use for the animation? Jul 9, 2018 at 14:31
• @naphaneal ScreenToGif with ffmpeg.
– Troy
Jul 9, 2018 at 14:40