or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

completion.mdconfiguration.mdhistory.mdhooks.mdindex.mdline-editing.md

line-editing.mddocs/

0

# Line Editing

1

2

Functions for accessing and manipulating the current line buffer, enabling custom editing behavior and text insertion capabilities. These functions provide direct access to the readline line buffer during interactive input.

3

4

## Capabilities

5

6

### Line Buffer Access

7

8

Functions for examining the current state of the readline input buffer, useful for custom completion and editing functions.

9

10

```python { .api }

11

def get_line_buffer():

12

"""

13

Return the current contents of the line buffer.

14

15

Returns:

16

str: Current line buffer contents

17

"""

18

```

19

20

**Usage Example:**

21

22

```python

23

import readline

24

25

def smart_completer(text, state):

26

# Access the full line being edited

27

line_buffer = readline.get_line_buffer()

28

29

# Provide context-aware completion based on full line

30

if line_buffer.startswith('cd '):

31

# Directory completion for cd command

32

import os

33

import glob

34

matches = glob.glob(text + '*')

35

directories = [match for match in matches if os.path.isdir(match)]

36

return directories[state] if state < len(directories) else None

37

elif line_buffer.startswith('python '):

38

# Python file completion

39

matches = glob.glob(text + '*.py')

40

return matches[state] if state < len(matches) else None

41

42

return None

43

44

readline.set_completer(smart_completer)

45

```

46

47

### Text Insertion

48

49

Functions for programmatically inserting text into the current command line, useful for custom editing commands and macros.

50

51

```python { .api }

52

def insert_text(string):

53

"""

54

Insert text into the command line.

55

56

Parameters:

57

- string (str): Text to insert at current cursor position

58

59

Returns:

60

None

61

"""

62

```

63

64

**Usage Example:**

65

66

```python

67

import readline

68

69

def insert_common_commands():

70

"""Example function to insert commonly used text"""

71

# This could be bound to a key combination

72

common_commands = [

73

"ls -la",

74

"cd ~/",

75

"git status",

76

"python -m",

77

]

78

79

# In practice, you'd select based on context or user choice

80

readline.insert_text("ls -la")

81

82

# Insert text during completion or editing

83

def custom_pre_input_hook():

84

# Insert default text when starting a new line

85

current_line = readline.get_line_buffer()

86

if not current_line: # Empty line

87

readline.insert_text("$ ") # Add prompt-like prefix

88

89

readline.set_pre_input_hook(custom_pre_input_hook)

90

```

91

92

### Display Control

93

94

Functions for controlling how the current line is displayed, useful when programmatically modifying the line buffer.

95

96

```python { .api }

97

def redisplay():

98

"""

99

Change what's displayed on screen to reflect current line buffer contents.

100

101

Returns:

102

None

103

"""

104

```

105

106

**Usage Example:**

107

108

```python

109

import readline

110

111

def modify_and_refresh():

112

"""Example of modifying line buffer and refreshing display"""

113

# Get current content

114

current = readline.get_line_buffer()

115

116

# Clear current line (this would normally be done differently)

117

# This is just for demonstration

118

if current:

119

# Insert modified text

120

readline.insert_text(" --verbose")

121

122

# Force display update

123

readline.redisplay()

124

125

# Practical usage in a completion display hook

126

def custom_display_hook(substitution, matches, longest_match_length):

127

"""Custom completion display with line buffer modification"""

128

print(f"\nAvailable completions ({len(matches)}):")

129

for i, match in enumerate(matches[:10]): # Show first 10

130

print(f" {i+1}. {match}")

131

132

if len(matches) > 10:

133

print(f" ... and {len(matches) - 10} more")

134

135

# Refresh the input line after our custom display

136

readline.redisplay()

137

138

readline.set_completion_display_matches_hook(custom_display_hook)

139

```

140

141

## Advanced Line Editing Patterns

142

143

### Context-Aware Editing

144

145

```python

146

import readline

147

148

def context_aware_editor():

149

"""Example of context-aware line editing"""

150

line = readline.get_line_buffer()

151

152

# Auto-complete file paths

153

if line.endswith(' '):

154

last_word = line.split()[-1] if line.split() else ""

155

if last_word.startswith('/'):

156

# Insert common path completion

157

readline.insert_text("home/user/")

158

159

# Auto-insert common parameters

160

if line.strip().endswith('git'):

161

readline.insert_text(" status")

162

readline.redisplay()

163

164

# Use with pre-input hook for automatic editing

165

readline.set_pre_input_hook(context_aware_editor)

166

```

167

168

### Command Line Macros

169

170

```python

171

import readline

172

173

class CommandMacros:

174

def __init__(self):

175

self.macros = {

176

'll': 'ls -la --color=auto',

177

'la': 'ls -A',

178

'l': 'ls -CF',

179

'grep': 'grep --color=auto',

180

}

181

182

def expand_macro(self):

183

"""Expand macros in the current line"""

184

line = readline.get_line_buffer()

185

words = line.split()

186

187

if words and words[0] in self.macros:

188

# Replace the macro with its expansion

189

expanded = self.macros[words[0]]

190

new_line = expanded + ' ' + ' '.join(words[1:])

191

192

# Clear current line and insert expanded version

193

readline.insert_text('\b' * len(line)) # Backspace to clear

194

readline.insert_text(new_line)

195

readline.redisplay()

196

197

# Usage would require key binding setup through parse_and_bind

198

```