Can I emulate arrow key in sublimetext3? - sublimetext3

In short:
I want to type [left arrow], [right arrow], [command-v] sequence whenever I press [command-v] in sublime text 3.
In length: I have a problem with IME and pasting in sublime text (OSX). If you don't use asian language you might not understand what I'm saying but let me try.
I'm korean. My language has letters like '각' and '가'. Do you notice the difference? The prior letter have additional 'ㄱ' at its bottom.
To type in those letters, you press 'ㄱ' -> 'ㅏ' -> 'ㄱ' for prior, and 'ㄱ' -> 'ㅏ' for latter. Again, do you notice the difference?
To complete the letter '각', you should make the letter '가' first, and type in additional 'ㄱ'. However, you want '가' this time, so typed 'ㄱ' -> 'ㅏ' and completed the letter '가', but sublime (or OSX IME) doesn't know whether you completed, or should wait for the additional ㄱ to complete the letter '각'. So it awaits next type to decide to start new letter, or to complete something like '각'. My problem begins here.
You completed the letter and want to paste something, so press [command-v], but sublime was waiting for additional component and did not finish the letter. It just deletes the letter '가'. To work around this problem, I type in something like [space] or [arrow key] to make sure sublime completes the letter. This is fairly annoying.
So, I want to automate the annoying potion. Can I type [left arrow], [right arrow], [command-v] sequence whenever I press [command-v] in sublime text 3? [space], [backspace], [command-v] or any combination will work, I think.
Any help will be highly appreiciated

I think a solution to your dilemma could be to create a macro. To do this, look in Tools -> Record Macro, which you'll also notice is triggered by Ctrl + Q. Once you activate that, perform the key combination you wish to invoke. In this case, the sequence:
Left Arrow
Right Arrow
Ctrl + V
Now go back into Tools -> Stop Recording Macro. Now, it won't be explicitly saved yet - your macro is now placed on the buffer, and you can play it back with Tools -> Playback Macro. To save it for continued use, use Tools -> Save Macro... and save it somewhere (most commonly in .../Packages/User, although by no means do you need to save it here).
For reference sake, your macro should look exactly like this:
[
{
"args":
{
"by": "characters",
"forward": false
},
"command": "move"
},
{
"args":
{
"by": "characters",
"forward": true
},
"command": "move"
},
{
"args": null,
"command": "paste"
}
]
That text was generated by following my steps exactly and recording the macro appropriate to your situation.
And to use this macro, we'll need to do a little more tinkering.
Look in Preferences -> Key Bindings - User. This will open a rather derelict text file (assuming you haven't touched it yet) where you can specify your own keyboard mappings. These will override the default settings (located in Preferences -> Key Bindings - Default, if you wish to check those out).
So in this "User" key bindings file, we need to specify that the normal Ctrl + V will be overridden by our custom macro. An empty "User" key bindings file will look like this:
[
]
Not much to extrapolate from. The syntax required here is fairly legible, but pretty difficult to guess at without prior knowledge. Add this entry between those brackets, like so:
[
{
"keys": ["ctrl+v"], "command": "run_macro_file", "args": {"file": "path/to/your/<macro_name>.sublime-macro"}
}
]
Save that file, and you should be good to go. You may need to restart Sublime for this to take effect.

Related

Sublime text 3: possible to bind to multiple (sequential) keypresses?

I'm giving Sublimetext 3 a go as a long time VIM user. One thing I'm wondering is if there is a way to bind to multiple sequential keys. In VIM I have used imap hh => which lets me type 2 h's in sequence and get a =>. Is there any way to set this up in ST3?
The keys key in a key binding is a JSON list and can contain multiple keys that must be hit in sequence in order for the binding to activate. For example, the default key binding for opening the Sidebar (on Windows/Linux) is declared as the following, requiring you to press Ctrl+k followed by Ctrl+b:
{
"keys": ["ctrl+k", "ctrl+b"],
"command": "toggle_side_bar"
},
It's also possible to bind unmodified keys as well, such as "h" (although you cannot bind just a modifier by itself like "ctrl"), so for your purposes you can do something like the following:
[EDIT] Starting with ST4, you can indeed bind just a modifier key if desired. [/EDIT]
{
"keys": ["h", "h"],
"command": "insert",
"args": {
"characters": "=>"
},
},
{
"keys": ["h", "i"],
"command": "insert",
"args": {
"characters": "->"
},
},
These use the built in insert command to insert a specific set of text; this command is smart enough to ensure that the insertion happens at all carets in the file. As seen in this example you can have multiple such keys defined if you like as well.
Note however that a key binding such as this sample is somewhat naive in that it will block you from actually typing these characters in a row, such as that second example effectively blocking you from typing the word this without waiting a bit after entering the h to allow Sublime to time out the key chord.
To get around that you can employ a context of some sort that constrains the availability of that key binding.
If you take advantage of some Vim type package for Sublime such as NeoVintageous they would also allow you to use more vi-like bindings directly as well.

Sublime: How can I jump n lines with the keyboard arrows?

I feel very tedious when I have to percor the text file with the arrow, line by line, but I feel that PageDown/PageUp makes me lose where I was.
Is there a way that I can simply jump n lines using a simply shortcut (like ctrl+arrow down/up)? I think that 5 lines would fits me very well.
The built in movement commands allow you to move in a variety of ways, but only one move at a time (e.g. one line up, one word left, one page down, etc).
As Ben mentions in his answer, one way around that is to create macros that make the movements that you want, bind keys to run the macros, and you're good to go.
Another alternative is to use a simple plugin such as the following (originally from this forum post), which you can use by selecting Tools > Developer > New Plugin... from the menu, replacing the stub code with this code, and then saving in the default location as something like move_amount.py:
import sublime
import sublime_plugin
class MoveAmountCommand(sublime_plugin.TextCommand):
def run(self, edit, amount=1, **kwargs):
for _ in range(amount):
self.view.run_command("move", args=kwargs)
This creates a command named move_amount that wraps the internal move command, providing an extra argument of amount to indicate how many times to take the move action. This can be handy if you have a few such bindings to make as it cuts down on the number of macros that you have to make and it's easier to customize them.
With that in place you can use the following key bindings, modifying the amount as desired:
{
"keys": ["ctrl+up"], "command": "move_amount",
"args": {"by": "lines", "amount": 5, "forward": false}
},
{
"keys": ["ctrl+down"], "command": "move_amount",
"args": {"by": "lines", "amount": 5, "forward": true}
},
Note that these keys are already bound to the scroll_lines command, which scrolls the viewport but leaves the caret alone, so if you use that functionality as well you may want to select different bindings.
From what I can tell there isn't an actual keyboard shortcut that will let you combine a move lines command with an amount to move.
Instead you can record & save two different macros. One moving up 5 lines, the other moving down 5 lines - (start recording macro, then hit the up or down arrow x number of times. stop recording, then save.).
Then you can create a binding to call them from your preferences file:
[
{ "keys": ["shift+alt+up"], "command": "run_macro_file", "args": {"file": "res://Packages/User/up-five-lines.sublime-macro"} },
{ "keys": ["shift+alt+down"], "command": "run_macro_file", "args": {"file": "res://Packages/User/down-five-lines.sublime-macro"} }
]

Sublime Text 3 disable SHIFT+Delete copying the selected text

Is it possible to disable this behaviour? It's possible in Visual Studio but I would like to change this also on ST3.
I think that the original question is not about removing the functionality of shift+delete, but only removing the copy function from it. So I'd like to post my answer here in case somebody would be looking for specifically this.
I found right_delete, mentioned in the comments, to be rather inconvenient, because you would need your cursor to be at the beginning of the line in order to work. I found that line deletion is set to ctrl+shift+k by default (on Windows), so I just used its macro for shift+delete.
So here's the line I added to my Preferences > Key Bindings:
{ "keys": ["shift+delete"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Line.sublime-macro"} },
This way you don't need to worry about the position of your cursor, as long as it's within the wanted line.
On Windows/Linux, the Default key bindings map Shift+Delete to the cut command:
{ "keys": ["shift+delete"], "command": "cut" },
In order to disable that behaviour, you can select Preferences > Key Bindings from the menu, and add the following to the binding to the right hand pane:
{ "keys": ["shift+delete"], "command": "noop" },
Alternately you can replace noop with some other command that you would rather perform in this case instead, should you want to use the key for something else.

Make cmd+| map to command in sublime text

In Atom cmd+| maps to toggling the file tree, how can I make sublime text do the same when these keys are pressed
If by toggling the file tree you mean hiding and showing the side bar, what you want to do is remap the key bound to the command that does that by default.
Note: Based on your use of cmd in the question, I'm assuming you're on a Mac so the answer is tailored for that. The operation remains the same but the key name will be different for the other platforms.
If you look at the default key bindings (available from the Preferences menu, which under MacOS is part of the Sublime Text menu entry) you can determine what keys are bound to what by default. In this case the command you want is toggle_side_bar; if you don't know the command you can find it by searching the key map to find the keys that you know you press to do this normally.
With that information, the default key binding for MacOS is:
{ "keys": ["super+k", "super+b"], "command": "toggle_side_bar" },
In your own custom key bindings file (available from the same menu as the defaults) you just need to insert your own key binding. For your case, that would look like this:
{ "keys": ["super+\\"], "command": "toggle_side_bar" },
You might think that the key should be super+| since that is what you want to bind to ultimately; however that will not work because in order to generate that character, you need to be pressing Shift, and thus without shift as a part of the key binding, it won't work.
Instead we use the character that is generated if you press the key without shift, which on my keyboard is \. That is a special character in JSON strings, so it needs to be doubled in order to be correct.
For keyboard layouts different from US QWERTY, you might need to swap that character for something else. You can determine the binding you want by opening the Sublime console with View > Show Console, entering the command sublime.log_input (True) and then pressing the key combination in question; Sublime will tell you what it thinks you pressed.

Sublime Text 2 vintage key mapping like vim

i am working with Sublime Text2 in vintage mode. I disabled the arrow keys, so i don't use them to move the cursor in insert mode. Now is was wondering, if it is possible, to map the up/down keys, so that they will move a line of code up and down. In vim it is easy possible by just mapping the keys to do a sequence like "dd k P" to delete the line move cursor up and past it above.
The syntax for the key mapping in Sublime still is quite complicated to me as a beginner.
Thanks
Insert the following into your user key bindings.
[
{ "keys": ["up"], "command": "swap_line_up" },
{ "keys": ["down"], "command": "swap_line_down" }
]
The key mapping file is just JSON. There are 4 keys.
keysis a list of key entries. An entry will generally be something like ["<modifier> + <character>"]. You can define multi level keybindings by creating additional entries in the array. An example of this is to show and hide the side bar. The entry for this is ["ctrl+k", "ctrl+b"]. The available keys are described here.
command is a string specifying the command to run. To see what command is running with a particular action, you can enter sublime.log_commands(True) in the ST console.
args are the arguments passed to the command. This is a dictionary object. The keys for this correspond to the parameter name for a given command.
context is a list of dictionary entries to conditionally execute a given command. These can be somewhat complicated. There is a reference for context here.
I think the best way to familiarize yourself with the key bindings is to just try things out. I used the default keys as a reference.
You might want to keep this as a reference.
You can run a series of commands by creating macros. These are just lists of commands and arguments and are further described here.

Resources